Refactor the label_thumbnail function in frigate/http.py to simplify the event_query logic and improve code readability

This commit is contained in:
Sergey Krashevich 2023-07-10 22:38:08 +03:00
parent 46e02acc2b
commit 6a3a67ab89
No known key found for this signature in database
GPG Key ID: 625171324E7D3856

View File

@ -576,26 +576,14 @@ def timeline():
@bp.route("/<camera_name>/<label>/thumbnail.jpg")
def label_thumbnail(camera_name, label):
label = unquote(label)
if label == "any":
event_query = (
Event.select()
.where(Event.camera == camera_name)
.order_by(Event.start_time.desc())
.limit(1)
)
else:
event_query = (
Event.select()
.where(Event.camera == camera_name)
.where(Event.label == label)
.order_by(Event.start_time.desc())
.limit(1)
)
event_query = Event.select(fn.MAX(Event.id)).where(Event.camera == camera_name)
if label != "any":
event_query = event_query.where(Event.label == label)
try:
event = event_query.get()
event = event_query.scalar()
return event_thumbnail(event.id, 60)
return event_thumbnail(event, 60)
except DoesNotExist:
frame = np.zeros((175, 175, 3), np.uint8)
ret, jpg = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])