仅当使用烧瓶流式传输时,来自 opencv 的视频错误
VIDEOIO ERROR from opencv ONLY when streaming with flask
我知道这是opencv的错误,我知道Flask和opencv没有关系。但是,请坚持到底。
仅当我流式传输 CV 帧时,我才收到这个非常奇怪的错误:
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy
我的代码:
// my camera_detector class does some AI works on the camera frame, other than that nothing special
camera = camera_detector(my_arguments)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
print('getting frame')
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('/feed')
def video_feed():
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
现在这就是为什么我说这只发生在 Flask 上,如果我像这样抓住相机框架:
while True:
frame = camera.get_frame()
无需使用烧瓶,一切运行良好o_0
如果有什么不同,我在 pi4 上使用 python3.7。在将帧返回到 Flask 之前,我的相机还对 camera_frame 由打开的 cv、绘制框、标签生成的 AI 进行了一些处理:
def get_frame(self):
ret, frame = self.camera.read()
# processing, does AI works, draw boxes and labels
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
[编辑] 相机信息如果有帮助:
{20-04-22 15:39}raspberrypi:~/detect pi% v4l2-ctl -d /dev/video0 --list-formats
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'YUYV' (YUYV 4:2:2)
[1]: 'MJPG' (Motion-JPEG, compressed)
[已解决]:有兴趣的人可以在下面找到答案。
您似乎是从 https://blog.miguelgrinberg.com/post/video-streaming-with-flask
借用代码
比较两者,您的代码段在每一帧的末尾都有一个额外的 \r\n'
。尝试删除它。
对于那些遇到同样问题的人,我找出了导致它的原因。问题是我创建了一个像这样的相机实例:
camera = camera_detector(my_arguments)
然后将其传递到我的路由函数中:
@app.route('/feed')
def video_feed():
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
原来 opencv 不太喜欢那样。我觉得这很奇怪,但是我把它改成:
@app.route('/feed')
def video_feed():
return Response(gen(camera_detector(my_arguments)), mimetype='multipart/x-mixed-replace; boundary=frame')
任何对此有解释的人都会很好!
我知道这是opencv的错误,我知道Flask和opencv没有关系。但是,请坚持到底。 仅当我流式传输 CV 帧时,我才收到这个非常奇怪的错误:
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
Unable to stop the stream: Device or resource busy
我的代码:
// my camera_detector class does some AI works on the camera frame, other than that nothing special
camera = camera_detector(my_arguments)
@app.route('/')
def index():
return render_template('index.html')
def gen(camera):
while True:
print('getting frame')
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('/feed')
def video_feed():
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
现在这就是为什么我说这只发生在 Flask 上,如果我像这样抓住相机框架:
while True:
frame = camera.get_frame()
无需使用烧瓶,一切运行良好o_0
如果有什么不同,我在 pi4 上使用 python3.7。在将帧返回到 Flask 之前,我的相机还对 camera_frame 由打开的 cv、绘制框、标签生成的 AI 进行了一些处理:
def get_frame(self):
ret, frame = self.camera.read()
# processing, does AI works, draw boxes and labels
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()
[编辑] 相机信息如果有帮助:
{20-04-22 15:39}raspberrypi:~/detect pi% v4l2-ctl -d /dev/video0 --list-formats
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'YUYV' (YUYV 4:2:2)
[1]: 'MJPG' (Motion-JPEG, compressed)
[已解决]:有兴趣的人可以在下面找到答案。
您似乎是从 https://blog.miguelgrinberg.com/post/video-streaming-with-flask
借用代码比较两者,您的代码段在每一帧的末尾都有一个额外的 \r\n'
。尝试删除它。
对于那些遇到同样问题的人,我找出了导致它的原因。问题是我创建了一个像这样的相机实例:
camera = camera_detector(my_arguments)
然后将其传递到我的路由函数中:
@app.route('/feed')
def video_feed():
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
原来 opencv 不太喜欢那样。我觉得这很奇怪,但是我把它改成:
@app.route('/feed')
def video_feed():
return Response(gen(camera_detector(my_arguments)), mimetype='multipart/x-mixed-replace; boundary=frame')
任何对此有解释的人都会很好!