python – 启动覆盆子pi3时视频录制的自动化

我编写了一个
python代码,其中包含一些opencv代码,可以使用我的网络摄像头.我已将它附加到raspi 3.我想在启动时启动(启动),它会自动开始录制视频..

我正在使用crontab.

My Python code:

import cv

if __name__ == "__main__":
# find the webcam
capture = cv2.VideoCapture(0)
capture1 = capture
# video recorder
fourcc = cv2.cv.CV_FOURCC(*'XVID')  #cv2.VideoWriter_fourcc() does not exist
videoOut = cv2.VideoWriter('out1.avi', fourcc, 10.0, (640, 480))
videoOut1 = cv2.VideoWriter('out2.avi', fourcc, 10.0, (640, 480))
# record video
while (capture.isOpened() and capture1.isOpened()):
    ret, frame = capture.read()
    ret1, frame1 = capture1.read()
    if ret:
        videoOut.write(frame)
        cv2.imshow('Video Stream', frame)



    else:
        break
    if ret1:
        frame1 = cv2.flip(frame1,1)
        videoOut1.write(frame1)
        cv2.imshow('Video Stream1', frame1)

    else:
        break

    # Tiny Pause
    key = cv2.waitKey(1)

capture1.release()
videoOut1.release()
capture.release()
videoOut.release()
cv2.destroyAllWindows()

Then I made a bash script which looks like this,

cd /
cd absolute path to my python file directory
sudo python cam22.py
cd /

cam22.py是我的python文件的名称

Then I used contrab to run this bash script at runtime by writing

@reboot path to my bash file

重新启动后,在同一目录中生成了两个avi文件,但视频没有被录制,但网络摄像头没有运行,但是当我自己执行这个bash文件来运行python文件时,它运行得很好.

作为评论中的建议,我创建了一个日志文件,它显示错误日志显示(视频流:542):Gtk-Warning **:无法打开显示:

最佳答案 正如Mark Setchell在评论中所建议的那样,删除imshow(),waitkey()后它可以正常工作.

点赞