Issue
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'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + bytearray(np.array(encodedImage)) + b'\r\n')
@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>
Solution
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'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + bytearray(np.array(encodedImage)) + b'\r\n')
Answered By - Mehul Purohit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.