def take_timelapse(): """Function to take a timelapse with the Rpi camera Returns: dict: text=a message to display to the User; status=if something went wrong """ try: exposure_photo = int(float(request.form['exposure_photo'].replace(',','.'))) time_between_photos = int(float(request.form['time_between_photos'].replace(',','.'))) number_photos = int(float(request.form['number_photos'].replace(',','.'))) resolution = (int(request.form['resolution_timelapse'].split(',')[0]), int(request.form['resolution_timelapse'].split(',')[1])) iso = int(request.form['iso_timelapse']) use_video_port = True if request.form['use_video_port_checkbox']=='true' else False dic_forms = { 'exposure_photo': exposure_photo, 'time_between_photos': time_between_photos, 'number_photos': number_photos, 'resolution': resolution, 'iso': iso, 'use_video_port': use_video_port } except Exception as e: message_error = "[take_timelapse] " + str(e) logger.error(message_error) return {'text': message_error, 'name':"", 'status':"error"} logger.info('Retrieved information from form') return utils_take_timelapse(dic_forms)
def utils_take_video(dic_elems): """Function using the picamera module to take a video Args: dic_elems (dict): dictionnary containing values from form Returns: dict: text=a message to display to the User; name=name of the pictures; status=if something went wrong """ try: with picamera.PiCamera() as camera: camera.resolution = dic_elems['resolution'] logger.info('Camera set up') sleep(2) # warmup logger.info('End of camera warmup') camera.start_recording(video_directory + dic_elems['video_name'] + ".h264", format='h264') camera.wait_recording(dic_elems['video_time']) camera.stop_recording() logger.info('Everything is closed, sending back the response') return { 'text': _("Video is over"), 'name': dic_elems['video_name'], 'status': "ok" } except Exception as e: message_error = "[utils_take_video] " + str(e) logger.error(message_error) return {'text': message_error, 'name': "", 'status': "error"}
def take_a_photo(): """Function to take a photo with the Rpi camera Returns: dict: text=a message to display to the User; name=name of the pictures; status=if something went wrong """ try: photo_name = datetime.today().strftime('%Y-%m-%d-%H-%M-%S') exposure_photo = int(float(request.form['exposure_photo'].replace(',','.'))) resolution = (int(request.form['resolution_photo'].split(',')[0]), int(request.form['resolution_photo'].split(',')[1])) iso = int(request.form['iso_photo']) file_format = request.form['format_photo'] advanced_options_is_checked = True if request.form['advanced_options_checkbox']=='true' else False dic_forms = { 'photo_name': photo_name, 'exposure_photo': exposure_photo, 'resolution': resolution, 'iso': iso, 'file_format': file_format, 'advanced_options_is_checked': advanced_options_is_checked } if advanced_options_is_checked: brightness = int(float(request.form['brightness_photo'].replace(',','.'))) contrast = int(float(request.form['contrast_photo'].replace(',','.'))) sharpness = int(float(request.form['sharpness_photo'].replace(',','.'))) saturation = int(request.form['saturation_photo']) rotation = int(float(request.form['rotation_photo'].replace(',','.'))) hflip = True if request.form['hflip_photo']=='true' else False vflip = True if request.form['vflip_photo']=='true' else False exposure_compensation = int(float(request.form['exposure_compensation_photo'].replace(',','.'))) exposure_mode = request.form['exposure_mode_photo'] image_effect = request.form['image_effect_photo'] meter_mode = request.form['meter_mode_photo'] awb_mode = request.form['awb_mode_photo'] dic_forms.update([('brightness', brightness), ('contrast', contrast), ('sharpness', sharpness), ('saturation', saturation), ('rotation', rotation), ('hflip', hflip), ('vflip', vflip), ('exposure_compensation', exposure_compensation), ('exposure_mode', exposure_mode), ('image_effect', image_effect), ('meter_mode', meter_mode), ('awb_mode', awb_mode) ]) except Exception as e: message_error = "[take_a_photo] " + str(e) logger.error(message_error) return {'text': message_error, 'name':"", 'status':"error"} logger.info('Retrieved information from form') return utils_take_photo(dic_forms)
def utils_take_timelapse(dic_elems): """Function using the picamera module to take a timelapse Args: dic_elems (dict): dictionnary containing values from form Returns: dict: text=a message to display to the User; status=if something went wrong """ try: if dic_elems['exposure_photo'] == 0: framerate = Fraction(0, 1) picamera.PiCamera.CAPTURE_TIMEOUT = 60 else: framerate = Fraction(1, dic_elems['exposure_photo']) picamera.PiCamera.CAPTURE_TIMEOUT = dic_elems[ 'exposure_photo'] * 8 # environ à revoir with picamera.PiCamera(framerate=framerate) as camera: if dic_elems['resolution'] == (4056, 3040): camera.sensor_mode = 3 else: camera.sensor_mode = 0 camera.resolution = dic_elems['resolution'] camera.shutter_speed = dic_elems['exposure_photo'] * 1000000 camera.iso = dic_elems['iso'] add_exif_tags(camera) vide_port = dic_elems['use_video_port'] logger.info('Camera set up') if dic_elems['exposure_photo'] > 6: sleep(30) # warmup else: sleep(3) logger.info('End of camera warmup') for i, filename in enumerate( camera.capture_continuous( timelapse_directory + '{timestamp:%Y_%m_%d_%H_%M_%S}-{counter:03d}.jpg', format='jpeg', use_video_port=vide_port)): logger.info('I took a photo => ' + filename) if i == dic_elems['number_photos'] - 1: break sleep(dic_elems['time_between_photos'] - dic_elems['exposure_photo']) camera.shutter_speed = 0 camera.framerate = 1 logger.info('Everything is closed, sending back the response') return {'text': _("Timelapse is over"), 'status': "ok"} except Exception as e: message_error = "[utils_take_timelapse] " + str(e) logger.error(message_error) return {'text': message_error, 'status': "error"}
def start_video(): """Function to take a video with the Rpi camera Returns: dict: text=a message to display to the User; name=video name; status=if something went wrong """ try: video_time = int(float(request.form['video_time'].replace(',','.'))) video_name = datetime.today().strftime('%Y-%m-%d-%H-%M-%S') resolution = (int(request.form['resolution_video'].split(',')[0]),int(request.form['resolution_video'].split(',')[1])) dic_forms = { 'video_time': video_time, 'video_name': video_name, 'resolution': resolution, } except Exception as e: message_error = "[start_video] " + str(e) logger.error(message_error) return {'text': message_error, 'name':"", 'status':"error"} logger.info('Retrieved information from form') return utils_take_video(dic_forms)
def utils_take_photo(dic_elems): """Function using the picamera module to take a photo Args: dic_elems (dict): dictionnary containing values from form Returns: dict: text=a message to display to the User; name=name of the pictures; status=if something went wrong """ try: if dic_elems['exposure_photo'] == 0: framerate = Fraction(0, 1) picamera.PiCamera.CAPTURE_TIMEOUT = 60 else: framerate = Fraction(1, dic_elems['exposure_photo']) picamera.PiCamera.CAPTURE_TIMEOUT = dic_elems[ 'exposure_photo'] * 8 # environ à revoir with picamera.PiCamera(framerate=framerate) as camera: if dic_elems['resolution'] == (4056, 3040): camera.sensor_mode = 3 else: camera.sensor_mode = 0 camera.shutter_speed = dic_elems['exposure_photo'] * 1000000 camera.iso = dic_elems['iso'] camera.resolution = dic_elems['resolution'] if dic_elems['advanced_options_is_checked']: camera.brightness = dic_elems['brightness'] camera.contrast = dic_elems['contrast'] camera.sharpness = dic_elems['sharpness'] camera.saturation = dic_elems['saturation'] camera.rotation = dic_elems['rotation'] camera.hflip = dic_elems['hflip'] camera.vflip = dic_elems['vflip'] camera.exposure_compensation = dic_elems[ 'exposure_compensation'] camera.exposure_mode = dic_elems['exposure_mode'] camera.image_effect = dic_elems['image_effect'] camera.meter_mode = dic_elems['meter_mode'] camera.awb_mode = dic_elems['awb_mode'] add_exif_tags( camera ) # TODO: check if exif tags is working with format other than jpg logger.info('Camera set up') if dic_elems['exposure_photo'] > 6: sleep(30) # warmup else: sleep(3) logger.info('End of camera warmup') if dic_elems['file_format'] == 'jpg': file_format_bis = 'jpeg' else: file_format_bis = dic_elems['file_format'] camera.capture(picture_directory + dic_elems['photo_name'] + '.' + dic_elems['file_format'], format=file_format_bis) logger.info('The photo was taken') camera.shutter_speed = 0 camera.framerate = 1 logger.info('Everything is closed, sending back the response') return { 'text': _("Photo taken!"), 'name': dic_elems['photo_name'], 'status': "ok" } except Exception as e: message_error = "[utils_take_photo] " + str(e) logger.error(message_error) return {'text': message_error, 'name': "", 'status': "error"}
from appFolder import logger import os from fractions import Fraction from time import sleep from flask_babel import _ from shutil import move try: import picamera except: logger.error("No picamera module available") picture_directory = './appFolder/static/camera/pictures/' timelapse_directory = './appFolder/static/camera/timelapse/' video_directory = './appFolder/static/camera/video/' PROJECT_NAME = "Hubble-Berry" def gen(camera): """It enters a loop where it continuously returns frames from the camera as response chunks. The function asks the camera to provide a frame by calling the camera.get_frame() method, and then it yields with this frame formatted as a response chunk with a content type of image/jpeg Args: camera (picamera.PiCamera): An instance of the camera class Yields: bytes: content type of image/jpeg