【发布时间】:2014-11-27 23:05:36
【问题描述】:
我正在使用 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()
有什么想法吗?
【问题讨论】:
-
看来 cv2.imshow('frame', image) 工作不正常。
-
您忘记了 cv2.waitKey() 行。没有它就行不通。
-
真的吗? ... 为什么? ... cv2.waitKey() 不只是从键盘获取命令吗?
-
绝对不是。它包含窗口的消息循环并执行实际的 blitting
-
现在它打开了窗口,但它实际上是空的。我忘了什么吗?
标签: python opencv raspberry-pi