Skip to content

Instantly share code, notes, and snippets.

@rasathus
Last active December 15, 2015 11:29
Show Gist options
  • Save rasathus/5253470 to your computer and use it in GitHub Desktop.
Save rasathus/5253470 to your computer and use it in GitHub Desktop.
class Capture_Upload:
def __init__(self, controlmypi):
self.image_id = 0
self.controlmypi = controlmypi
self.storage_bucket = 'image-store'
self.uri = boto.storage_uri(self.storage_bucket, 'gs')
self.temp_dir = tempfile.mkdtemp(prefix='googlestorage')
temp_uuid = str(uuid4())
self.entr = temp_uuid[0:8]
pygame.init()
pygame.camera.init()
self.cam = pygame.camera.Camera('/dev/video0', (640, 480), 'RGB')
self.cam.start()
def say_cheese(self):
filename = "capture-%s-%d.jpeg" % (self.entr, self.image_id)
self.image_id += 1
# for some reason the cam is always two pics behind, so discard the first two.
# Suspect its a bug with the pygame as Jeremey independantly found the same issue.
surf = self.cam.get_image()
surf = self.cam.get_image()
surf = self.cam.get_image()
pygame.image.save(surf, os.path.join(self.temp_dir, filename))
# upload our image
dst_uri = boto.storage_uri(
self.storage_bucket + '/' + filename, 'gs')
image_data = file(os.path.join(self.temp_dir, filename), 'r')
dst_uri.new_key().set_contents_from_file(image_data)
image_data.close()
uri = boto.storage_uri(self.storage_bucket + '/' + filename, 'gs')
#print str(uri.get_acl())
uri.set_canned_acl('public-read')
#print str(uri.get_acl())
logging.info("Image should be available @ http://image-store.storage.googleapis.com/%s , updating controlmypi.com" % filename)
self.controlmypi.update_status({'box_image': "http://image-store.storage.googleapis.com/%s" % filename})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment