-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add scaled_bbox_centroid and add tests.
- Loading branch information
Showing
8 changed files
with
269 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,110 @@ | ||
# -*- coding: utf-8 -*- | ||
# import pytest | ||
# import numpy as np | ||
# from PIL import Image | ||
# import cv2 | ||
# import dlib | ||
# from unittest.mock import Mock, patch | ||
# from common_image_tools.conversion import tuple_to_rect, rect_to_tuple, pil_to_cv2, cv2_to_pil | ||
# | ||
# | ||
# class TestTupleToRect: | ||
# @pytest.mark.parametrize( | ||
# "bbox, expected_rect", | ||
# [ | ||
# ((0, 0, 10, 10), dlib.rectangle(0, 0, 10, 10)), | ||
# ((-5, -5, 10, 10), dlib.rectangle(-5, -5, 5, 5)), | ||
# ((100, 200, 30, 40), dlib.rectangle(100, 200, 130, 240)), | ||
# ], | ||
# ) | ||
# def test_valid_bboxes(self, bbox, expected_rect): | ||
# rect = tuple_to_rect(bbox) | ||
# assert rect == expected_rect | ||
# | ||
# | ||
# class TestRectToTuple: | ||
# @pytest.mark.parametrize( | ||
# "rect, expected_tuple", | ||
# [ | ||
# (dlib.rectangle(0, 0, 10, 10), (0, 0, 10, 10)), # Test case 1 | ||
# (dlib.rectangle(-5, -5, 5, 5), (-5, -5, 10, 10)), # Test case 2 | ||
# (dlib.rectangle(100, 200, 130, 240), (100, 200, 30, 40)), # Test case 3 | ||
# # Add more test cases for other scenarios | ||
# ], | ||
# ) | ||
# def test_rect_to_tuple(self, rect, expected_tuple): | ||
# assert rect_to_tuple(rect) == expected_tuple | ||
# | ||
# | ||
# # Test pil_to_cv2 function | ||
# def test_pil_to_cv2_rgb(): | ||
# mock_pil_image = Mock(spec=Image.Image) | ||
# mock_np_array = np.array([[[1, 2, 3]]]) | ||
# | ||
# with patch("numpy.array", return_value=mock_np_array): | ||
# with patch("cv2.cvtColor", return_value="cv2_image") as mock_cvtColor: | ||
# result = pil_to_cv2(mock_pil_image) | ||
# | ||
# assert result == "cv2_image" | ||
# mock_cvtColor.assert_called_once_with(mock_np_array, cv2.COLOR_RGB2BGR) | ||
# | ||
# | ||
# def test_pil_to_cv2_rgba(): | ||
# mock_pil_image = Mock(spec=Image.Image) | ||
# mock_np_array = np.array([[[1, 2, 3, 4]]]) | ||
# | ||
# with patch("numpy.array", return_value=mock_np_array): | ||
# with patch("cv2.cvtColor", return_value="cv2_image") as mock_cvtColor: | ||
# result = pil_to_cv2(mock_pil_image) | ||
# | ||
# assert result == "cv2_image" | ||
# mock_cvtColor.assert_called_once_with(mock_np_array, cv2.COLOR_RGBA2BGRA) | ||
# | ||
# | ||
# # Test cv2_to_pil function | ||
# def test_cv2_to_pil_rgb(): | ||
# mock_cv2_image = np.array([[[1, 2, 3]]]) | ||
# | ||
# with patch("cv2.cvtColor", return_value="rgb_image") as mock_cvtColor: | ||
# with patch("PIL.Image.fromarray", return_value="pil_image") as mock_fromarray: | ||
# result = cv2_to_pil(mock_cv2_image) | ||
# | ||
# assert result == "pil_image" | ||
# mock_cvtColor.assert_called_once_with(mock_cv2_image, cv2.COLOR_BGR2RGB) | ||
# mock_fromarray.assert_called_once_with("rgb_image") | ||
# | ||
# | ||
# def test_cv2_to_pil_rgba(): | ||
# mock_cv2_image = np.array([[[1, 2, 3, 4]]]) | ||
# | ||
# with patch("cv2.cvtColor", return_value="rgba_image") as mock_cvtColor: | ||
# with patch("PIL.Image.fromarray", return_value="pil_image") as mock_fromarray: | ||
# result = cv2_to_pil(mock_cv2_image) | ||
# | ||
# assert result == "pil_image" | ||
# mock_cvtColor.assert_called_once_with(mock_cv2_image, cv2.COLOR_BGRA2RGBA) | ||
# mock_fromarray.assert_called_once_with("rgba_image") | ||
# | ||
# | ||
# # Test rect_to_tuple function | ||
# def test_rect_to_tuple(): | ||
# mock_rect = Mock(spec=dlib.rectangle) | ||
# mock_rect.left.return_value = 10 | ||
# mock_rect.top.return_value = 20 | ||
# mock_rect.right.return_value = 30 | ||
# mock_rect.bottom.return_value = 50 | ||
# | ||
# result = rect_to_tuple(mock_rect) | ||
# | ||
# assert result == (10, 20, 20, 30) | ||
# | ||
# | ||
# # Test tuple_to_rect function | ||
# def test_tuple_to_rect(): | ||
# bbox = (10, 20, 30, 40) | ||
# | ||
# with patch("dlib.rectangle", return_value="dlib_rectangle") as mock_rectangle: | ||
# result = tuple_to_rect(bbox) | ||
# | ||
# assert result == "dlib_rectangle" | ||
# mock_rectangle.assert_called_once_with(10, 20, 40, 60) | ||
import pytest | ||
import numpy as np | ||
from PIL import Image | ||
import cv2 | ||
import dlib | ||
from unittest.mock import Mock, patch | ||
from common_image_tools.conversion import tuple_to_rect, rect_to_tuple, pil_to_cv2, cv2_to_pil | ||
|
||
|
||
class TestTupleToRect: | ||
@pytest.mark.parametrize( | ||
"bbox, expected_rect", | ||
[ | ||
((0, 0, 10, 10), dlib.rectangle(0, 0, 10, 10)), | ||
((-5, -5, 10, 10), dlib.rectangle(-5, -5, 5, 5)), | ||
((100, 200, 30, 40), dlib.rectangle(100, 200, 130, 240)), | ||
], | ||
) | ||
def test_valid_bboxes(self, bbox, expected_rect): | ||
rect = tuple_to_rect(bbox) | ||
assert rect == expected_rect | ||
|
||
|
||
class TestRectToTuple: | ||
@pytest.mark.parametrize( | ||
"rect, expected_tuple", | ||
[ | ||
(dlib.rectangle(0, 0, 10, 10), (0, 0, 10, 10)), # Test case 1 | ||
(dlib.rectangle(-5, -5, 5, 5), (-5, -5, 10, 10)), # Test case 2 | ||
(dlib.rectangle(100, 200, 130, 240), (100, 200, 30, 40)), # Test case 3 | ||
# Add more test cases for other scenarios | ||
], | ||
) | ||
def test_rect_to_tuple(self, rect, expected_tuple): | ||
assert rect_to_tuple(rect) == expected_tuple | ||
|
||
|
||
# Test pil_to_cv2 function | ||
def test_pil_to_cv2_rgb(): | ||
mock_pil_image = Mock(spec=Image.Image) | ||
mock_np_array = np.array([[[1, 2, 3]]]) | ||
|
||
with patch("numpy.array", return_value=mock_np_array): | ||
with patch("cv2.cvtColor", return_value="cv2_image") as mock_cvtColor: | ||
result = pil_to_cv2(mock_pil_image) | ||
|
||
assert result == "cv2_image" | ||
mock_cvtColor.assert_called_once_with(mock_np_array, cv2.COLOR_RGB2BGR) | ||
|
||
|
||
def test_pil_to_cv2_rgba(): | ||
mock_pil_image = Mock(spec=Image.Image) | ||
mock_np_array = np.array([[[1, 2, 3, 4]]]) | ||
|
||
with patch("numpy.array", return_value=mock_np_array): | ||
with patch("cv2.cvtColor", return_value="cv2_image") as mock_cvtColor: | ||
result = pil_to_cv2(mock_pil_image) | ||
|
||
assert result == "cv2_image" | ||
mock_cvtColor.assert_called_once_with(mock_np_array, cv2.COLOR_RGBA2BGRA) | ||
|
||
|
||
# Test cv2_to_pil function | ||
def test_cv2_to_pil_rgb(): | ||
mock_cv2_image = np.array([[[1, 2, 3]]]) | ||
|
||
with patch("cv2.cvtColor", return_value="rgb_image") as mock_cvtColor: | ||
with patch("PIL.Image.fromarray", return_value="pil_image") as mock_fromarray: | ||
result = cv2_to_pil(mock_cv2_image) | ||
|
||
assert result == "pil_image" | ||
mock_cvtColor.assert_called_once_with(mock_cv2_image, cv2.COLOR_BGR2RGB) | ||
mock_fromarray.assert_called_once_with("rgb_image") | ||
|
||
|
||
def test_cv2_to_pil_rgba(): | ||
mock_cv2_image = np.array([[[1, 2, 3, 4]]]) | ||
|
||
with patch("cv2.cvtColor", return_value="rgba_image") as mock_cvtColor: | ||
with patch("PIL.Image.fromarray", return_value="pil_image") as mock_fromarray: | ||
result = cv2_to_pil(mock_cv2_image) | ||
|
||
assert result == "pil_image" | ||
mock_cvtColor.assert_called_once_with(mock_cv2_image, cv2.COLOR_BGRA2RGBA) | ||
mock_fromarray.assert_called_once_with("rgba_image") | ||
|
||
|
||
# Test rect_to_tuple function | ||
def test_rect_to_tuple(): | ||
mock_rect = Mock(spec=dlib.rectangle) | ||
mock_rect.left.return_value = 10 | ||
mock_rect.top.return_value = 20 | ||
mock_rect.right.return_value = 30 | ||
mock_rect.bottom.return_value = 50 | ||
|
||
result = rect_to_tuple(mock_rect) | ||
|
||
assert result == (10, 20, 20, 30) | ||
|
||
|
||
# Test tuple_to_rect function | ||
def test_tuple_to_rect(): | ||
bbox = (10, 20, 30, 40) | ||
|
||
with patch("dlib.rectangle", return_value="dlib_rectangle") as mock_rectangle: | ||
result = tuple_to_rect(bbox) | ||
|
||
assert result == "dlib_rectangle" | ||
mock_rectangle.assert_called_once_with(10, 20, 40, 60) |
Oops, something went wrong.