我正在尝试使用opencv和python访问pi摄像机,我想捕获并录制视频。它可以正常直播,但不能保存在正确的文件中。文件已损坏。我是新手,所以请为我建议正确的语法。还可以帮助我编写代码,以便可以使用cv2.capturevideo()使视频变灰。预先感谢。
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import imutils
import os
# 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)
cap = cv2.VideoCapture(0)
#640,480
w = 640
h = 480
out = cv2.VideoWriter('outpy.mp4',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (640,480))
# 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
out.write(image)
# 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"):
cv2.destroyAllWindows()
break