我正在使用樹莓來簡單地顯示視頻(就目前來說)。爲此,我必須使用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()
任何想法?
似乎cv2.imshow('框架',圖像)不能正常工作。 – giogix 2014-10-02 21:10:15
您忘記了cv2.waitKey()行。它不會沒有工作。 – berak 2014-10-03 06:30:10
真的嗎? ...爲什麼? ...是不是cv2.waitKey()只是從鍵盤獲取命令? – giogix 2014-10-03 07:55:05