forked from drumminhands/drumminhands_photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
49 lines (41 loc) · 1.75 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import config
from time import gmtime, strftime, sleep
import cv2
import time
import subprocess
import logging
def takePhoto():
photoTitle = config.FOLDER_PHOTOS_ORIGINAL + "photobooth_" + strftime("%Y-%m-%d_%H%M%S", gmtime()) + ".jpg"
camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1) # If you don't wait, the image will be dark
return_value, image = camera.read()
cv2.imwrite(photoTitle, image)
del(camera)
return photoTitle
def actuate_camera_shutter(img_size):
'''
Actuates the camera and downloads the image onto the Raspberry Pi
:return: the filepath of the photo taken
'''
image_name = "photobooth_" + strftime("%Y-%m-%d_%H%M%S", gmtime()) + ".jpg"
image_filepath = config.FOLDER_PHOTOS_ORIGINAL + image_name
gpout = ""
try:
gpout = subprocess.check_output("gphoto2 --set-config /main/imgsettings/imagesize=" + str(img_size), stderr=subprocess.STDOUT, shell=True)
gpout = subprocess.check_output("gphoto2 --capture-image-and-download --keep --filename " + image_filepath, stderr=subprocess.STDOUT, shell=True)
# CalledProcessError is raised when the camera is turned off (or battery dies?)
#if "ERROR" in gpout:
print(gpout)
# logging.error(gpout)
# raise IOError("Not able to take photo as the command failed for photo " + image_filepath)
except subprocess.CalledProcessError as e:
logging.error("Unable to take photo, likely due to camera not responding - check batteries")
logging.error(e)
raise
except Exception as e:
logging.error("Unable to take photo as the command failed for photo " + image_filepath)
logging.error(e)
raise
else:
return image_filepath