14 lines
449 B
Python
14 lines
449 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import cv2
|
||
|
|
import numpy as np
|
||
|
|
|
||
|
|
|
||
|
|
def frame_to_jpeg_bytes(frame: np.ndarray, *, quality: int = 85) -> bytes:
|
||
|
|
"""Encode BGR frame to JPEG bytes for model services expecting image bytes."""
|
||
|
|
params = [int(cv2.IMWRITE_JPEG_QUALITY), int(quality)]
|
||
|
|
ok, buf = cv2.imencode(".jpg", frame, params)
|
||
|
|
if not ok or buf is None:
|
||
|
|
raise RuntimeError("cv2.imencode failed for JPEG")
|
||
|
|
return buf.tobytes()
|