正尝试在视频流转换为OpenCV的从http地址

问题描述 投票:0回答:3

我是比较新的这一点,但这里是我想要做的事。我有连接到树莓派相机覆盆子零,我从树莓派通过无线流媒体uv4l视频。我使用这个命令:

须藤uv4l -f -k --sched-FIFO --mem锁--driver raspicam --auto-video_nr --encoding H264 --width 1080 --height 720 --enable的服务器上

我能够通过查看PI的IP地址上的Web浏览器访问该流。现在我想要做的是能够在OpenCV中观看视频流。这是我读过的作品,但是我遇到了以下错误:

Streaming http://192.168.1.84:8080/stream
Traceback (most recent call last):
  File "videoStream.py", line 17, in <module>
    bytes+=stream.read('1024')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 448, in read
    b = bytearray(amt)
TypeError: string argument without an encoding

这里是我的代码。请注意我运行的Python 3.5和OpenCV 3:

import cv2
import urllib.request
import numpy as np
import sys

host = "192.168.1.84:8080"
if len(sys.argv)>1:
    host = sys.argv[1]

hoststr = 'http://' + host + '/stream'
print('Streaming ' + hoststr)

stream=urllib.request.urlopen(hoststr)

bytes=''
while True:
    bytes+=stream.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:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
        cv2.imshow(hoststr,i)
        if cv2.waitKey(1) ==27:
            exit(0)

我不知道如何解决这个问题,或者可能有在OpenCV中查看该视频流的更好的方法。

python http opencv raspberry-pi video-streaming
3个回答
1
投票

尝试了这一点。更改

bytes=''
while True:
    bytes+=stream.read('1024')
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')

bytes=b''
while True:
    bytes+=stream.read(1024)
    a = bytes.find(b'\xff\xd8')
    b = bytes.find(b'\xff\xd9')

和使用

i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)

这为我工作在Python 3.5,CV2版本4.0.0


0
投票

我真的不能验证你的代码,因为我没有流设置和我做。但首先,我想stream.read('1024')应该是stream.read(1024)。该1024是缓冲器的以字节为单位的大小,而不是1024的字符串。

其次,urllib.request.openurl().read()返回一个字节的对象,所以你的代码可能以后有解码问题,当它击中线np.fromstring(jpg, dtype=np.uint8)作为np.fromstring期待jpg作为一个字符串,但jpg的类型是一个字节。您需要将其转换为一个像这样的字符串:

np.fromstring(jpg.decode('utf-8'), dtype=np.uint8)

0
投票

只需更换

bytes=''

bytes=bytearray()

bytes+=stream.read('1024')

bytes+=bytearray(stream.read(1024))
© www.soinside.com 2019 - 2024. All rights reserved.