Automation of video recording on booting of raspberry pi3

落花浮王杯 提交于 2019-12-07 07:56:30

问题


I have written a python code which contains some opencv code to play around with my webcam. I have attached it to raspi 3. I want that on startup(booting), it start recording video automatically.. I am using crontab for this.

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 is the name of my python file

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

@reboot path to my bash file

After rebooting two avi files are generated in the same directory but the video is not recorded, the webcam doesn't runs, which however runs perfectly when I myself execute this bash file to run the python file.

As Suggested in comments, I've created a log file, it shows The error log shows (Video Stream:542): Gtk-Warning **: cannot open display:


回答1:


As suggested by Mark Setchell in the comments, it worked correctly after removing imshow(), waitkey().




回答2:


If two files are generated that means crontab works fine. it also means that video not generated due to wrong format spelling .avi change it to .AVI and try once

fourcc = cv2.cv.CV_FOURCC('D', 'I', 'V', 'X')
videoOut = cv2.VideoWriter('output1.AVI', fourcc, 20, (640, 480), 1)
videoOut2 = cv2.VideoWriter('output2.AVI', fourcc, 20, (640, 480), 1)


来源:https://stackoverflow.com/questions/45447020/automation-of-video-recording-on-booting-of-raspberry-pi3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!