我正在尝试从 picamera(RaspberryPi) 捕获图像并使用 Flask(在网络上)显示它,但我遇到了这个问题:
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
TypeError:cannot concatenate 'str' and 'numpy.ndarray' objects
这是代码:
stream = io.BytesIO()
with picamera.PiCamera() as camera2:
camera2.start_preview()
time.sleep(0.1)
camera2.capture(stream, format='jpeg')
data = np.fromstring(stream.getvalue(), dtype=np.uint8)
frame = cv2.imdecode(data, 1)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
虽然我已阅读此链接,但它对我没有帮助: https://blog.miguelgrinberg.com/post/video-streaming-with-flask
输出:
data = np.fromstring(stream.getvalue(), dtype=np.uint8)
[255 216 255 ..., 175 255 217]
还有
frame = cv2.imdecode(data, 1)
[[[120 125 104] [120 125 104] [120 124 105] ...
任何帮助将不胜感激...
谢谢
最佳答案
将字节字符串 b'Content-Type: image/jpeg\r\n\r\n'
与 numpy 数组 frames
连接时遇到麻烦。在示例中,frames
使用字节字符串,它直接以二进制模式从 jpeg 文件读取。
我认为你不能对stream.getvalue()
做任何事情。它返回您准备好的流媒体数据结构(以字节表示的 jpeg 文件)。所以就用它吧
stream = io.BytesIO()
with picamera.PiCamera() as camera2:
camera2.start_preview()
time.sleep(0.1)
camera2.capture(stream, format='jpeg')
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + stream.getvalue() + b'\r\n')
关于python - Flask - 图像问题 - 不能 str 和 numpy.ndarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52570052/