【问题标题】:Capturing video with raspberry using opencv+picamera stream IO使用 opencv+picamera 流 IO 使用覆盆子捕获视频
【发布时间】: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


【解决方案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

【讨论】:

  • 我有一个非常奇怪的行为,你的第一个解决方案可以工作,而第二个解决方案只会在我的 Raspi 3 上产生黑框。有人知道这里可能出现什么问题吗?
【解决方案2】:

尝试:

sudo modprobe bcm2835-v4l2 

确保您拥有适用于 linux 驱动程序的视频

【讨论】:

    【解决方案3】:

    我遇到了类似的问题,即摄像头输出正常,但视频流始终为黑色。原来是picamera版本的问题。安装 1.10 对我有用,与演示代码没有任何其他偏差:

    pip install 'picamera[array]'== 1.10
    

    【讨论】:

      【解决方案4】:

      首先,需要将 cv2.waitKey() 添加到 cv2.imshow('frame', image) 的下面一行。然后,stream.seek(0);流.截断();需要添加到循环的末尾,否则图像不会改变。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-07
        • 2011-11-26
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-29
        相关资源
        最近更新 更多