diff --git a/eyetrackvr_backend/algorithms/ahsf.py b/eyetrackvr_backend/algorithms/ahsf.py index c9f1dc1..fc2f38b 100644 --- a/eyetrackvr_backend/algorithms/ahsf.py +++ b/eyetrackvr_backend/algorithms/ahsf.py @@ -350,7 +350,7 @@ def coarse_detection(img_gray, params): return pupil_rect_coarse, outer_rect_coarse, max_response_coarse, mu_inner, mu_outer -def fine_detection(frame, pupil_rect_coarse): +def fine_detection(frame: MatLike, pupil_rect_coarse): valid_ratio = 1.2 boundary = (0, 0, frame.shape[1], frame.shape[0]) valid_rect = intersect_rect(rect_scale(pupil_rect_coarse, valid_ratio), boundary) @@ -358,7 +358,7 @@ def fine_detection(frame, pupil_rect_coarse): valid_rect[1] : valid_rect[1] + valid_rect[3], valid_rect[0] : valid_rect[0] + valid_rect[2], ] - img_pupil_blur = cv2.GaussianBlur(img_pupil, (5, 5), 0, 0) + img_pupil_blur = cv2.GaussianBlur(img_pupil, (5, 5), sigmaX=0, sigmaY=0) edges_filter = detect_edges(img_pupil_blur) # fit ellipse to edges contours, hierarchy = cv2.findContours(edges_filter, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) @@ -399,12 +399,14 @@ def fine_detection(frame, pupil_rect_coarse): return pupil_rect_coarse, center -def detect_edges(img_pupil_blur): +def detect_edges(img_pupil_blur: MatLike): edges = cv2.Canny(img_pupil_blur, 64, 128) # img_bw = np.zeros_like(img_pupil_blur) # img_bw[img_pupil_blur > 100] = 255 - img_bw = cv2.compare(img_pupil_blur, 100, cv2.CMP_GT) + + # The usage is valid, the typing in opencv-python is wrong, see https://github.com/opencv/opencv-python/issues/1008 + img_bw = cv2.compare(img_pupil_blur, 100, cv2.CMP_GT) # type: ignore[call-overload] kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) img_bw = cv2.dilate(img_bw, kernel) diff --git a/eyetrackvr_backend/algorithms/ransac.py b/eyetrackvr_backend/algorithms/ransac.py index 715323a..27925e7 100644 --- a/eyetrackvr_backend/algorithms/ransac.py +++ b/eyetrackvr_backend/algorithms/ransac.py @@ -29,13 +29,13 @@ # ruff: noqa: F841 # TODO: remove this noqa once unused variables have been cleaned up +import platform import cv2 import numpy as np from enum import IntEnum import os import psutil -import sys from cv2.typing import MatLike from ..processes import EyeProcessor from ..utils import BaseAlgorithm @@ -44,13 +44,12 @@ from pye3d.detector_3d import Detector3D, DetectorMode process = psutil.Process(os.getpid()) # set process priority to low -try: # medium chance this does absolutely nothing but eh - sys.getwindowsversion() -except AttributeError: - process.nice(0) # UNIX: 0 low 10 high +if platform.system() == "Windows": + # Ignoring type error appearing on Linux as this is a Windows variable + process.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS) # type: ignore[attr-defined] process.nice() else: - process.nice(psutil.BELOW_NORMAL_PRIORITY_CLASS) # Windows + process.nice(0) # UNIX: 0 low 10 high process.nice() @@ -284,14 +283,13 @@ def run(self, frame: MatLike, tracker_position: TrackerPosition) -> tuple[EyeDat hull = [] for cnt in contours: - hull.append(cv2.convexHull(cnt, False)) + hull.append(cv2.convexHull(cnt, clockwise=False)) if not hull: # If empty, go to next loop pass try: - - cnt = sorted(hull, key=cv2.contourArea) - maxcnt = cnt[-1] + contour = sorted(hull, key=cv2.contourArea) + maxcnt = contour[-1] # ellipse = cv2.fitEllipse(maxcnt) ransac_data = fit_rotated_ellipse_ransac(maxcnt.reshape(-1, 2), rng) print(ransac_data) diff --git a/eyetrackvr_backend/processes/camera.py b/eyetrackvr_backend/processes/camera.py index 05833c8..e653ca1 100644 --- a/eyetrackvr_backend/processes/camera.py +++ b/eyetrackvr_backend/processes/camera.py @@ -127,7 +127,7 @@ def connect_serial_camera(self) -> None: if os.path.islink(capture_source): capture_source = os.path.realpath(capture_source) self.logger.info(f"Connecting to serial capture source {self.current_capture_source} ({capture_source})") - if not any(p for p in serial.tools.list_ports.comports() if capture_source in p): + if not any(p for p in serial.tools.list_ports.comports() if capture_source in p.device): self.logger.warning(f"Serial port `{self.current_capture_source}` (`{capture_source}`) not found, waiting for reconnect.") self.set_state(CameraState.DISCONNECTED) time.sleep(COM_PORT_NOT_FOUND_TIMEOUT) @@ -135,9 +135,9 @@ def connect_serial_camera(self) -> None: try: self.serial_camera = serial.Serial(port=capture_source, baudrate=3000000, xonxoff=False, dsrdtr=False, rtscts=False) - # The `set_buffer_size` method is only available on Windows + # The `set_buffer_size` method is only available on Windows (we ignore the type error for linux) if os.name == "nt": - self.serial_camera.set_buffer_size(rx_size=32768, tx_size=32768) + self.serial_camera.set_buffer_size(rx_size=32768, tx_size=32768) # type: ignore[attr-defined] self.logger.info(f"Serial camera connected to `{self.current_capture_source}` (`{capture_source}`)") self.set_state(CameraState.CONNECTED) except Exception: