[英]How to broadcast Webcam Image stream to multiple devices using Opencv and Flask?
我有以下代碼。 嘗試將一台設備連接到服務器時,它可以正常工作。 當兩個設備連接時,只有一個設備工作,另一個凍結,並出現以下錯誤。
目標是將視頻流廣播到多個設備。 另外,有沒有辦法通過Flask改善傳輸的FPS並減少延遲?
碼
Camera.py
import cv2
class VideoCamera(object):
def __init__(self):
# Using OpenCV to capture from device 0. If you have trouble capturing
# from a webcam, comment the line below out and use a video file
# instead.
self.video = cv2.VideoCapture(0)
# If you decide to use video.mp4, you must have this file in the folder
# as the main.py.
# self.video = cv2.VideoCapture('video.mp4')
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
Main.py
import os
install_opencv = os.system("pip install flask opencv-python")
print("Install OPENCV-PYTHON: ", install_opencv)
from flask import Flask, render_template, Response
from camera import VideoCamera
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
frame = camera.get_frame()
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():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
錯誤
Traceback (most recent call last):
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgcodecs\src\grfmt_base.cpp:145: error: (-10:Unknown error code -10) Raw image encoder error: Empty JPEG image (DNL not supported) in function 'cv::BaseImageEncoder::throwOnEror'
127.0.0.1 - - [01/Nov/2018 16:16:21] "GET /video_feed HTTP/1.1" 200
我也這樣做了,無法訪問和兩個設備。 在github上有一個項目https://github.com/miguelgrinberg/flask-video-streaming或者你可以在開發者的網站上看到更好的解釋https://blog.miguelgrinberg.com/post/flask-video-streaming-重訪 #commentform
使用此代碼,我可以從多個設備訪問,但只允許顯示相機。 我正在工作,以便可以查看2個或更多相機,就好像它是DVR一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.