【问题标题】:VIDEOIO ERROR from opencv ONLY when streaming with flask仅在使用烧瓶流式传输时来自 opencv 的 VIDEOIO 错误
【发布时间】:2020-04-22 20:20:38
【问题描述】:

我知道这是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 之前,我的相机还对 open cv、绘制框、标签产生的 camera_frame 进行了一些 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)

[已解决]:下面为感兴趣的人提供答案。

【问题讨论】:

    标签: opencv flask python-3.7 raspberry-pi4


    【解决方案1】:

    对于那些遇到同样问题的人,我找出了导致它的原因。问题是我创建了一个这样的相机实例:

    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')
    

    任何对此有解释的机构都会很好!

    【讨论】:

      【解决方案2】:

      您似乎是从 https://blog.miguelgrinberg.com/post/video-streaming-with-flask 借用代码

      比较两者,您的 sn-p 在每一帧的末尾都有一个额外的\r\n'。尝试删除它。

      【讨论】:

      • 它来自不同的来源,但非常相似。感谢您的建议,但它没有解决它:/ Plus 不只是 /r/n 只是回车和换行符吗?仅供参考:代码在这里,完全一样github.com/behl1anmol/VideoStreamingFlask不确定谁借了谁:P
      • 是的,但是如果你多放一对,它就会从浏览器中弹出。
      • 嗨,谢谢,但无论如何,我修好了它,仍然没有工作:/
      猜你喜欢
      • 2015-03-05
      • 2018-10-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 2018-08-11
      • 2013-02-09
      • 2017-05-17
      • 2019-08-01
      相关资源
      最近更新 更多