diff --git a/final_project/main.py b/final_project/main.py index 3b61168348cdd8c1b55acfe2c278c81f14cb073b..86adc1b6cf00094f4e13069854d9e748a8c3c5ac 100644 --- a/final_project/main.py +++ b/final_project/main.py @@ -47,8 +47,8 @@ def main(): screen_height = root.winfo_screenheight() # Define window size and position (e.g., 320x240 window at bottom-right corner) - window_width = 160 - window_height = 120 + window_width = 160*4 + window_height = 120*4 x_position = screen_width - window_width - 10 # 10px margin from the right y_position = screen_height - window_height - 70 # 50px margin from the bottom @@ -95,13 +95,21 @@ def main(): # apply model pred = model.predict(np.asarray(normalised_landmark_list).reshape(1, -1)) mouse_command = pred[0] + hand_size = landmark_list[0][0] - landmark_list[12][0], landmark_list[0][1] - landmark_list[12][1] cv2.putText( - img=frameRGB, text=pred[0], org=(30, 30), - fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, color=(255, 0, 0), thickness=1 + img=frameRGB, + text=f"{pred[0]} pos {landmark_list[8][0]:.2f}, {landmark_list[8][1]:.2f}", + org=(30, 30), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, color=(255, 0, 0), thickness=1 + ) + cv2.putText( + img=frameRGB, + text=f"hand size: {hand_size[0]:.2f}, {hand_size[1]:.2f}", + org=(30, 60), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, color=(0, 255, 0), thickness=1 ) mouse.add_prediction(mouse_command) if mouse_command == "move cursor" or "grab": + mouse.get_hand_size(landmark_list[12], landmark_list[0]) mouse.get_hand_pos(landmark_list[8]) # Convert frame to Tkinter-compatible format and display diff --git a/final_project/mouse_class.py b/final_project/mouse_class.py index fdb9bbadbba3a85aef18221b79d2a45dea7cc377..1a205286da65d40e9a91abc3e3dd0a1e34e31cc6 100644 --- a/final_project/mouse_class.py +++ b/final_project/mouse_class.py @@ -5,6 +5,9 @@ from screeninfo import get_monitors MONITOR = get_monitors()[0] WIDTH, HEIGHT = MONITOR.width, MONITOR.height +SIDE_MARGIN = 0.06 +BOTTOM_MARGIN = 0.6 +DEFAULT_HAND_SIZE = 0.5 class Mouse: def __init__(self) -> None: @@ -12,24 +15,28 @@ class Mouse: self.predictions = [] self.previous_action = None self.freeze_action = False + self.stop_pos = None + self.hand_size = None # parameters to fine-tune - self.action_length = 15 - #self.move_distance = 10 + self.action_length = 5 self.scroll_distance = 50 - #self.time_checking = 0.5 - self.stop_pos = None + def get_hand_pos(self, hand_pos): self.hand_pos_x = hand_pos[0] self.hand_pos_y = hand_pos[1] + #print(f"{self.hand_pos_x}, {self.hand_pos_y}") + self.resize_coordinates() + #print(f"{self.hand_pos_x}, {self.hand_pos_y} \n") + def add_prediction(self, prediction): self.predictions.append(prediction) if len(self.predictions) == self.action_length: self.make_action() elif self.previous_action in {"move cursor", "scrolling up", "scrolling down", "scrolling left", "scrolling right"}: - if len(self.predictions) > 3: + if len(self.predictions) > 1: safe_action = self.get_major_element(self.predictions[-3:]) if safe_action == self.previous_action: self.mouse_control(self.previous_action) @@ -50,8 +57,6 @@ class Mouse: self.freeze_action = action in {"left click", "right click", "double click"} # maybe change to keyboard and drops - def mouse_hand_parameters(self): - pass def mouse_control(self, prediction): if prediction == "stop execution" or None: @@ -106,3 +111,14 @@ class Mouse: major_element, _ = counts.most_common(1)[0] return major_element + + def resize_coordinates(self): + max_x = 1 - 2 * SIDE_MARGIN + self.hand_pos_x = (self.hand_pos_x-SIDE_MARGIN) / (max_x - SIDE_MARGIN) #* DEFAULT_HAND_SIZE/self.hand_size + self.hand_pos_y = (self.hand_pos_y-SIDE_MARGIN) / (BOTTOM_MARGIN - SIDE_MARGIN) #* DEFAULT_HAND_SIZE/self.hand_size + + def get_hand_size(self, middle_tip_coord, palm_coord): + self.hand_size = palm_coord[1] - middle_tip_coord[1] + + +