From 97792b89af3a73a922f3c96b381eae1341c0bcbe Mon Sep 17 00:00:00 2001 From: Yuichiro Aoki <45054071+yuichiroaoki@users.noreply.github.com> Date: Sat, 5 Aug 2023 12:11:00 +0900 Subject: [PATCH] Refactor (#4) * connect lines * update readme * fmt --- README.md | 15 ++++++++++++++- cnceye/cmm/all.py | 18 +++++++++++++++--- cnceye/cmm/single.py | 2 +- cnceye/line.py | 6 +++++- scripts/create_test_images.py | 2 +- tests/test_all.py | 7 ++++++- tests/test_line.py | 36 +++++++++++++++++++++-------------- 7 files changed, 64 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ef7dc4b..a1c9bb1 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ -# cnceye \ No newline at end of file +# cnceye +An image-based CMM (Coordinate Measuring Machine) system for CNC machine tools + +## Simulation with Blender +Create test data + +Prerequisites +- Blender 3.6.1 or later + +Change the output path in `scripts/create_test_images.py` and run + +```bash +blender "blender/example.blend" --background --python scripts/create_test_images.py +``` \ No newline at end of file diff --git a/cnceye/cmm/all.py b/cnceye/cmm/all.py index 913f639..36c997d 100644 --- a/cnceye/cmm/all.py +++ b/cnceye/cmm/all.py @@ -33,15 +33,27 @@ def add_image(self, image, distance: float, center: Coordinate) -> None: self.previous_lines.append(line) def save_image(self, path: str) -> None: - entire_image = np.asarray([[[0, 0, 0]] * 300] * 300, dtype=np.uint8) + entire_image = np.asarray([[[0, 0, 0]] * 1200] * 1000, dtype=np.uint8) for line in self.previous_lines: start = line.start end = line.end cv2.line( entire_image, - (int(start.x + 100), int(-start.y + 100)), - (int(end.x + 100), int(-end.y + 100)), + (int((start.x + 100) * 5), int((-start.y + 100) * 5)), + (int((end.x + 100) * 5), int((-end.y + 100) * 5)), (255, 255, 255), 1, ) + cv2.putText( + entire_image, + f"{line.get_length():.2f}", + ( + int((start.x + end.x + 200) * 5 / 2), + int((-start.y - end.y + 200) * 5 / 2), + ), + cv2.FONT_HERSHEY_SIMPLEX, + 0.4, + (105, 145, 209), + 1, + ) cv2.imwrite(path, entire_image) diff --git a/cnceye/cmm/single.py b/cnceye/cmm/single.py index 1ff4766..472ebe8 100644 --- a/cnceye/cmm/single.py +++ b/cnceye/cmm/single.py @@ -32,7 +32,7 @@ def from_opencv_coord(self, distance: float, opencv_xy: tuple) -> Coordinate: return Coordinate( opencv_origin.x + opencv_xy[0] / pixel_per_mm, opencv_origin.y - opencv_xy[1] / pixel_per_mm, - self.center.x - distance, + self.center.z - distance, ) def from_pixel_length(self, distance: float, pixel_length) -> float: diff --git a/cnceye/line.py b/cnceye/line.py index 1a54e1c..7286ba6 100644 --- a/cnceye/line.py +++ b/cnceye/line.py @@ -55,7 +55,7 @@ def is_same_straight_line(self, other: "Line") -> bool: slope_diff = self.get_slope_diff(other) intercept_diff = self.get_intercept_diff(other) if self.get_slope() == np.inf and other.get_slope() == np.inf: - return abs(self.start.x - other.start.x) < 0.1 + return abs(self.start.x - other.start.x) < 0.2 return slope_diff < 0.1 and intercept_diff < 0.1 def is_x_overlapping(self, other: "Line") -> bool: @@ -79,6 +79,10 @@ def is_y_overlapping(self, other: "Line") -> bool: ) def is_overlapping(self, other: "Line") -> bool: + if self.get_slope() == np.inf and other.get_slope() == np.inf: + return self.is_y_overlapping(other) + if self.get_slope() == 0 and other.get_slope() == 0: + return self.is_x_overlapping(other) return self.is_x_overlapping(other) and self.is_y_overlapping(other) def connect_lines(self, other: "Line") -> "Line" or None: diff --git a/scripts/create_test_images.py b/scripts/create_test_images.py index 6103e10..55f75b6 100644 --- a/scripts/create_test_images.py +++ b/scripts/create_test_images.py @@ -29,7 +29,7 @@ def render_image(output_path, camera_position, camera_rotation, light_position): light_position = (0, 0, 0.1) # Create a folder to save the rendered images - output_folder = "/home/runner/work/cnceye/cnceye/output" + output_folder = "/path/to/output_images" os.makedirs(output_folder, exist_ok=True) # Render images with different camera and light positions diff --git a/tests/test_all.py b/tests/test_all.py index 0ce8161..bd22f13 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -4,6 +4,8 @@ from cnceye.coordinate import Coordinate from tests.utils import diff_in_micron +# import pytest + focal_length = 50.0 # mm camera_height = 60.0 # mm object_height = 10.0 # mm @@ -12,6 +14,7 @@ camera = Camera(focal_length, sensor_width) +# @pytest.mark.skip(reason="need to fix") def test_add_image_one_row(): x_move = Coordinate(20.0, 0.0, 0.0) all_images = AllImages(camera) @@ -45,6 +48,7 @@ def test_add_image_one_row(): assert third_line_diff_in_micron < 100.0 +# @pytest.mark.skip(reason="need to fix") def test_add_image_two_rows(): x_move = Coordinate(20.0, 0.0, 0.0) y_move = Coordinate(0.0, -10.0, 0.0) @@ -58,7 +62,8 @@ def test_add_image_two_rows(): all_images.add_image(image, distance, center) index += 1 - print(f"lines: {len(all_images.previous_lines)}") + line_count = len(all_images.previous_lines) + print(f"line count: {line_count}") for line in all_images.previous_lines: print(line) diff --git a/tests/test_line.py b/tests/test_line.py index 8b34ab8..a5f404f 100644 --- a/tests/test_line.py +++ b/tests/test_line.py @@ -49,29 +49,37 @@ def test_get_intersection(): assert line.get_intersection(other) == (0.5, 0.5) -def test_is_same_straight_line(): - line0 = Line( - Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100) - ) +def test_is_same_straight_line0(): + line0 = Line(Coordinate(-53.01875, 14.89375, -10), Coordinate(-53.01875, 28.0, -10)) line1 = Line( - Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 25.125, -100) + Coordinate(-53.01875, 4.89375, -10), Coordinate(-53.01875, 25.125, -10) ) assert line0.is_same_straight_line(line1) -def test_is_overlapping(): - line0 = Line( - Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100) - ) +def test_is_same_straight_line1(): + line0 = Line(Coordinate(-37.775, 20.66875, 10), Coordinate(37.775, 20.66875, 10)) + line1 = Line(Coordinate(-37.85625, 20.6625, 10), Coordinate(37.85625, 20.6625, 10)) + assert line0.is_same_straight_line(line1) + + +def test_is_overlapping0(): + line0 = Line(Coordinate(-53.01875, 14.89375, -10), Coordinate(-53.01875, 28.0, -10)) line1 = Line( - Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 25.125, -100) + Coordinate(-53.01875, 4.89375, -10), Coordinate(-53.01875, 25.125, -10) ) assert line0.is_overlapping(line1) - line0 = Line( - Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100) - ) + line0 = Line(Coordinate(-53.01875, 14.89375, -10), Coordinate(-53.01875, 28.0, -10)) line1 = Line( - Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 11.125, -100) + Coordinate(-53.01875, 4.89375, -10), Coordinate(-53.01875, 11.125, -10) ) assert not line0.is_overlapping(line1) + + +def test_is_overlapping1(): + line0 = Line( + Coordinate(-42.66875, 4.89375, -10), Coordinate(-42.66875, 15.76875, -10) + ) + line1 = Line(Coordinate(-42.675, 4.89375, -10), Coordinate(-42.675, 15.675, -10)) + assert line0.is_overlapping(line1)