Skip to content

Commit

Permalink
add video feed
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitner committed Aug 1, 2018
1 parent c500472 commit 3961cf4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 61 deletions.
83 changes: 31 additions & 52 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,40 @@
import os
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
from flask import Flask, url_for, render_template, Response
from darkflow.net.build import TFNet
import cv2
import base64

UPLOAD_FOLDER = 'static'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
options = {"model": "/home/rohitner/tensorflow/darkflow/cfg/tiny-yolo-voc.cfg", "load": "/home/rohitner/tensorflow/darkflow/bin/tiny-yolo-voc.weights", "threshold": 0.1, "gpu": 0.5}
options = {"model": "/home/rohitner/tensorflow/darkflow/cfg/tiny-yolo-voc.cfg",
"load": "/home/rohitner/tensorflow/darkflow/bin/tiny-yolo-voc.weights",
"threshold": 0.1, "gpu": 0.8}
tfnet = TFNet(options)

def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
imgcv = cv2.imread(os.path.join(app.config['UPLOAD_FOLDER'], filename))
results = tfnet.return_predict(imgcv)
for result in results:
cv2.rectangle(imgcv,
(result["topleft"]["x"], result["topleft"]["y"]),
(result["bottomright"]["x"], result["bottomright"]["y"]),
(255, 0, 0), 4)
text_x, text_y = result["topleft"]["x"] - 10, result["topleft"]["y"] - 10

cv2.putText(imgcv, result["label"], (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (0, 255, 0), 2, cv2.LINE_AA)
cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], filename), imgcv)
# return redirect(url_for('send_file', filename=filename))
# ret, frame_buff = cv2.imencode('.jpg', imgcv)
# frame_b64 = base64.b64encode(frame_buff)
return redirect(url_for('show_video',filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''

@app.route('/video/<filename>')
def show_video(filename):
return render_template('webcam.html', fl=filename)

@app.route('/uploads/<filename>')
def send_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
def gen(camera):
while True:
success, imgcv = camera.read()
results = tfnet.return_predict(imgcv)
for result in results:
cv2.rectangle(imgcv,
(result["topleft"]["x"], result["topleft"]["y"]),
(result["bottomright"]["x"], result["bottomright"]["y"]),
(255, 0, 0), 4)
text_x, text_y = result["topleft"]["x"] - 10, result["topleft"]["y"] - 10

cv2.putText(imgcv, result["label"], (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (0, 255, 0), 2, cv2.LINE_AA)
ret, jpeg = cv2.imencode('.jpg', imgcv)
frame = jpeg.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
cam = cv2.VideoCapture(0)
return Response(gen(cam),mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/')
def webcam():
return render_template('webcam.html')

if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)
Expand Down
16 changes: 7 additions & 9 deletions templates/webcam.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<html>
<head></head>
<body>
hiiii
<p>
{% if result != None %}
<img src="{{url_for('static', filename=fl)}}"></img>
{% endif %}
</p>
</body>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img id="bg" src="{{ url_for('video_feed') }}">
</body>
</html>

0 comments on commit 3961cf4

Please sign in to comment.