You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地托管视频流读取问题:OpenCV读取树莓派Flask流失败

解决OpenCV读取Flask MJPEG流失败的问题

问题场景

树莓派通过Flask将MJPEG视频推流到192.168.0.81:5000,同网络设备可正常访问该流,但使用OpenCV读取流时始终失败,尝试多种地址变体均报错。

测试代码

import cv2

vid = cv2.VideoCapture('192.168.0.81:5000')
result, image = vid.read()
cv2.imshow('',image)

报错信息

直接使用IP:端口时的错误

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

使用IP:端口/video_feed时的错误

(-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): 192.168.0.81:5000/ in function 'cv::icvExtractPattern'


解决步骤

1. 给流地址添加完整HTTP协议头

OpenCV的VideoCapture需要明确的协议标识,直接写IP端口会被当作本地文件处理,必须加上http://前缀,同时确保地址指向Flask推流的正确端点(通常是/video_feed)。

修正后的读取代码:

import cv2

# 替换为你的完整流地址,必须带http://前缀
stream_url = 'http://192.168.0.81:5000/video_feed'
vid = cv2.VideoCapture(stream_url)

# 循环读取帧,避免单次read失败的情况
while vid.isOpened():
    result, image = vid.read()
    if not result:
        print("无法读取帧,流可能已中断")
        break
    cv2.imshow('MJPEG Stream', image)
    # 按q键退出窗口
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放资源
vid.release()
cv2.destroyAllWindows()

2. 检查Flask推流的响应格式

确保你的Flask路由返回标准MJPEG流格式,示例推流代码如下(供对照排查):

from flask import Flask, Response
import cv2

app = Flask(__name__)
# 树莓派摄像头设备,通常为0
camera = cv2.VideoCapture(0)

def generate_frames():
    while True:
        success, frame = camera.read()
        if not success:
            break
        else:
            # 将帧编码为JPEG格式
            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            # 按MJPEG流标准格式返回
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(generate_frames(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    # 允许所有IP访问,关闭debug模式避免性能问题
    app.run(host='0.0.0.0', port=5000, debug=False)

如果Flask代码未设置正确的mimetype或帧编码错误,OpenCV将无法解析流。

3. 排查网络与权限问题

  • 确认运行OpenCV代码的设备与树莓派在同一局域网,能正常ping通192.168.0.81
  • 检查树莓派防火墙设置,确保允许5000端口的入站请求
  • 避免在Flask中开启debug模式,该模式会影响流的稳定性

内容的提问来源于stack exchange,提问作者Scottelli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 08:30:52