我已经设置了一个IP摄像机,并且可以浏览到其IP并获取MJPEG流,我试图逐帧下载它,以便可以在另一台服务器上分析图像。但是我在阅读流时遇到了麻烦。我的代码是:
import cv2
import urllib.request
import numpy as np
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
stream = urllib.request.urlopen('https://x.x.x.x:8602/Interface/Cameras/GetJPEGStream?Camera=Bosch%20NBE6502AL%20Bullet&ResponseFormat=XML&AuthUser=username&AuthPass=password',context=ctx)
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('i', i)
if cv2.waitKey(1) == 27:
exit(0)
它给了我以下错误:
Traceback (most recent call last):
File "mjpeg.py", line 15, in <module>
bytes += stream.read(1024)
TypeError: can only concatenate str (not "bytes") to str
我认为流会输出字符串,但是如何将流转换为字节并保存文件呢?
亲切的问候,
埃伦斯
最佳答案
整个过程中,您都需要bytes
y,因此将bytes
(这也是最好的名称,因为它也是内置类型的名称)初始化为一个字节字符串,并使用字节常量来查找开始/结束标记:
bytes = b''
while True:
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
# ...
关于python - 逐帧下载MJPEG流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53669951/