2014-10-02 39 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