I can’t seem to be able to stream a video using my webcam on the website using python
@app.route("/Record" , methods=['GET', 'POST'])
def Record():
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
vc.release()
cv2.destroyWindow("preview")
return render_template('record.html')
I have tried these codes that i have found online but it only allows the video to be streamed by opening a new window. What should I do?
2
Answers
You can modify you API as following,
Here, I have used
yield
for every frame and used mimetypemultipart/x-mixed-replace; boundary=frame
to return the response. Now, you can invoke this API from wherever you want and it will return you video frames. You can design separate HTML page to handle it or by directly open in chrome will also work.You can achieve this by using a web framework like Flask for the server-side and a combination of HTML, JavaScript, and perhaps a library like Flask-SocketIO for real-time communication.