Skip to content

Commit f6d77ab

Browse files
committed
fix: improve mouse movement.
upon start of the program, move the mouse cursor to the center of the screen because the it give a better refrence for the mouse movements.
1 parent 87942d0 commit f6d77ab

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"python": "D:/ahar/Computer-Pointer-Controller-with-Eyes/.venv/Scripts/python.exe",
1414
"args": [
1515
"-i",
16-
"bin/face.png",
16+
// "bin/face.png", // for single image
17+
"CAM",
1718
"-ftm",
1819
"bin/models/face-detection-retail-0004.xml",
1920
"-ldm",

lib/mouse_controller.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
"""
2-
This is a sample class that you can use to control the mouse pointer.
3-
It uses the pyautogui library. You can set the precision for mouse movement
4-
(how much the mouse moves) and the speed (how fast it moves) by changing
5-
precision_dict and speed_dict.
6-
Calling the move function with the x and y output of the gaze estimation model
7-
will move the pointer.
8-
This class is provided to help get you started; you can choose whether you want to use it or create your own from scratch.
2+
Mouse controller utilities built on top of pyautogui.
3+
4+
Provides a lightweight MouseController class to drive the system pointer using
5+
relative movements that are scaled by a precision factor and animated over a
6+
configurable duration. This is useful for driving the pointer from gaze- or
7+
pose-estimation outputs where you receive small x, y offsets.
8+
9+
Constructor presets (string keys accepted):
10+
- precision: "high" (100), "medium" (500), "low" (1000)
11+
- speed: "fast" (1), "medium" (5), "slow" (10)
12+
13+
API summary:
14+
- MouseController.move(x, y)
15+
Move the pointer by (x * precision, -y * precision). Note the y value is
16+
negated internally to accommodate screen coordinate orientation.
17+
- MouseController.move_to_center()
18+
Move the pointer to the center of the primary screen using the same scaling.
19+
20+
Notes and requirements:
21+
- This module disables pyautogui.FAILSAFE by default. Re-enable it if you want
22+
the corner-escape behavior.
23+
- Install pyautogui before use.
24+
- Interpret x and y according to your gaze model (e.g., normalized offsets,
25+
pixel deltas, etc.) and tune precision/speed presets accordingly.
26+
27+
Example:
28+
mc = MouseController(precision="medium", speed="fast")
29+
mc.move(0.1, -0.05) # relative movement scaled by precision
930
"""
1031

1132
import pyautogui
@@ -25,3 +46,16 @@ def move(self, x, y):
2546
pyautogui.moveRel(
2647
x * self.precision, -1 * y * self.precision, duration=self.speed
2748
)
49+
50+
def move_to_center(self):
51+
w, h = pyautogui.size()
52+
cx, cy = w // 2, h // 2
53+
cur_x, cur_y = pyautogui.position()
54+
55+
# required inputs for mc.move
56+
dx = (cx - cur_x) / self.precision
57+
dy = (
58+
cur_y - cy
59+
) / self.precision # invert because MouseController.move negates y internally
60+
61+
self.move(dx, dy)

main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ def rectange_eyes(frame, face_coords, eye_coords):
260260

261261
print("New mouse coordinates: {}\n\n".format(mouse_coords))
262262
### Move Mouse
263-
mouse_controler = MouseController("medium", "fast")
264263
mouse_controler.move(mouse_coords[0], mouse_coords[1])
265264
# go to next frame
266265

0 commit comments

Comments
 (0)