fix support for yuv models (#4523)

This commit is contained in:
Blake Blackshear 2022-11-26 19:15:47 -06:00 committed by GitHub
parent 91982c4f7e
commit 047c2408d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 2 deletions

View File

@ -379,6 +379,47 @@ def yuv_crop_and_resize(frame, region, height=None):
return yuv_cropped_frame return yuv_cropped_frame
def yuv_to_3_channel_yuv(yuv_frame):
height = yuv_frame.shape[0] // 3 * 2
width = yuv_frame.shape[1]
# flatten the image into array
yuv_data = yuv_frame.ravel()
# create a numpy array to hold all the 3 chanel yuv data
all_yuv_data = np.empty((height, width, 3), dtype=np.uint8)
y_count = height * width
uv_count = y_count // 4
# copy the y_channel
all_yuv_data[:, :, 0] = yuv_data[0:y_count].reshape((height, width))
# copy the u channel doubling each dimension
all_yuv_data[:, :, 1] = np.repeat(
np.reshape(
np.repeat(yuv_data[y_count : y_count + uv_count], repeats=2, axis=0),
(height // 2, width),
),
repeats=2,
axis=0,
)
# copy the v channel doubling each dimension
all_yuv_data[:, :, 2] = np.repeat(
np.reshape(
np.repeat(
yuv_data[y_count + uv_count : y_count + uv_count + uv_count],
repeats=2,
axis=0,
),
(height // 2, width),
),
repeats=2,
axis=0,
)
return all_yuv_data
def copy_yuv_to_position( def copy_yuv_to_position(
destination_frame, destination_frame,
destination_offset, destination_offset,
@ -497,6 +538,17 @@ def copy_yuv_to_position(
) )
def yuv_region_2_yuv(frame, region):
try:
# TODO: does this copy the numpy array?
yuv_cropped_frame = yuv_crop_and_resize(frame, region)
return yuv_to_3_channel_yuv(yuv_cropped_frame)
except:
print(f"frame.shape: {frame.shape}")
print(f"region: {region}")
raise
def yuv_region_2_rgb(frame, region): def yuv_region_2_rgb(frame, region):
try: try:
# TODO: does this copy the numpy array? # TODO: does this copy the numpy array?

View File

@ -29,9 +29,9 @@ from frigate.util import (
intersection, intersection,
intersection_over_union, intersection_over_union,
listen, listen,
yuv_crop_and_resize,
yuv_region_2_rgb, yuv_region_2_rgb,
yuv_region_2_bgr, yuv_region_2_bgr,
yuv_region_2_yuv,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -97,7 +97,7 @@ def create_tensor_input(frame, model_config, region):
elif model_config.input_pixel_format == PixelFormatEnum.bgr: elif model_config.input_pixel_format == PixelFormatEnum.bgr:
cropped_frame = yuv_region_2_bgr(frame, region) cropped_frame = yuv_region_2_bgr(frame, region)
else: else:
cropped_frame = yuv_crop_and_resize(frame, region) cropped_frame = yuv_region_2_yuv(frame, region)
# Resize if needed # Resize if needed
if cropped_frame.shape != (model_config.height, model_config.width, 3): if cropped_frame.shape != (model_config.height, model_config.width, 3):