使用 opencv+picamera 流 IO 捕获带有树莓派的视频 [英] Capturing video with raspberry using opencv+picamera stream IO
问题描述
我正在使用 Raspberry 来简单地显示视频(现在仅此而已).为此,我必须使用 opencv (cv2).我尝试了很多解决方案,但现在我想使用 Picamera 库捕获视频.我会告诉你我的代码:
I'm using the Raspberry to simply show a video (just this for now). To do this i have to use opencv (cv2). I tried many solution, but now i want to capture the video using the Picamera library. I'll show you my code:
import io
import time
import picamera
import cv2
import numpy as np
# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
while True:
camera.capture(stream, format='jpeg')
# Construct a numpy array from the stream
data = np.fromstring(stream.getvalue(), dtype=np.uint8)
# "Decode" the image from the array, preserving colour
image = cv2.imdecode(data, 1)
cv2.imshow('frame', image)
正如您所见,这真的很简单,但它不起作用.实际上它并没有打开窗户.我想重现下一个的行为,它完美地工作:
It's really simple as you can see, but it doesn't work. Actually it doesn't evev open the window. I would like to reproduce the behavior of the next one, which works perfectly:
#import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
有什么想法吗?
推荐答案
我遇到了类似的问题,摄像头输出正常,但视频流始终为黑色.原来是picamera版本问题.安装 1.10 对我有用,与演示代码没有任何其他偏差:
I was having a similar problem where the camera output was working but the video stream was always black. It turns out that it's a picamera version issue. Installing 1.10 worked for me without any other deviations from the demo code:
pip install 'picamera[array]'== 1.10
这篇关于使用 opencv+picamera 流 IO 捕获带有树莓派的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!