使用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屋!