如何配置 while 循环以使用 picamera?
How do I configure while loop to use picamera?
所以我想使用 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() return 是从相机连续捕获的图像的无限迭代器。它不是 return 单个图像。这就是为什么它确实适用于 for 循环。
在您的 while 循环中,您应该使用 capture() 函数,它 return 图像。
您可以(并且应该 ;) ) 在此 documentation
中阅读更多相关信息
所以我想使用 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() return 是从相机连续捕获的图像的无限迭代器。它不是 return 单个图像。这就是为什么它确实适用于 for 循环。
在您的 while 循环中,您应该使用 capture() 函数,它 return 图像。
您可以(并且应该 ;) ) 在此 documentation
中阅读更多相关信息