limpetcam
Posts: 3
Joined: Thu Aug 01, 2013 8:07 pm

Camera freezes when using pygame.camera.Camera.start()

Thu Aug 01, 2013 8:20 pm

Hello all,

I am a marine biologist hoping to use the raspberry pi as part of a field experiment. Right now, I am trying to write a python program on the pi that activates a webcam (Microsoft LifeCam), takes a picture, and sends that picture to me via dropbox once a minute.

My code works perfectly when I use one webcam, but when I remove that one and connect a new (but identical) one to the same port, the program won't run. The webcam mounts successfully, and camera.start() appears to work, but the whole thing gets hung up on camera.get_image(). No error messages are thrown, but nothing else happens, either. My Ctrl+C won't halt the program, and I have to go into Task Manager to close IDLE and start over again.

I tried changing the camera from /dev/video0 to /dev/video1. This caused the program to throw an error upon mounting the camera, which basically said that /dev/video1 didn't exist.

I am a moderately experienced python user, but am brand new with the raspberry pi. I've also never tried something like this before. If you have any thoughts or suggestions, I'd really appreciate it! My code is below. The problematic section is in the last block.

Code: Select all

import pygame, sys
from pygame.locals import *
from datetime import datetime
import pygame.camera
import time
import dropbox

# XXX Fill in your consumer key and secret below
APP_KEY = 'xpzr9nn6hz4shch'
APP_SECRET = 'yrmmefhni7apsxy'
##flow = dropbox.client.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
ACCESS_TYPE = 'app_folder'

# initialize dropbox session
sess = dropbox.session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
request_token = sess.obtain_request_token()

# make the user log in and authorize this token
url = sess.build_authorize_url(request_token)
sys.stdout.write("1. Go to: %s\n" % url)
sys.stdout.write("2. Authorize this app.\n")
sys.stdout.write("After you're done, press ENTER.\n")
raw_input()

# This will fail if the user didn't visit the above URL and hit "Allow"
access_token = sess.obtain_access_token(request_token)
dBoxClient = dropbox.client.DropboxClient(sess)
account_info = dBoxClient.account_info()
sys.stdout.write("Link successful. %s is uid %s\n" % (account_info['display_name'], account_info['uid']))

#initialize camera
pygame.init()
pygame.camera.init()
#set image resolution
width = 1280
height = 720

pic_root = "/home/pi/Image_Files/"

print "Beginning 1 minute camera cycles now."

counter = 0
minutes = time.localtime()[4]
# time.localtime() gives a list of all the local time data
# the 4th item in the list is minutes (remember, indexing starts at 0)

while True:
    #the pi automatically mounts the camera on /dev/video0
    cam = pygame.camera.Camera("/dev/video0",(width,height))
    print 'camera mounted'
    min, sec = time.localtime()[4:6]
    # queries the computer for the local time ten times per second
    if sec == 0 and minutes != min:
        minutes = min
        cam.start()
        #starts camera and captures an image
        print 'camera on'
        time.sleep(5)
        print 'pause over'
        image = cam.get_image()
### THIS IS WHERE THE PRINT STATEMENTS STOP APPEARING AND EVERYTHING FREEZES ###
        print 'image captured'
        cam.stop()
        print 'camera off'
        #give the current timestamp as the filename
        filename = datetime.now().strftime("%Y_%m_%d_%H_%M_%S") +'.jpg'
        filepath = pic_root+filename
        #first save the file on the Pi's disk
        pygame.image.save(image, filepath)
        time.sleep(10)
        #PUT file into DropBox
        dBoxClient.put_file("/"+filename,open(filepath))
        #repeat every minute
        counter += 1
        print 'Picture ', counter, ' completed.'
        time.sleep(33)

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Camera freezes when using pygame.camera.Camera.start()

Fri Aug 02, 2013 1:55 pm

Are you swapping the camera when the script is running? And have you another way of checking that hot-swapping the camera actually works?

Dave.
Apple say... Monkey do !!

limpetcam
Posts: 3
Joined: Thu Aug 01, 2013 8:07 pm

Re: Camera freezes when using pygame.camera.Camera.start()

Fri Aug 02, 2013 3:39 pm

Hi Dave,

No, I have not been swapping the camera while the code is running. I stop the code, swap the camera, and restart it from the top, at which point it freezes up.

Thanks!

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Camera freezes when using pygame.camera.Camera.start()

Fri Aug 02, 2013 5:11 pm

It doesn't sound like an issue with PyGame so I can only suggest it's due to hot-swapping the camera. Maybe try some other package like Python Imaging Library or OpenCV to see if the problem remains or have both cameras connected through a USB hub and swap them in your code.

Dave.
Apple say... Monkey do !!

limpetcam
Posts: 3
Joined: Thu Aug 01, 2013 8:07 pm

Re: Camera freezes when using pygame.camera.Camera.start()

Sat Aug 03, 2013 12:05 am

I'm not sure what you mean by hot-swapping, but it looks like the pi isn't having trouble recognizing the cameras. If I have both plugged in, they register as "dev/video0" and "dev/video1." If I remove one, only "dev/video0" continues to be listed. If I use the command camera.list() in my python program, the program reacts the same way to adding or removing cameras. But it still sticks on get_image() - no image is taken, and the program stops responding to me in any way.

Thanks for the thoughts!!!

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Camera freezes when using pygame.camera.Camera.start()

Sat Aug 03, 2013 12:53 pm

Clutching at straws a bit here but it may be worth catching PyGame events in your main loop and using pygame.quit() to exit cleanly instead of relying on CTRL-C. This way all the PyGame modules are uninitialized properly.
And hot-swapping is plugging/unplugging peripherals without powering down or ejecting them first.

Dave.
Apple say... Monkey do !!

Return to “Python”