/
bccpsa.py
145 lines (118 loc) · 5.91 KB
/
bccpsa.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import gphoto2 as gp
import os
import time
from PIL import Image
from flask import Flask, request, redirect, url_for, render_template
from bccp_helper import ISO_CONSTANTS, SHUTTER_SPEED_CONSTANTS, APERTURE_CONSTANTS
app = Flask(__name__)
app.config.from_object(__name__)
context = gp.gp_context_new()
camera = gp.check_result(gp.gp_camera_new())
gp.check_result(gp.gp_camera_init(camera, context))
def get_config_value(key, camera, context):
config_widget = gp.check_result(gp.gp_camera_get_config(camera, context))
camera_widget_child = gp.check_result(gp.gp_widget_get_child_by_name(config_widget, key))
config_value = gp.check_result(gp.gp_widget_get_value(camera_widget_child))
return config_value
def set_config_value(key, value, camera, context):
config_widget = gp.check_result(gp.gp_camera_get_config(camera, context))
camera_widget_child = gp.check_result(gp.gp_widget_get_child_by_name(config_widget, key))
gp.check_result(gp.gp_widget_set_value(camera_widget_child, value))
gp.check_result(gp.gp_camera_set_config(camera, config_widget, context))
def _capture_image(camera, context, settings):
print "Capturing Image"
if settings:
set_iso(str(ISO_CONSTANTS[settings['iso']]), camera, context)
set_aperture(APERTURE_CONSTANTS[settings['f-number']], camera, context)
set_shutterspeed(SHUTTER_SPEED_CONSTANTS[settings["shutterspeed2"]], camera, context)
# Image being captured and stored on camera
image_path = gp.check_result(gp.gp_camera_capture(camera, gp.GP_CAPTURE_IMAGE, context))
# Grabbing timestamp for unique file name
timestamp = int(time.time())
extension = image_path.name.split('.')[1]
image_name = "TST_{}.".format(timestamp) + extension
target_path = os.path.join(os.getcwd()+'/images', image_name)
# "Grunt" work to move the file off camera
image_file = gp.check_result(gp.gp_camera_file_get(camera, image_path.folder, image_path.name, gp.GP_FILE_TYPE_NORMAL, context))
gp.check_result(gp.gp_file_save(image_file, target_path))
def _get_folder_contents(folder, camera, context):
result = []
folders = []
for name, value in camera.folder_list_files(folder, context):
result.append(os.path.join(folder, name))
for name, value in camera.folder_list_folders(folder, context):
folders.append(name)
if folders:
for name in folders:
result.extend(_get_folder_contents(os.path.join(folder, name),camera, context))
else:
print "No folders to print"
return result
@app.route('/')
def basic_info():
basic_info_data = dict()
basic_info_data['iso'] = get_iso(camera, context)
basic_info_data['shutterspeed'] = get_shutterspeed(camera, context)
basic_info_data['aperture'] = get_aperture(camera, context)
basic_info_data['battery'] = get_config_value('batterylevel', camera, context)
basic_info_data['model'] = get_config_value('cameramodel', camera, context)
basic_info_data['manufacturer'] = get_config_value('manufacturer', camera, context)
basic_info_data['serialnumber'] = get_config_value('serialnumber', camera, context)
basic_info_data['maximumshots'] = get_config_value('maximumshots', camera, context)
basic_info_data['flashmode'] = get_config_value('flashmode', camera, context)
return render_template('basic_info.html', basic_data=basic_info_data)
#return init_string
#gp.check_result(gp.gp_camera_exit(camera, context))
@app.route('/process_capture', methods=['POST'])
def process_capture_image():
basic_settings = {"iso": request.form['asa'], "shutterspeed2": request.form['shutterspeed'], "f-number": request.form['aperture']}
print basic_settings
print "Aperture: ", APERTURE_CONSTANTS[basic_settings['f-number']]
print "ISO: ", ISO_CONSTANTS[basic_settings['iso']]
print "SHUTTER: ", SHUTTER_SPEED_CONSTANTS[basic_settings["shutterspeed2"]]
print "Going to capture image..."
_capture_image(camera, context, basic_settings)
return redirect(url_for('captured_images'))
#return request.form['asa'] + "<br>" + request.form['shutterspeed'] + "<br>" + request.form['aperture']
@app.route('/captured_images')
def captured_images():
target_folder = os.getcwd()+'/images'
list_of_files = os.listdir(target_folder)
return render_template('captured_images.html', list_of_files=list_of_files)
@app.route('/images/<imagefile>')
def display_image(imagefile):
im = Image.open(os.getcwd()+'/images/'+str(imagefile))
im.show()
return redirect(url_for('captured_images'))
@app.route('/capture')
def capture():
iso = get_iso(camera, context)
fstop = get_aperture(camera, context)
shutterspeed = get_shutterspeed(camera, context)
return render_template('image_capture.html', iso_current=int(iso),
fstop_current=fstop,
shutterspeed_current=shutterspeed,
ISOS_SORTED=sorted(ISO_CONSTANTS, key=ISO_CONSTANTS.__getitem__),
ISOS=ISO_CONSTANTS,
FSTOPS=APERTURE_CONSTANTS,
SHUTTERS=SHUTTER_SPEED_CONSTANTS)
def get_iso(camera, context):
return get_config_value("iso", camera, context)
def set_iso(value, camera, context):
set_config_value("iso", value, camera, context)
def get_shutterspeed(camera, context):
return get_config_value("shutterspeed2", camera, context)
def set_shutterspeed(value, camera, context):
set_config_value("shutterspeed2", value, camera, context)
def get_aperture(camera, context):
return get_config_value("f-number", camera, context)
def set_aperture(value, camera, context):
set_config_value("f-number", value, camera, context)
@app.route('/folder')
def folder_contents():
print "In FOLDER"
folder = '/'
return render_template('show_folder_contents.html', files=_get_folder_contents(folder, camera, context))
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')
#sys.exit(main())