test_yuv_region_2_rgb.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import cv2
  2. import numpy as np
  3. from unittest import TestCase, main
  4. from frigate.util import yuv_region_2_rgb
  5. class TestYuvRegion2RGB(TestCase):
  6. def setUp(self):
  7. self.bgr_frame = np.zeros((100, 200, 3), np.uint8)
  8. self.bgr_frame[:] = (0, 0, 255)
  9. self.bgr_frame[5:55, 5:55] = (255,0,0)
  10. # cv2.imwrite(f"bgr_frame.jpg", self.bgr_frame)
  11. self.yuv_frame = cv2.cvtColor(self.bgr_frame, cv2.COLOR_BGR2YUV_I420)
  12. def test_crop_yuv(self):
  13. cropped = yuv_region_2_rgb(self.yuv_frame, (10,10,50,50))
  14. # ensure the upper left pixel is blue
  15. assert(np.all(cropped[0, 0] == [0, 0, 255]))
  16. def test_crop_yuv_out_of_bounds(self):
  17. cropped = yuv_region_2_rgb(self.yuv_frame, (0,0,200,200))
  18. # cv2.imwrite(f"cropped.jpg", cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
  19. # ensure the upper left pixel is red
  20. # the yuv conversion has some noise
  21. assert(np.all(cropped[0, 0] == [255, 1, 0]))
  22. # ensure the bottom right is black
  23. assert(np.all(cropped[199, 199] == [0, 0, 0]))
  24. def test_crop_yuv_portrait(self):
  25. bgr_frame = np.zeros((1920, 1080, 3), np.uint8)
  26. bgr_frame[:] = (0, 0, 255)
  27. bgr_frame[5:55, 5:55] = (255,0,0)
  28. # cv2.imwrite(f"bgr_frame.jpg", self.bgr_frame)
  29. yuv_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2YUV_I420)
  30. cropped = yuv_region_2_rgb(yuv_frame, (0, 852, 648, 1500))
  31. # cv2.imwrite(f"cropped.jpg", cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
  32. if __name__ == '__main__':
  33. main(verbosity=2)