2014-10-02 75 views
2

我正在使用树莓来简单地显示视频(就目前来说)。为此,我必须使用opencv(cv2)。我尝试了很多解决方案,但现在我想使用Picamera库捕获视频。 我会告诉你我的代码:使用opencv + picamera stream捕获视频与覆盆子IO

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

似乎cv2.imshow('框架',图像)不能正常工作。 – giogix 2014-10-02 21:10:15

+0

您忘记了cv2.waitKey()行。它不会没有工作。 – berak 2014-10-03 06:30:10

+0

真的吗? ...为什么? ...是不是cv2.waitKey()只是从键盘获取命令? – giogix 2014-10-03 07:55:05

回答

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

我有一个非常奇怪的行为,你的第一个解决方案将工作,第二个只会产生黑色框架pn我的Raspi 3.任何人有什么想法可以在这里的问题? – Dominic 2016-08-05 06:16:15

0

首先,需要将cv2.waitKey()添加到cv2.imshow('frame',image)的以下行中。然后,stream.seek(0); stream.truncate();需要添加到循环的结尾,否则图像不会改变。

1

尝试:

sudo modprobe bcm2835-v4l2 

,以确保你有Linux驱动程序的视频

0

我有一个类似的问题,其中摄像机输出是工作,但视频流总是黑色。事实证明,这是一个picamera版本问题。安装1.10为我工作没有任何其他偏离演示代码:

pip install 'picamera[array]'== 1.10