Files
frigate/frigate/test/test_yuv_region_2_rgb.py
T

44 lines
1.5 KiB
Python
Raw Normal View History

2023-05-29 12:31:17 +02:00
from unittest import TestCase, main
2020-12-12 06:59:38 -06:00
import cv2
import numpy as np
2023-05-29 12:31:17 +02:00
2023-07-06 08:28:50 -06:00
from frigate.util.image import yuv_region_2_rgb
2020-12-12 06:59:38 -06:00
2021-02-17 07:23:32 -06:00
2020-12-12 06:59:38 -06:00
class TestYuvRegion2RGB(TestCase):
def setUp(self):
self.bgr_frame = np.zeros((100, 200, 3), np.uint8)
self.bgr_frame[:] = (0, 0, 255)
2021-02-17 07:23:32 -06:00
self.bgr_frame[5:55, 5:55] = (255, 0, 0)
2020-12-12 06:59:38 -06:00
# cv2.imwrite(f"bgr_frame.jpg", self.bgr_frame)
self.yuv_frame = cv2.cvtColor(self.bgr_frame, cv2.COLOR_BGR2YUV_I420)
def test_crop_yuv(self):
2021-02-17 07:23:32 -06:00
cropped = yuv_region_2_rgb(self.yuv_frame, (10, 10, 50, 50))
2020-12-12 06:59:38 -06:00
# ensure the upper left pixel is blue
2021-02-17 07:23:32 -06:00
assert np.all(cropped[0, 0] == [0, 0, 255])
2020-12-12 06:59:38 -06:00
def test_crop_yuv_out_of_bounds(self):
2021-02-17 07:23:32 -06:00
cropped = yuv_region_2_rgb(self.yuv_frame, (0, 0, 200, 200))
2020-12-12 06:59:38 -06:00
# cv2.imwrite(f"cropped.jpg", cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
# ensure the upper left pixel is red
# the yuv conversion has some noise
2021-02-17 07:23:32 -06:00
assert np.all(cropped[0, 0] == [255, 1, 0])
2020-12-12 06:59:38 -06:00
# ensure the bottom right is black
2021-02-17 07:23:32 -06:00
assert np.all(cropped[199, 199] == [0, 0, 0])
2020-12-12 06:59:38 -06:00
def test_crop_yuv_portrait(self):
bgr_frame = np.zeros((1920, 1080, 3), np.uint8)
bgr_frame[:] = (0, 0, 255)
2021-02-17 07:23:32 -06:00
bgr_frame[5:55, 5:55] = (255, 0, 0)
2020-12-12 06:59:38 -06:00
# cv2.imwrite(f"bgr_frame.jpg", self.bgr_frame)
yuv_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2YUV_I420)
2023-05-29 12:31:17 +02:00
yuv_region_2_rgb(yuv_frame, (0, 852, 648, 1500))
2020-12-12 06:59:38 -06:00
# cv2.imwrite(f"cropped.jpg", cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
2021-02-17 07:23:32 -06:00
if __name__ == "__main__":
2022-04-16 08:43:26 -05:00
main(verbosity=2)