首页 > 解决方案 > cv2.VideoCapture(url) 直播不工作 Ubuntu

问题描述

预期结果:

我想在直播中进行对象检测。

我的代码:


    import cv2
    cap=cv2.VideoCapture()
    url='http://192.168.10.1/media/?action=stream'
    cap.open(url)

返回错误:

int() argument must be a string, a bytes-like object or a number, not 'NoneType'

我能做些什么?我已经尝试了一切

标签: pythonopencvtensorflowobject-detectionvideo-capture

解决方案


根据Opencv , cap.open () ,打开视频文件或捕获设备或IP视频流进行视频捕获。我希望下面的代码将使用 numpy 和 requests 库解决问题。

import requests
import cv2
import numpy as np  
url = ('http://192.168.10.1/media/?action=stream')
stream = requests.get(url, stream=True)  
bytes=''  
while(True):
    bytes+=stream.raw.read(1024)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
        cv2.imshow('Live',img)
        if cv2.waitKey(1) ==27:
            exit(0)

推荐阅读