Video streaming on flask no errors but it is only running the app not displaying on the browser
Question:
I want to stream a video on flask frame work. I initially tries to stream a camera video. There is no errors but it not working. Actually it does not stream on html page.It loads the html page but not displaying the video i want.
app.py:
import cv2
import numpy as np
from flask import Flask, render_template, Response
app = Flask(__name__)
@app.route('/')
def index():
# return the rendered template
return render_template("view.html")
def gen():
while True:
capture = cv2.VideoCapture(0)
ret, frame = capture.read()
if ret == False:
continue
# encode the frame in JPEG format
encodedImage = cv2.imencode(".jpg", frame)
yield (b'--framern'b'Content-Type: image/jpegrnrn' + bytearray(np.array(encodedImage)) + b'rn')
@app.route('/video_feed')
def video_feed():
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug = True)
This is my html code.
view.html:
<html>
<head>
<title>Pi Video Surveillance</title>
</head>
<body>
<h1>Pi Video Surveillance</h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>
Answers:
Just write the capture = cv2.VideoCapture(0)
outside while True:
in def gen():
.
Below is the modified def gen()
function:
def gen():
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
if ret == False:
continue
# encode the frame in JPEG format
encodedImage = cv2.imencode(".jpg", frame)
yield (b'--framern'b'Content-Type: image/jpegrnrn' + bytearray(np.array(encodedImage)) + b'rn')
Use Mehul Purohit answer will encounter follow issue.
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
Change this following is work for me.
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + cv2.imencode('.jpg', frame)[1].tobytes() + b'rnrn')
I want to stream a video on flask frame work. I initially tries to stream a camera video. There is no errors but it not working. Actually it does not stream on html page.It loads the html page but not displaying the video i want.
app.py:
import cv2
import numpy as np
from flask import Flask, render_template, Response
app = Flask(__name__)
@app.route('/')
def index():
# return the rendered template
return render_template("view.html")
def gen():
while True:
capture = cv2.VideoCapture(0)
ret, frame = capture.read()
if ret == False:
continue
# encode the frame in JPEG format
encodedImage = cv2.imencode(".jpg", frame)
yield (b'--framern'b'Content-Type: image/jpegrnrn' + bytearray(np.array(encodedImage)) + b'rn')
@app.route('/video_feed')
def video_feed():
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug = True)
This is my html code.
view.html:
<html>
<head>
<title>Pi Video Surveillance</title>
</head>
<body>
<h1>Pi Video Surveillance</h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>
Just write the capture = cv2.VideoCapture(0)
outside while True:
in def gen():
.
Below is the modified def gen()
function:
def gen():
capture = cv2.VideoCapture(0)
while True:
ret, frame = capture.read()
if ret == False:
continue
# encode the frame in JPEG format
encodedImage = cv2.imencode(".jpg", frame)
yield (b'--framern'b'Content-Type: image/jpegrnrn' + bytearray(np.array(encodedImage)) + b'rn')
Use Mehul Purohit answer will encounter follow issue.
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
Change this following is work for me.
yield (b'--framern'
b'Content-Type: image/jpegrnrn' + cv2.imencode('.jpg', frame)[1].tobytes() + b'rnrn')