8
8
9
9
from picamera2 import Picamera2
10
10
from picamera2 .encoders import JpegEncoder
11
+ from picamera2 .encoders import MJPEGEncoder
11
12
from picamera2 .outputs import FileOutput
12
13
from libcamera import Transform , controls
13
14
33
34
selected_resolution = capture_settings ["Resolution" ]
34
35
resolution = capture_settings ["available-resolutions" ][selected_resolution ]
35
36
print (capture_settings )
37
+ print (resolution )
36
38
37
39
# Get the sensor modes and pick from the the camera_config
38
40
camera_modes = picam2 .sensor_modes
39
41
mode = picam2 .sensor_modes [sensor_mode ]
40
42
41
43
# Create the video_config
42
- video_config = picam2 .create_video_configuration (main = {'size' : mode [ 'size' ] }, sensor = {'output_size' : mode ['size' ], 'bit_depth' : mode ['bit_depth' ]})
44
+ video_config = picam2 .create_video_configuration (main = {'size' :resolution }, sensor = {'output_size' : mode ['size' ], 'bit_depth' : mode ['bit_depth' ]})
43
45
print (video_config )
44
46
45
47
# Pull default settings and filter live_settings for anything picamera2 wont use (because the not all cameras use all settings)
@@ -137,7 +139,7 @@ def video_feed():
137
139
# Route to update settings to the buffer
138
140
@app .route ('/update_live_settings' , methods = ['POST' ])
139
141
def update_settings ():
140
- global live_settings , capture_settings , picam2 , video_config , resolution , sensor_mode
142
+ global live_settings , capture_settings , picam2 , video_config , resolution , sensor_mode , mode
141
143
try :
142
144
# Parse JSON data from the request
143
145
data = request .get_json ()
@@ -160,15 +162,21 @@ def update_settings():
160
162
capture_settings ['Resolution' ] = int (data [key ])
161
163
selected_resolution = int (data [key ])
162
164
resolution = capture_settings ["available-resolutions" ][selected_resolution ]
163
- return jsonify (success = True , message = "Settings updated successfully" , settings = live_settings )
165
+ stop_camera_stream ()
166
+ video_config = picam2 .create_video_configuration (main = {'size' :resolution }, sensor = {'output_size' : mode ['size' ], 'bit_depth' : mode ['bit_depth' ]})
167
+ start_camera_stream ()
168
+ return jsonify (success = True , message = "Settings updated successfully" , settings = capture_settings )
169
+ elif key in ('makeRaw' ):
170
+ capture_settings [key ] = data [key ]
171
+ return jsonify (success = True , message = "Settings updated successfully" , settings = capture_settings )
164
172
elif key == ('sensor_mode' ):
165
173
sensor_mode = int (data [key ])
166
174
mode = picam2 .sensor_modes [sensor_mode ]
167
175
stop_camera_stream ()
168
- video_config = picam2 .create_video_configuration (main = {'size' : mode [ 'size' ] }, sensor = {'output_size' : mode ['size' ], 'bit_depth' : mode ['bit_depth' ]})
176
+ video_config = picam2 .create_video_configuration (main = {'size' :resolution }, sensor = {'output_size' : mode ['size' ], 'bit_depth' : mode ['bit_depth' ]})
169
177
start_camera_stream ()
170
178
save_sensor_mode (sensor_mode )
171
- return jsonify (success = True , message = "Settings updated successfully" , settings = live_settings )
179
+ return jsonify (success = True , message = "Settings updated successfully" , settings = sensor_mode )
172
180
except Exception as e :
173
181
return jsonify (success = False , message = str (e ))
174
182
@@ -269,10 +277,7 @@ def save_sensor_mode(sensor_mode):
269
277
####################
270
278
271
279
def start_camera_stream ():
272
- global picam2 , output , video_config , still_config
273
- #video_config = picam2.create_video_configuration()
274
- # Flip Camera #################### Make configurable
275
- # video_config["transform"] = Transform(hflip=1, vflip=1)
280
+ global picam2 , output , video_config
276
281
picam2 .configure (video_config )
277
282
output = StreamingOutput ()
278
283
picam2 .start_recording (JpegEncoder (), FileOutput (output ))
@@ -298,17 +303,18 @@ def take_photo():
298
303
global picam2 , capture_settings
299
304
try :
300
305
timestamp = int (datetime .timestamp (datetime .now ()))
301
- image_name = f'pimage_{ timestamp } .jpg '
306
+ image_name = f'pimage_{ timestamp } '
302
307
filepath = os .path .join (app .config ['UPLOAD_FOLDER' ], image_name )
303
308
request = picam2 .capture_request ()
304
- request .save ("main" , filepath )
305
-
309
+ request .save ("main" , f'{ filepath } .jpg' )
310
+ if capture_settings ["makeRaw" ]:
311
+ request .save_dng (f'{ filepath } .dng' )
306
312
request .release ()
307
- selected_resolution = capture_settings ["Resolution" ]
308
- resolution = capture_settings ["available-resolutions" ][selected_resolution ]
309
- original_image = Image .open (filepath )
310
- resized_image = original_image .resize (resolution )
311
- resized_image .save (filepath )
313
+ # selected_resolution = capture_settings["Resolution"]
314
+ # resolution = capture_settings["available-resolutions"][selected_resolution]
315
+ # original_image = Image.open(filepath)
316
+ # resized_image = original_image.resize(resolution)
317
+ # resized_image.save(filepath)
312
318
logging .info (f"Image captured successfully. Path: { filepath } " )
313
319
except Exception as e :
314
320
logging .error (f"Error capturing image: { e } " )
@@ -337,24 +343,31 @@ def restart_configure_camera(restart_settings):
337
343
# Image Gallery Functions
338
344
####################
339
345
346
+ from datetime import datetime
347
+ import os
348
+
340
349
@app .route ('/image_gallery' )
341
350
def image_gallery ():
342
351
try :
343
- image_files = [f for f in os .listdir (UPLOAD_FOLDER ) if f .endswith (('.jpg' , '.jpeg' , '.png' , '.gif' ))]
352
+ image_files = [f for f in os .listdir (UPLOAD_FOLDER ) if f .endswith (('.jpg' ))]
344
353
345
354
if not image_files :
346
355
# Handle the case where there are no files
347
356
return render_template ('no_files.html' )
348
357
349
- # Create a list of dictionaries containing file name and timestamp
358
+ # Create a list of dictionaries containing file name, timestamp, and dng presence
350
359
files_and_timestamps = []
351
360
for image_file in image_files :
352
361
# Extracting Unix timestamp from the filename
353
362
unix_timestamp = int (image_file .split ('_' )[- 1 ].split ('.' )[0 ])
354
363
timestamp = datetime .utcfromtimestamp (unix_timestamp ).strftime ('%Y-%m-%d %H:%M:%S' )
355
364
365
+ # Check if corresponding .dng file exists
366
+ dng_file = os .path .splitext (image_file )[0 ] + '.dng'
367
+ has_dng = os .path .exists (os .path .join (UPLOAD_FOLDER , dng_file ))
368
+
356
369
# Appending dictionary to the list
357
- files_and_timestamps .append ({'filename' : image_file , 'timestamp' : timestamp })
370
+ files_and_timestamps .append ({'filename' : image_file , 'timestamp' : timestamp , 'has_dng' : has_dng , 'dng_file' : dng_file })
358
371
359
372
# Sorting the list based on Unix timestamp
360
373
files_and_timestamps .sort (key = lambda x : x ['timestamp' ], reverse = True )
@@ -364,6 +377,7 @@ def image_gallery():
364
377
logging .error (f"Error loading image gallery: { e } " )
365
378
return render_template ('error.html' , error = str (e ))
366
379
380
+
367
381
@app .route ('/delete_image/<filename>' , methods = ['DELETE' ])
368
382
def delete_image (filename ):
369
383
try :
0 commit comments