opencv - 如何配置while循环以使用picamera?

标签 opencv raspberry-pi raspberry-pi3

因此,我想使用while循环读取picamera的各个帧。这是我使用for循环执行此操作的发现:

 # 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
cv2.destroyAllWindows()

现在,当我使用上面的代码时,我得到了视频提要,但我打算使用while循环来做同样的事情。按照相同的逻辑,我添加了一个while循环,如下所示:
while True:  frame1=camera.capture_continious(rawCapture,format="bgr",use_video_port=True)
        image1 = frame1.array
        # show the frame
        cv2.imshow("Frame1", image1)
        # clear the stream in preparation for the next frame
        rawCapture.truncate(0)

但是我仍然遇到错误,因为frame1是生成器,并且在for循环中相同的代码运行良好时不包含此类属性。我可以进行哪些修改?

最佳答案

函数capture_continuous()返回从相机连续捕获的图像的无限迭代器。它不返回单个图像。这就是为什么它可以与for循环一起使用的原因。

在while循环中,应使用capture()函数,该函数会返回图像。

您可以(并且应该;))在documentation中阅读更多有关它的信息

关于opencv - 如何配置while循环以使用picamera?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53578043/

相关文章:

linux - ubuntu 上树莓派 pi3 的 libusb 交叉编译

bluetooth - 配置并运行蓝牙后运行脚本 - Raspberry Pi 3

android - 如何使用 OpenCV 从 C++ 程序制作 Android 应用程序

c++ - 使用 Opencv 绘制彩色图像的直方图

PHP - 在 Raspberry Pi 上打开文件或死亡

OpenCV 3.4 : OpenCL on RaspberryPi 4 GPU

linux - wget 无法在终端中与树莓派项目 pacman 一起使用

c++ - 黑屏视频采集opencv

python - OpenCv Python 颜色检测

linux - 从 vi/vim 中运行命令