diff --git a/parallax/calibration_camera.py b/parallax/calibration_camera.py index a633199c..3ddd339b 100644 --- a/parallax/calibration_camera.py +++ b/parallax/calibration_camera.py @@ -14,7 +14,7 @@ # Set logger name logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.WARNING) # Set the logging level for PyQt5.uic.uiparser/properties logging.getLogger("PyQt5.uic.uiparser").setLevel(logging.DEBUG) logging.getLogger("PyQt5.uic.properties").setLevel(logging.DEBUG) diff --git a/parallax/reticle_detect_manager.py b/parallax/reticle_detect_manager.py index 21d8bce6..62eaa2b2 100644 --- a/parallax/reticle_detect_manager.py +++ b/parallax/reticle_detect_manager.py @@ -177,12 +177,6 @@ def process(self, frame): if not ret: logger.debug(f"{ self.name} get_coords_interest fails ") - frame_ = cv2.cvtColor(frame_, cv2.COLOR_GRAY2BGR) - for pixel in inliner_lines_pixels[0]: - cv2.circle(frame_, tuple(pixel), 2, (0, 255, 0), -1) - for pixel in inliner_lines_pixels[1]: - cv2.circle(frame_, tuple(pixel), 1, (255, 0, 0), -1) - cv2.imwrite("debug/reticle_detect_fail.jpg", frame_) else: # TODO # ret, mtx, dist = self.calibrationCamera.get_predefined_intrinsic(x_axis_coords, y_axis_coords) diff --git a/parallax/reticle_detection.py b/parallax/reticle_detection.py index f14927bc..6f90cd2b 100644 --- a/parallax/reticle_detection.py +++ b/parallax/reticle_detection.py @@ -17,7 +17,7 @@ # Set logger name logger = logging.getLogger(__name__) # Set the logging level for PyQt5.uic.uiparser/properties to WARNING, to ignore DEBUG messages -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.WARNING) logging.getLogger("PyQt5.uic.uiparser").setLevel(logging.WARNING) logging.getLogger("PyQt5.uic.properties").setLevel(logging.WARNING) @@ -407,7 +407,7 @@ def _add_missing_pixels(self, bg, lines, line_pixels): pixels_array = np.array(pixels) missing_points = self._estimate_missing_points(pixels_array) x_diff, y_diff = self._get_median_distance_x_y(pixels_array) - + if missing_points.ndim == 1 and missing_points.size > 0: missing_points = missing_points.reshape(-1, 2) # Reshape to 2D if it's flat but not empty elif missing_points.size == 0: @@ -558,6 +558,23 @@ def coords_detect_morph(self, img): return ret, img, inliner_lines, inliner_lines_pixels + def _draw_debug(self, img, pixels_in_lines, filename): + if logger.getEffectiveLevel() == logging.DEBUG: + if img.ndim == 2: + img_ = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) + else: + img_ = img.copy() + + if len(pixels_in_lines) == 2: + for pt in pixels_in_lines[0]: + cv2.circle(img_, tuple(pt), 2, (0, 255, 0), -1) + for pt in pixels_in_lines[1]: + cv2.circle(img_, tuple(pt), 1, (255, 0, 0), -1) + + cv2.imwrite(f"debug/{filename}.jpg", img_) + else: + return + def get_coords(self, img): """Detect coordinates using morphological operations. @@ -572,42 +589,23 @@ def get_coords(self, img): - inliner_lines_pixels (list): List of inlier pixel coordinates for each line. """ bg = self._preprocess_image(img) - cv2.imwrite("debug/0_bg.jpg", bg) + self._draw_debug(bg, [], "0_bg") masked = self._apply_mask(bg) - cv2.imwrite("debug/masked.jpg", masked) + self._draw_debug(masked, [], "1_bg") ret, bg, inliner_lines, pixels_in_lines = self.coords_detect_morph(masked) - bg_ = cv2.cvtColor(bg, cv2.COLOR_GRAY2BGR) - for pixel in pixels_in_lines[0]: - pt = tuple(pixel) - cv2.circle(bg_, pt, 2, (0, 255, 0), -1) - for pixel in pixels_in_lines[1]: - pt = tuple(pixel) - cv2.circle(bg_, pt, 1, (255, 0, 0), -1) - cv2.imwrite("debug/1coords_detect_morph.jpg", bg_) - - + self._draw_debug(bg, pixels_in_lines, "2_detect_morph") logger.debug(f"{self.name} nLines: {len(pixels_in_lines)}") + if ret: bg, inliner_lines, pixels_in_lines = self._refine_pixels(bg, inliner_lines, pixels_in_lines) logger.debug(f"{self.name} detect: {len(pixels_in_lines[0])}, {len(pixels_in_lines[1])}" ) - for pixel in pixels_in_lines[0]: - pt = tuple(pixel) - cv2.circle(bg_, pt, 2, (0, 255, 0), -1) - for pixel in pixels_in_lines[1]: - pt = tuple(pixel) - cv2.circle(bg_, pt, 1, (255, 0, 0), -1) - cv2.imwrite("debug/2refine_pixels.jpg", bg_) + self._draw_debug(bg, pixels_in_lines, "3_refine_pixels") bg, pixels_in_lines = self._add_missing_pixels(bg, inliner_lines, pixels_in_lines) logger.debug(f"{self.name} interpolate: {len(pixels_in_lines[0])} {len(pixels_in_lines[1])}") - for pixel in pixels_in_lines[0]: - pt = tuple(pixel) - cv2.circle(bg_, pt, 2, (0, 255, 0), -1) - for pixel in pixels_in_lines[1]: - pt = tuple(pixel) - cv2.circle(bg_, pt, 1, (255, 0, 0), -1) - cv2.imwrite("debug/3_add_missing_pixels.jpg", bg_) + self._draw_debug(bg, pixels_in_lines, "4_add_missing_pixels") + return ret, bg, inliner_lines, pixels_in_lines """ diff --git a/parallax/reticle_detection_coords_interests.py b/parallax/reticle_detection_coords_interests.py index 89db969a..bc3ebaed 100644 --- a/parallax/reticle_detection_coords_interests.py +++ b/parallax/reticle_detection_coords_interests.py @@ -75,7 +75,9 @@ def _get_center_coords_index(self, center, coords): x_center, y_center = center for i in range(-5, 6): # Range from -5 to 5 for j in range(-5, 6): # Range from -5 to 5 - test_center = np.array([x_center + i, y_center + j]) + test_center = np.array( + [x_center + i, y_center + j], dtype=coords.dtype + ) result = np.where((coords == test_center).all(axis=1)) if len(result[0]) > 0: # Check if the element was found return result[0][0] # Return the first occurrence index @@ -93,6 +95,7 @@ def _get_pixels_interest(self, center, coords): """ center_index = self._get_center_coords_index(center, coords) if center_index is None: + logger.debug("Center coordinates not found.") return coords[center_index] = center # Replace center to center point we gets @@ -162,10 +165,16 @@ def get_coords_interest(self, pixels_in_lines): for pixels_in_line in pixels_in_lines: coords = self._get_pixels_interest(center_point, pixels_in_line) if coords is None or len(coords) < self.n_interest_pixels * 2 + 1: + logger.debug(f"_get_pixels_interest fails.") + if coords is None: + logger.debug(f"coords: None") + if coords is not None: + logger.debug(f"length of coords: {len(coords)}") return False, None, None coords_interest.append(coords) ret, x_axis, y_axis = self._get_orientation(coords_interest) if ret is False: + logger.debug(f"getting orientation of x and y axis fails") return False, None, None return True, x_axis, y_axis