Skip to content

Commit

Permalink
Fix API latest image quality and API MIME types (#15964)
Browse files Browse the repository at this point in the history
* Fix API latest image quality

* Fix mime types

* Code formatting + media_type fix
  • Loading branch information
tpjanssen authored Jan 13, 2025
1 parent 32c71c4 commit 9983bd8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions frigate/api/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ def latest_frame(
"regions": params.regions,
}
quality = params.quality
mime_type = extension

if extension == "png":
quality_params = None
elif extension == "webp":
quality_params = [int(cv2.IMWRITE_WEBP_QUALITY), quality]
else:
quality_params = [int(cv2.IMWRITE_JPEG_QUALITY), quality]
mime_type = "jpeg"

if camera_name in request.app.frigate_config.cameras:
frame = frame_processor.get_current_frame(camera_name, draw_options)
Expand Down Expand Up @@ -173,13 +182,11 @@ def latest_frame(

frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_AREA)

ret, img = cv2.imencode(
f".{extension}", frame, [int(cv2.IMWRITE_WEBP_QUALITY), quality]
)
ret, img = cv2.imencode(f".{extension}", frame, quality_params)
return Response(
content=img.tobytes(),
media_type=f"image/{extension}",
headers={"Content-Type": f"image/{extension}", "Cache-Control": "no-store"},
media_type=f"image/{mime_type}",
headers={"Content-Type": f"image/{mime_type}", "Cache-Control": "no-store"},
)
elif camera_name == "birdseye" and request.app.frigate_config.birdseye.restream:
frame = cv2.cvtColor(
Expand All @@ -192,13 +199,11 @@ def latest_frame(

frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_AREA)

ret, img = cv2.imencode(
f".{extension}", frame, [int(cv2.IMWRITE_WEBP_QUALITY), quality]
)
ret, img = cv2.imencode(f".{extension}", frame, quality_params)
return Response(
content=img.tobytes(),
media_type=f"image/{extension}",
headers={"Content-Type": f"image/{extension}", "Cache-Control": "no-store"},
media_type=f"image/{mime_type}",
headers={"Content-Type": f"image/{mime_type}", "Cache-Control": "no-store"},
)
else:
return JSONResponse(
Expand Down Expand Up @@ -241,6 +246,7 @@ def get_snapshot_from_recording(
recording: Recordings = recording_query.get()
time_in_segment = frame_time - recording.start_time
codec = "png" if format == "png" else "mjpeg"
mime_type = "png" if format == "png" else "jpeg"
config: FrigateConfig = request.app.frigate_config

image_data = get_image_from_recording(
Expand All @@ -257,7 +263,7 @@ def get_snapshot_from_recording(
),
status_code=404,
)
return Response(image_data, headers={"Content-Type": f"image/{format}"})
return Response(image_data, headers={"Content-Type": f"image/{mime_type}"})
except DoesNotExist:
return JSONResponse(
content={
Expand Down

0 comments on commit 9983bd8

Please sign in to comment.