Raspberry Pi 如何在启动时启动网络摄像头并存储在闪存驱动器中

How to start webcam and store in a flash drive on boot time in Raspberry Pi

第 1 部分: 我是 运行 一个 Python 使用 cv2 将视频从网络摄像头保存到 pendrive 的脚本。

import cv2,os

dypa = ('/media/pi/PSYCH') #specify the absolute output path here
fnam1 = 'output.avi' #specify the output file name here
fnam2 = 'output1.avi'
dypa1 = os.path.join(dypa,fnam1)
dypa2 = os.path.join(dypa, fnam2)

if __name__ == "__main__":

    # find the webcam
    capture = cv2.VideoCapture(0)
    capture1 = capture
    # video recorder
    fourcc = cv2.cv.CV_FOURCC(*'XVID')
    videoOut = cv2.VideoWriter(dypa1, fourcc, 10.0, (640, 480))
    videoOut1 = cv2.VideoWriter(dypa2, 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)
        else:
            break
        if ret1:
            frame1 = cv2.flip(frame1,1)
            videoOut1.write(frame1)
        else:
            break

    # Tiny Pause
    key = cv2.waitKey(1)

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

如果我知道 pendrive 的名称 ("/media/pi/PSYCH"),我就设法做到了。但后来我把命令放在 bash 文件中

sudo nano /etc/rc.local

并添加

sudo python /home/pi/Desktop/TheCode.py

以便它在启动时执行。

当我重新启动时,仍然存在

/media/pi/PSYCH

但现在无法访问并且 pendrive 现在位于

/media/pi/PSYCH1

。下次重新启动,它位于 /media/pi/PSYCH2 等等。

PS : 我正在使用 Rasberry Pi 3 和 Raspbian Jessie

解决方案:

在代码中,我注释掉了所有显示交互。 我还评论了

key = cv2.waitKey(1)

我将rc.local中的命令编辑为

sudo python /home/pi/Desktop/TheCode.py &

然后我使用库

TheCode.py文件添加了1分钟的延迟
import time

然后写一个

time.delay(60)

这将使 pendrive 和相机有时间安装,这会产生 PSYCH1 问题。