1

Part 1 : I am running a Python script using cv2 to save the video from a webcam to a 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()

I managed to do that if I know the name of the pendrive ("/media/pi/PSYCH"). But then I put the command in a bash file

sudo nano /etc/rc.local

and added

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

so that it executes on startup.

When I reboot then there still exists a

/media/pi/PSYCH

but is now not accessible and the pendrive is now at

/media/pi/PSYCH1

. Next reboot and it's at /media/pi/PSYCH2 and so on.

PS : I am using a Rasberry Pi 3 with Raspbian Jessie

  • 1
    SO Part 1: add your code, part 2: add a proper error description, part 3: one question at a time. – Klaus D. Aug 01 '17 at 20:37
  • Sorry for such a sloppy question framing. I'll edit it according to your suggestions. Will keep it in mind. –  Aug 01 '17 at 20:46
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 01 '17 at 20:49
  • If you run `mount`, you should get a list of the actually mounted drives. You could use that in Python or Bash to determine the name of your drive. – halfer Aug 01 '17 at 20:52
  • I did use mount, as "sudo mount -a" to mount everything before excuting my code in "/etc/rc.local". Let me edit my answer properly then maybe you could help me better. –  Aug 01 '17 at 20:55
  • No, I mean using `mount` to see what it is mounted, rather than to mount something. Your first part indicates that you have an already mounted drive, but you are having trouble addressing it (because the name keeps changing). – halfer Aug 01 '17 at 21:05
  • Tell me if I'm interpreting this right. you are asking me to write a shell script to see what all devices are mounted and to chose from the available options the pen-drive I wish to write in? but the code isn't working even after I made it independent of any externally mounted devices (refer part 3). –  Aug 01 '17 at 21:11
  • @KlausD. I have edited the question. I hope it is better now. Thanks for the suggestion. –  Aug 01 '17 at 21:38
  • Do you mount the pen drive from `/etc/fstab`? – Mark Setchell Aug 01 '17 at 21:51
  • I did not do that. I thought I could access the drive at "/media/pi/PHSYCH". I did try a "sudo mount -a" before the "sudo python TheCode.py" though. –  Aug 01 '17 at 21:55
  • I suspect the pen drive will come up at the same, constant place if you mount it from `/etc/fstab` – Mark Setchell Aug 01 '17 at 22:02
  • Is this ok (Step 5)? http://www.raspberrypi-spy.co.uk/2014/05/how-to-mount-a-usb-flash-disk-on-the-raspberry-pi/ –  Aug 01 '17 at 22:05
  • Yes, that's what I do. – Mark Setchell Aug 01 '17 at 22:19
  • Ok, thanks. I'll give it a try in the morning. –  Aug 01 '17 at 22:21
  • Note that I generally use options `defaults,noatime,nofail` in `/etc/fstab` for pendrives, so that Raspbian boots even if the drive is not inserted. – Mark Setchell Aug 01 '17 at 22:52

1 Answers1

0

Solution:

In the code I commented out any and all display interactions. I also commented the

key = cv2.waitKey(1)

I edited the command in rc.local to

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

Then I added a delay of 1 minute to the TheCode.py file using the library

import time

Then write a

time.delay(60)

This will give the pendrive and the camera time to mount, which was creating the PSYCH1 problem.