我正在使用Raspberry来简单地显示一个视频(现在就是这个)。要做到这一点,我必须使用opencv(cv2)。我尝试了很多解决方案,但现在我想使用Picamera库捕获视频。 我会告诉你我的代码:
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)
你可以看到它非常简单,但它并不起作用。实际上它并没有打开窗户。 我想重现下一个的行为,它完美地运作:
#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()
有什么想法吗?
答案 0 :(得分:1)
结帐this blog posting。它有代码来实现这个目的:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)
# allow the camera to warmup
time.sleep(0.1)
# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)
还有一个例子是不断捕捉图像。
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
答案 1 :(得分:1)
尝试:
sudo modprobe bcm2835-v4l2
确保您拥有适用于Linux驱动程序的视频
答案 2 :(得分:1)
我遇到类似的问题,相机输出正在工作,但视频流始终是黑色的。事实证明,这是一个picamera版本问题。安装1.10对我有用,没有任何其他偏离演示代码:
pip install 'picamera[array]'== 1.10
答案 3 :(得分:0)
首先,需要将cv2.waitKey()添加到cv2.imshow的以下行(' frame',image)。然后,stream.seek(0); stream.truncate();需要添加到循环的末尾,否则图像不会改变。