Skip to content

Commit

Permalink
allow setting size and cropping of snapshots and best.jpg endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeblackshear committed Sep 17, 2020
1 parent 1ce9930 commit 50e568b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ cameras:
################
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
# data related to the tracked object. This works by telling ffmpeg to write video segments to /cache
Expand Down
12 changes: 8 additions & 4 deletions detect_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,14 @@ def stats():
@app.route('/<camera_name>/<label>/best.jpg')
def best(camera_name, label):
if camera_name in CONFIG['cameras']:
best_frame = object_processor.get_best(camera_name, label)
if best_frame is None:
best_frame = np.zeros((720,1280,3), np.uint8)

best_object = object_processor.get_best(camera_name, label)
best_frame = best_object.get('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])))
width = int(height*best_frame.shape[1]/best_frame.shape[0])

Expand Down
12 changes: 10 additions & 2 deletions frigate/object_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ def end(camera, obj):

def snapshot(camera, obj):
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)
if ret:
jpg_bytes = jpg.tobytes()
Expand Down Expand Up @@ -310,9 +318,9 @@ def object_status(camera, object_name, status):
def get_best(self, camera, label):
best_objects = self.camera_states[camera].best_objects
if label in best_objects:
return best_objects[label]['frame']
return best_objects[label]
else:
return None
return {}

def get_current_frame(self, camera):
return self.camera_states[camera].current_frame
Expand Down

0 comments on commit 50e568b

Please sign in to comment.