-
Notifications
You must be signed in to change notification settings - Fork 0
/
Backend.py
94 lines (75 loc) · 2.88 KB
/
Backend.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import logging
import os
import shutil
from os.path import expanduser
import gphoto2
import shortuuid
from flask import Flask, send_from_directory
from gphoto2 import gp_camera_exit, gp_context_new, gp_camera_init, \
gp_camera_new, gp_camera_get_summary, GPhoto2Error
from FileNumber import get_and_increase_number
from Thumbnail import create_thumb
from Utils import convert_bytes
THUMBNAIL_PREFIXX = "thumb/"
app = Flask(__name__)
logging.root.setLevel(logging.DEBUG)
logging.getLogger("gphoto2").disabled = True
@app.route('/photo/<photo_id>')
def get_photo(photo_id):
logging.info("Requested photo with id " + photo_id)
path = expanduser("~") + "/booth/"
filename = photo_id + ".jpg"
file_size = os.path.getsize(path + filename)
logging.debug("File size: " + convert_bytes(file_size))
return send_from_directory(path, filename)
@app.route('/thumb/<photo_id>')
def get_thumb(photo_id):
logging.info("Requested photo with id " + photo_id)
path = expanduser("~") + "/booth/"
filename = photo_id + ".jpg"
file_size = os.path.getsize(path + filename)
logging.debug("File size original: " + convert_bytes(file_size))
thumbnail_path = path + THUMBNAIL_PREFIXX + "t" + filename
if not os.path.exists(thumbnail_path):
create_thumb(path + filename, thumbnail_path)
else:
logging.debug("Thumb already exists " + thumbnail_path)
return send_from_directory(path + THUMBNAIL_PREFIXX, "t" + filename)
@app.route('/info')
def info():
context = gp_context_new()
error, camera = gp_camera_new()
error = gp_camera_init(camera, context)
error, text = gp_camera_get_summary(camera, context)
error = gp_camera_exit(camera, context)
return text.text
@app.route('/capture')
def capture():
try:
path = capture_photo()
except GPhoto2Error as error:
error_message = "GPhoto Error: " + str(error)
logging.error(error_message)
return error_message, 500
exists = os.path.exists(path)
is_file = os.path.isfile(path)
# name = str(datetime.datetime.now()).split('.')[0]
name = get_and_increase_number() + "-" + shortuuid.uuid()
new_path = expanduser("~") + "/booth/" + str(name) + ".jpg"
if exists and is_file:
shutil.move(path, new_path)
return name
def capture_photo():
callback_obj = gphoto2.check_result(gphoto2.use_python_logging())
camera = gphoto2.Camera()
camera.init()
logging.debug('Capturing image')
file_path = camera.capture(gphoto2.GP_CAPTURE_IMAGE)
logging.debug('Camera file path: ${0}/${1}'.format(file_path.folder, file_path.name))
target = os.path.join('/tmp', file_path.name)
logging.info('Copying image to ' + target)
camera_file = camera.file_get(file_path.folder, file_path.name, gphoto2.GP_FILE_TYPE_NORMAL)
camera_file.save(target)
return target
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000)