Skip to content
Snippets Groups Projects
Commit 7bb4bc58 authored by Vajay Mónika's avatar Vajay Mónika
Browse files

divide into classes

parent 9913fe28
Branches
No related tags found
No related merge requests found
def normalise_landmarks(landmark_list):
if len(landmark_list) == 0:
return landmark_list
x = [lm[0] for lm in landmark_list]
y = [lm[1] for lm in landmark_list]
min_x = min(x)
max_x = max(x)
min_y = min(y)
max_y = max(y)
normalised_landmarks = []
for lm in landmark_list:
x_norm = (lm[0] - min_x) / (max_x - min_x)
y_norm = (lm[1] - min_y) / (max_y - min_y)
lm_norm = (x_norm, y_norm)
normalised_landmarks.append(lm_norm)
return normalised_landmarks
\ No newline at end of file
......@@ -4,130 +4,12 @@ import mediapipe as mp
import pickle
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import pyautogui
import time
from collections import Counter
from screeninfo import get_monitors
import os
MONITOR = get_monitors()[0]
WIDTH, HEIGHT = MONITOR.width, MONITOR.height
class Mouse:
def __init__(self) -> None:
self.predictions = []
self.previous_action = None
self.freeze_action = False
# parameters to fine-tune
self.action_length = 5
#self.move_distance = 10
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]
def add_prediction(self, prediction):
self.predictions.append(prediction)
if len(self.predictions) == self.action_length:
self.make_action()
def make_action(self):
action = self.get_major_element(self.predictions)
if self.freeze_action and action == self.previous_action:
self.update_init(action)
else:
self.mouse_control(action)
self.update_init(action)
def update_init(self, action):
self.predictions = []
self.previous_action = action
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:
pass # Stop movement
elif prediction == "move cursor":
#hand_point = ([int(self.hand_pos_x*WIDTH), int(self.hand_pos_y*HEIGHT)])
hand_x = np.clip(int(self.hand_pos_x*WIDTH), 0, WIDTH-1)
hand_y = np.clip(int(self.hand_pos_y*HEIGHT), 0, HEIGHT-1)
pyautogui.moveTo(hand_x, hand_y)
elif prediction == "stop moving":
pyautogui.move(0, 0) # Stop cursor
self.stop_pos = pyautogui.position()
elif prediction == "left click":
pyautogui.click() # Left click
elif prediction == "right click":
pyautogui.click(button='right') # Right click
elif prediction == "double click":
pyautogui.click(clicks=2) # Double click
elif prediction == "scrolling up":
pyautogui.scroll(self.scroll_distance) # Scroll up
elif prediction == "scrolling down":
pyautogui.scroll(-self.scroll_distance) # Scroll down
elif prediction == "scrolling right":
pyautogui.hscroll(self.scroll_distance) # Scroll right
# THIS FUNCTION NOT WORKS ON WINDOWS
elif prediction == "scrolling left":
pyautogui.hscroll(self.scroll_distance) # Scroll left
# THIS FUNCTION NOT WORKS ON WINDOWS
elif prediction == "drag":
if self.previous_action == "stop moving":
pyautogui.moveTo(*self.stop_pos)
pyautogui.mouseDown()
hand_x = np.clip(int(self.hand_pos_x*WIDTH), 0, WIDTH-1)
hand_y = np.clip(int(self.hand_pos_y*HEIGHT), 0, HEIGHT-1)
pyautogui.moveTo(hand_x, hand_y)
elif prediction == "drop":
pyautogui.mouseUp()
elif prediction == "multiple item selection grab":
pyautogui.mouseDown()
elif prediction == "multiple item selection drop":
pyautogui.mouseUp()
elif prediction == "change to keyboard":
pass
#time.sleep(self.time_checking) # Adjust speed of movement
def get_major_element(self, string_list):
counts = Counter(string_list)
# Find the element with the maximum count
major_element, _ = counts.most_common(1)[0]
return major_element
def normalise_landmarks(landmark_list):
if len(landmark_list) == 0:
return landmark_list
x = [lm[0] for lm in landmark_list]
y = [lm[1] for lm in landmark_list]
min_x = min(x)
max_x = max(x)
min_y = min(y)
max_y = max(y)
normalised_landmarks = []
for lm in landmark_list:
x_norm = (lm[0] - min_x) / (max_x - min_x)
y_norm = (lm[1] - min_y) / (max_y - min_y)
lm_norm = (x_norm, y_norm)
normalised_landmarks.append(lm_norm)
return normalised_landmarks
from mouse_class import Mouse
from hand_detection import normalise_landmarks
## main: open video and do hand detection
def main():
......@@ -135,7 +17,9 @@ def main():
mouse = Mouse()
# load model
model_dict = pickle.load(open('./trained_Moni_data.p', 'rb'))
current_dir = os.path.dirname(__file__)
model_path = os.path.abspath(os.path.join(current_dir, os.pardir, 'trained_models', 'trained_Moni_data.p'))
model_dict = pickle.load(open(model_path, 'rb'))
model = model_dict['model']
# create hand detection object
......
import numpy as np
import pyautogui
from collections import Counter
from screeninfo import get_monitors
MONITOR = get_monitors()[0]
WIDTH, HEIGHT = MONITOR.width, MONITOR.height
class Mouse:
def __init__(self) -> None:
self.predictions = []
self.previous_action = None
self.freeze_action = False
# parameters to fine-tune
self.action_length = 5
#self.move_distance = 10
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]
def add_prediction(self, prediction):
self.predictions.append(prediction)
if len(self.predictions) == self.action_length:
self.make_action()
def make_action(self):
action = self.get_major_element(self.predictions)
if self.freeze_action and action == self.previous_action:
self.update_init(action)
else:
self.mouse_control(action)
self.update_init(action)
def update_init(self, action):
self.predictions = []
self.previous_action = action
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:
pass # Stop movement
elif prediction == "move cursor":
#hand_point = ([int(self.hand_pos_x*WIDTH), int(self.hand_pos_y*HEIGHT)])
hand_x = np.clip(int(self.hand_pos_x*WIDTH), 0, WIDTH-1)
hand_y = np.clip(int(self.hand_pos_y*HEIGHT), 0, HEIGHT-1)
pyautogui.moveTo(hand_x, hand_y)
elif prediction == "stop moving":
pyautogui.move(0, 0) # Stop cursor
self.stop_pos = pyautogui.position()
elif prediction == "left click":
pyautogui.click() # Left click
elif prediction == "right click":
pyautogui.click(button='right') # Right click
elif prediction == "double click":
pyautogui.click(clicks=2) # Double click
elif prediction == "scrolling up":
pyautogui.scroll(self.scroll_distance) # Scroll up
elif prediction == "scrolling down":
pyautogui.scroll(-self.scroll_distance) # Scroll down
elif prediction == "scrolling right":
pyautogui.hscroll(self.scroll_distance) # Scroll right
# THIS FUNCTION NOT WORKS ON WINDOWS
elif prediction == "scrolling left":
pyautogui.hscroll(self.scroll_distance) # Scroll left
# THIS FUNCTION NOT WORKS ON WINDOWS
elif prediction == "drag":
if self.previous_action == "stop moving":
pyautogui.moveTo(*self.stop_pos)
pyautogui.mouseDown()
hand_x = np.clip(int(self.hand_pos_x*WIDTH), 0, WIDTH-1)
hand_y = np.clip(int(self.hand_pos_y*HEIGHT), 0, HEIGHT-1)
pyautogui.moveTo(hand_x, hand_y)
elif prediction == "drop":
pyautogui.mouseUp()
elif prediction == "multiple item selection grab":
pyautogui.mouseDown()
elif prediction == "multiple item selection drop":
pyautogui.mouseUp()
elif prediction == "change to keyboard":
pass
#time.sleep(self.time_checking) # Adjust speed of movement
def get_major_element(self, string_list):
counts = Counter(string_list)
# Find the element with the maximum count
major_element, _ = counts.most_common(1)[0]
return major_element
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment