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)