allow setting size and cropping of snapshots and best.jpg endpoint

This commit is contained in:
Blake Blackshear 2020-09-13 08:57:47 -05:00
parent 1ce993051e
commit 50e568b84c
3 changed files with 25 additions and 6 deletions

View File

@ -162,6 +162,13 @@ cameras:
################ ################
take_frame: 1 take_frame: 1
################
# MQTT settings
################
# mqtt:
# crop_to_region: True
# snapshot_height: 300
################ ################
# This will save a clip for each tracked object by frigate along with a json file that contains # This will save a clip for each tracked object by frigate along with a json file that contains
# data related to the tracked object. This works by telling ffmpeg to write video segments to /cache # data related to the tracked object. This works by telling ffmpeg to write video segments to /cache

View File

@ -381,9 +381,13 @@ def main():
@app.route('/<camera_name>/<label>/best.jpg') @app.route('/<camera_name>/<label>/best.jpg')
def best(camera_name, label): def best(camera_name, label):
if camera_name in CONFIG['cameras']: if camera_name in CONFIG['cameras']:
best_frame = object_processor.get_best(camera_name, label) best_object = object_processor.get_best(camera_name, label)
if best_frame is None: best_frame = best_object.get('frame', np.zeros((720,1280,3), np.uint8))
best_frame = np.zeros((720,1280,3), np.uint8)
crop = bool(request.args.get('crop', 0))
if crop:
region = best_object.get('region', [0,0,300,300])
best_frame = best_frame[region[1]:region[3], region[0]:region[2]]
height = int(request.args.get('h', str(best_frame.shape[0]))) height = int(request.args.get('h', str(best_frame.shape[0])))
width = int(height*best_frame.shape[1]/best_frame.shape[0]) width = int(height*best_frame.shape[1]/best_frame.shape[0])

View File

@ -254,6 +254,14 @@ class TrackedObjectProcessor(threading.Thread):
def snapshot(camera, obj): def snapshot(camera, obj):
best_frame = cv2.cvtColor(obj['frame'], cv2.COLOR_RGB2BGR) best_frame = cv2.cvtColor(obj['frame'], cv2.COLOR_RGB2BGR)
mqtt_config = self.camera_config.get('mqtt', {'crop_to_region': False})
if mqtt_config.get('crop_to_region'):
region = obj['region']
best_frame = best_frame[region[1]:region[3], region[0]:region[2]]
if 'snapshot_height' in mqtt_config:
height = int(mqtt_config['snapshot_height'])
width = int(height*best_frame.shape[1]/best_frame.shape[0])
best_frame = cv2.resize(best_frame, dsize=(width, height), interpolation=cv2.INTER_AREA)
ret, jpg = cv2.imencode('.jpg', best_frame) ret, jpg = cv2.imencode('.jpg', best_frame)
if ret: if ret:
jpg_bytes = jpg.tobytes() jpg_bytes = jpg.tobytes()
@ -310,9 +318,9 @@ class TrackedObjectProcessor(threading.Thread):
def get_best(self, camera, label): def get_best(self, camera, label):
best_objects = self.camera_states[camera].best_objects best_objects = self.camera_states[camera].best_objects
if label in best_objects: if label in best_objects:
return best_objects[label]['frame'] return best_objects[label]
else: else:
return None return {}
def get_current_frame(self, camera): def get_current_frame(self, camera):
return self.camera_states[camera].current_frame return self.camera_states[camera].current_frame