Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IT Management Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Formanek Balázs István
IT Management Project
Commits
7bb4bc58
Commit
7bb4bc58
authored
6 months ago
by
Vajay Mónika
Browse files
Options
Downloads
Patches
Plain Diff
divide into classes
parent
9913fe28
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
final_project/hand_detection.py
+23
-0
23 additions, 0 deletions
final_project/hand_detection.py
final_project/main.py
+110
-0
110 additions, 0 deletions
final_project/main.py
final_project/mouse_class.py
+101
-0
101 additions, 0 deletions
final_project/mouse_class.py
with
234 additions
and
0 deletions
final_project/hand_detection.py
0 → 100644
+
23
−
0
View file @
7bb4bc58
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
This diff is collapsed.
Click to expand it.
final_project/
control_mouse
.py
→
final_project/
main
.py
+
110
−
0
View file @
7bb4bc58
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
final_project/mouse_class.py
0 → 100644
+
101
−
0
View file @
7bb4bc58
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment