fix: type comparason error

This commit is contained in:
Prohurtz 2024-08-17 11:24:55 -05:00
parent 9a130a1de9
commit bf4b77abe7

View File

@ -39,11 +39,11 @@ from utils.misc_utils import PlaySound, SND_FILENAME, SND_ASYNC, resource_path
import numpy as np import numpy as np
# for clarity when indexing # for clarity when indexing
X = 0 X = 0
Y = 1 Y = 1
class CameraWidget: class CameraWidget:
def __init__(self, widget_id: EyeId, main_config: EyeTrackConfig, osc_queue: Queue): def __init__(self, widget_id: EyeId, main_config: EyeTrackConfig, osc_queue: Queue):
self.gui_camera_addr = f"-CAMERAADDR{widget_id}-" self.gui_camera_addr = f"-CAMERAADDR{widget_id}-"
@ -80,9 +80,7 @@ class CameraWidget:
elif self.eye_id == EyeId.LEFT: elif self.eye_id == EyeId.LEFT:
self.config = main_config.left_eye self.config = main_config.left_eye
else: else:
raise RuntimeError( raise RuntimeError("\033[91m[WARN] Cannot have a camera widget represent both eyes!\033[0m")
"\033[91m[WARN] Cannot have a camera widget represent both eyes!\033[0m"
)
self.cancellation_event = Event() self.cancellation_event = Event()
# Set the event until start is called, otherwise we can block if shutdown is called. # Set the event until start is called, otherwise we can block if shutdown is called.
@ -102,7 +100,7 @@ class CameraWidget:
self.capture_queue, self.capture_queue,
self.image_queue, self.image_queue,
self.eye_id, self.eye_id,
self.osc_queue self.osc_queue,
) )
self.camera_status_queue = Queue() self.camera_status_queue = Queue()
@ -115,9 +113,6 @@ class CameraWidget:
self.capture_queue, self.capture_queue,
) )
self.hover = None self.hover = None
# cartesian co-ordinates in widget space are used during selection # cartesian co-ordinates in widget space are used during selection
@ -129,7 +124,7 @@ class CameraWidget:
self.roi_size = None self.roi_size = None
self.clip_size = None self.clip_size = None
self.clip_pos = None self.clip_pos = None
self.padded_size = None self.padded_size = [244, 244]
self.img_pos = None self.img_pos = None
self.roi_image_center = None self.roi_image_center = None
@ -139,8 +134,6 @@ class CameraWidget:
self.movavg_fps_queue = deque(maxlen=120) self.movavg_fps_queue = deque(maxlen=120)
self.movavg_bps_queue = deque(maxlen=120) self.movavg_bps_queue = deque(maxlen=120)
def get_widget_layout(self): def get_widget_layout(self):
self.widget_layout = [ self.widget_layout = [
[ [
@ -260,9 +253,7 @@ class CameraWidget:
], ],
[ [
sg.Text("Mode:", background_color="#424042"), sg.Text("Mode:", background_color="#424042"),
sg.Text( sg.Text("Calibrating", key=self.gui_mode_readout, background_color="#424042"),
"Calibrating", key=self.gui_mode_readout, background_color="#424042"
),
sg.Text("", key=self.gui_tracking_fps, background_color="#424042"), sg.Text("", key=self.gui_tracking_fps, background_color="#424042"),
sg.Text("", key=self.gui_tracking_bps, background_color="#424042"), sg.Text("", key=self.gui_tracking_bps, background_color="#424042"),
# sg.Checkbox( # sg.Checkbox(
@ -293,7 +284,6 @@ class CameraWidget:
], ],
] ]
def update_layouts(self): def update_layouts(self):
self.get_roi_layout() self.get_roi_layout()
self.get_tracking_layout() self.get_tracking_layout()
@ -313,8 +303,7 @@ class CameraWidget:
if not (self.xy0 is None or self.xy1 is None): if not (self.xy0 is None or self.xy1 is None):
roi_center = (self.xy0 + self.xy1) / 2 - self.roi_image_center roi_center = (self.xy0 + self.xy1) / 2 - self.roi_image_center
self.cr = np.linalg.norm(roi_center) self.cr = np.linalg.norm(roi_center)
self.ca = math.atan2(roi_center[Y], roi_center[X]) + \ self.ca = math.atan2(roi_center[Y], roi_center[X]) + math.radians(self.config.rotation_angle)
math.radians(self.config.rotation_angle)
self.roi_size = np.abs(self.xy1 - self.xy0) self.roi_size = np.abs(self.xy1 - self.xy0)
def _polar_to_cartesian_at_angle(self, rotation_angle_radians): def _polar_to_cartesian_at_angle(self, rotation_angle_radians):
@ -322,17 +311,14 @@ class CameraWidget:
ca = self.ca - rotation_angle_radians ca = self.ca - rotation_angle_radians
cx = math.cos(ca) * self.cr + self.roi_image_center[X] cx = math.cos(ca) * self.cr + self.roi_image_center[X]
cy = math.sin(ca) * self.cr + self.roi_image_center[Y] cy = math.sin(ca) * self.cr + self.roi_image_center[Y]
roi_pos = np.array((int(cx), int(cy))) - self.roi_size//2 roi_pos = np.array((int(cx), int(cy))) - self.roi_size // 2
return (roi_pos, roi_pos + self.roi_size) return (roi_pos, roi_pos + self.roi_size)
else: else:
return (None, None) return (None, None)
def _polar_to_cartesian(self): def _polar_to_cartesian(self):
if not (self.cr is None or self.ca is None or self.roi_size is None): if not (self.cr is None or self.ca is None or self.roi_size is None):
(self.xy0), (self.xy1) = \ (self.xy0), (self.xy1) = self._polar_to_cartesian_at_angle(math.radians(self.config.rotation_angle))
self._polar_to_cartesian_at_angle(
math.radians(self.config.rotation_angle))
def started(self): def started(self):
return not self.cancellation_event.is_set() return not self.cancellation_event.is_set()
@ -382,17 +368,9 @@ class CameraWidget:
if self.settings.gui_disable_gui == False: if self.settings.gui_disable_gui == False:
# If anything has changed in our configuration settings, change/update those. # If anything has changed in our configuration settings, change/update those.
if ( if event == self.gui_save_tracking_button and values[self.gui_camera_addr] != self.config.capture_source:
event == self.gui_save_tracking_button print("\033[94m[INFO] New value: {}\033[0m".format(values[self.gui_camera_addr]))
and values[self.gui_camera_addr] != self.config.capture_source
):
print(
"\033[94m[INFO] New value: {}\033[0m".format(
values[self.gui_camera_addr]
)
)
try: try:
# Try storing ints as ints, for those using wired cameras. # Try storing ints as ints, for those using wired cameras.
self.config.capture_source = int(values[self.gui_camera_addr]) self.config.capture_source = int(values[self.gui_camera_addr])
@ -405,9 +383,7 @@ class CameraWidget:
and "http" not in values[self.gui_camera_addr] and "http" not in values[self.gui_camera_addr]
and ".mp4" not in values[self.gui_camera_addr] and ".mp4" not in values[self.gui_camera_addr]
): # If http is not in camera address, add it. ): # If http is not in camera address, add it.
self.config.capture_source = ( self.config.capture_source = f"http://{values[self.gui_camera_addr]}/"
f"http://{values[self.gui_camera_addr]}/"
)
else: else:
self.config.capture_source = values[self.gui_camera_addr] self.config.capture_source = values[self.gui_camera_addr]
changed = True changed = True
@ -422,7 +398,6 @@ class CameraWidget:
changed = True changed = True
self.cartesian_needs_update = True self.cartesian_needs_update = True
# if self.config.gui_circular_crop != values[self.gui_circular_crop]: # if self.config.gui_circular_crop != values[self.gui_circular_crop]:
# self.config.gui_circular_crop = values[self.gui_circular_crop] # self.config.gui_circular_crop = values[self.gui_circular_crop]
# changed = True # changed = True
@ -476,6 +451,7 @@ class CameraWidget:
if self.is_mouse_up: if self.is_mouse_up:
self.hover_pos = np.array(values[self.gui_roi_selection]) self.hover_pos = np.array(values[self.gui_roi_selection])
if self.padded_size is not None:
if any(self.hover_pos > self.padded_size): if any(self.hover_pos > self.padded_size):
self.hover_pos = None self.hover_pos = None
@ -526,7 +502,7 @@ class CameraWidget:
# cv2.waitKey(1) # cv2.waitKey(1)
# cv2.destroyAllWindows() # cv2.destroyAllWindows()
# print("lighen") # print("lighen")
#except Empty: # except Empty:
# pass # pass
# if event == self.gui_mask_markup: # if event == self.gui_mask_markup:
# print("markup") # print("markup")
@ -542,9 +518,9 @@ class CameraWidget:
img_h, img_w, _ = image.shape img_h, img_w, _ = image.shape
hyp = math.ceil((img_w**2 + img_h**2)**0.5) hyp = math.ceil((img_w**2 + img_h**2) ** 0.5)
rotation_matrix = cv2.getRotationMatrix2D( rotation_matrix = cv2.getRotationMatrix2D(
((img_w/2), (img_h/2)), self.config.rotation_angle, 1 ((img_w / 2), (img_h / 2)), self.config.rotation_angle, 1
) )
# calculate position of all four corners of image # calculate position of all four corners of image
@ -552,23 +528,21 @@ class CameraWidget:
# calculate crop corner locations in original image space # calculate crop corner locations in original image space
x_coords, y_coords = np.matmul( x_coords, y_coords = np.matmul(
rotation_matrix, rotation_matrix,
np.transpose([ np.transpose([[0, 0, 1], [img_w, 0, 1], [0, img_h, 1], [img_w, img_h, 1]]),
[0, 0, 1],
[img_w, 0, 1],
[0, img_h, 1],
[img_w, img_h, 1]]),
) )
self.clip_size = np.array([math.ceil(max(x_coords) - min(x_coords)), self.clip_size = np.array(
math.ceil(max(y_coords) - min(y_coords))]) [math.ceil(max(x_coords) - min(x_coords)), math.ceil(max(y_coords) - min(y_coords))]
)
if self.config.gui_rotation_ui_padding: if self.config.gui_rotation_ui_padding:
self.padded_size = np.array([hyp, hyp]) self.padded_size = np.array([hyp, hyp])
else: else:
self.padded_size = self.clip_size self.padded_size = self.clip_size
self.img_pos = ((self.padded_size - (img_w, img_h))/2).astype(np.int32) self.img_pos = ((self.padded_size - (img_w, img_h)) / 2).astype(np.int32)
self.clip_pos = ((self.padded_size - self.clip_size)/2).astype(np.int32) self.clip_pos = ((self.padded_size - self.clip_size) / 2).astype(np.int32)
self.roi_image_center = self.padded_size / 2 self.roi_image_center = self.padded_size / 2
@ -577,9 +551,7 @@ class CameraWidget:
self._polar_to_cartesian() self._polar_to_cartesian()
self.cartesian_needs_update = False self.cartesian_needs_update = False
pad_matrix = np.float32([[1, 0, self.img_pos[X]], pad_matrix = np.float32([[1, 0, self.img_pos[X]], [0, 1, self.img_pos[Y]], [0, 0, 1]])
[0, 1, self.img_pos[Y]],
[0, 0, 1]])
rotation_matrix_padded = cv2.getRotationMatrix2D( rotation_matrix_padded = cv2.getRotationMatrix2D(
self.roi_image_center, self.config.rotation_angle, 1 self.roi_image_center, self.config.rotation_angle, 1
) )
@ -609,7 +581,7 @@ class CameraWidget:
item = spawn_item(color) item = spawn_item(color)
graph._TKCanvas2.itemconfig(item, dash=(pixel_duty, 8 - pixel_duty), dashoffset=dashoffset) graph._TKCanvas2.itemconfig(item, dash=(pixel_duty, 8 - pixel_duty), dashoffset=dashoffset)
if (self.xy0 is None or self.xy1 is None): if self.xy0 is None or self.xy1 is None:
# roi_window rotates around roi center, we rotate around image center # roi_window rotates around roi center, we rotate around image center
# TODO: it would be nice if they were more consistent # TODO: it would be nice if they were more consistent
roi_window_pos = (self.config.roi_window_x, self.config.roi_window_y) roi_window_pos = (self.config.roi_window_x, self.config.roi_window_y)
@ -623,16 +595,25 @@ class CameraWidget:
style = {} style = {}
if self.is_mouse_up: if self.is_mouse_up:
style = {"dark": "#7f78ff", "light": "#d002ff", "duty": 0.5} style = {"dark": "#7f78ff", "light": "#d002ff", "duty": 0.5}
make_dashed(lambda color: graph.draw_rectangle( make_dashed(
self.xy0, self.xy1, line_color=color, lambda color: graph.draw_rectangle(
), **style) self.xy0,
self.xy1,
line_color=color,
),
**style,
)
if self.is_mouse_up and self.hover_pos is not None: if self.is_mouse_up and self.hover_pos is not None:
make_dashed(lambda color: graph.draw_line( make_dashed(
lambda color: graph.draw_line(
(self.hover_pos[X], 0), (self.hover_pos[X], self.padded_size[Y]), color=color (self.hover_pos[X], 0), (self.hover_pos[X], self.padded_size[Y]), color=color
)) )
make_dashed(lambda color: graph.draw_line( )
make_dashed(
lambda color: graph.draw_line(
(0, self.hover_pos[Y]), (self.padded_size[X], self.hover_pos[Y]), color=color (0, self.hover_pos[Y]), (self.padded_size[X], self.hover_pos[Y]), color=color
)) )
)
except Empty: except Empty:
pass pass
@ -653,9 +634,7 @@ class CameraWidget:
graph = window[self.gui_output_graph] graph = window[self.gui_output_graph]
graph.erase() graph.erase()
if ( if eye_info.info_type != EyeInfoOrigin.FAILURE: # and not eye_info.blink:
eye_info.info_type != EyeInfoOrigin.FAILURE
): # and not eye_info.blink:
graph.update(background_color="white") graph.update(background_color="white")
if not np.isnan(eye_info.x) and not np.isnan(eye_info.y): if not np.isnan(eye_info.x) and not np.isnan(eye_info.y):
@ -682,9 +661,7 @@ class CameraWidget:
) )
else: else:
graph.draw_line( graph.draw_line((-100, 0.5 * 200), (-100, 100), color="#6f4ca1", width=10)
(-100, 0.5 * 200), (-100, 100), color="#6f4ca1", width=10
)
if eye_info.blink <= 0.0: if eye_info.blink <= 0.0:
graph.update(background_color="#6f4ca1") graph.update(background_color="#6f4ca1")
@ -696,8 +673,10 @@ class CameraWidget:
pass pass
else: else:
def back(*args): def back(*args):
pass pass
try: try:
window[self.gui_roi_message].update(visible=False) window[self.gui_roi_message].update(visible=False)
window[self.gui_output_graph].update(visible=False) window[self.gui_output_graph].update(visible=False)