Python 使用opencv+;皮卡梅拉河
我用覆盆子来简单地播放一段视频(现在就看这个)。为此,我必须使用opencv(cv2)。我尝试了许多解决方案,但现在我想使用Picamera库捕获视频。 我将向您展示我的代码:Python 使用opencv+;皮卡梅拉河,python,opencv,raspberry-pi,Python,Opencv,Raspberry Pi,我用覆盆子来简单地播放一段视频(现在就看这个)。为此,我必须使用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:
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()
有什么想法吗?快去看看。它具有使其工作的代码:
# 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
首先,需要将cv2.waitKey()添加到cv2.imshow的下一行('frame',image)。然后,stream.seek(0);stream.truncate();需要添加到循环的末尾,否则图像将不会更改。尝试:
sudo modprobe bcm2835-v4l2
为了确保您拥有linux驱动程序的视频,我遇到了一个类似的问题,摄像头输出正常,但视频流始终为黑色。事实证明这是一个picamera版本的问题。安装1.10适用于我,与演示代码没有任何其他偏差:
pip install 'picamera[array]'== 1.10
似乎cv2.imshow('frame',image)工作不正常。您忘记了cv2.waitKey()行。没有它就不行。真的吗。。。为什么?cv2.waitKey()不就是从键盘上获取命令吗?绝对不是。它包含窗口的messageloop,并执行实际的BlitingNow打开窗口,但实际上是空的。我忘了什么吗?我有一个非常奇怪的行为,你的第一个解决方案是可行的,而第二个解决方案只会在我的Raspi 3上产生黑框。有人知道这里有什么问题吗?