ICEI-PUC-Minas-PPC-CC
/
ppc-cc-2024-2-p7-vcrm-projeto-controle-do-mouse-com-deteccao-de-gesto
Public
forked from ICEI-PUC-Minas-PPC-CC/ppc-cc-2024-2-p7-vcrm-ppc-cc-2024-2-p7-vcrm-projeto-Disciplinas-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
104 lines (79 loc) · 3.45 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import cv2 as cv
import jsonpickle
import numpy as np
from pynput.mouse import Button, Controller
import requests
from src.configs import Configs
from src.constants import EXIT_KEY_CODE, WEBCAM_INDEX
from src.countdown_timer import CountdownTimer
from src.vector import Vector
API_URL = 'http://localhost:5000'
def main():
webcam = cv.VideoCapture(WEBCAM_INDEX)
configs = Configs()
previous_finger_position = Vector(0, 0)
mouse_controller = Controller()
left_click_countdown = CountdownTimer(configs.left_click_delay, lambda: mouse_controller.click(Button.left))
right_click_countdown = CountdownTimer(configs.right_click_delay, lambda: mouse_controller.click(Button.right))
while webcam.isOpened():
status, frame = webcam.read()
if not status or cv.waitKey(1) == EXIT_KEY_CODE:
break
_, image_encoded = cv.imencode('.jpg', frame)
request_body = {
'captured_image': image_encoded.tobytes(),
'configs': {
'cursor_sensitivity': configs.cursor_sensitivity,
'left_click_max_distance': configs.left_click_max_distance,
'left_click_delay': configs.left_click_delay,
'right_click_max_distance': configs.right_click_max_distance,
'right_click_delay': configs.right_click_delay,
'show_landmarkers': configs.show_landmarkers,
'show_delay_bar': configs.show_delay_bar,
},
'previous_finger_position': {
'x': previous_finger_position.x,
'y': previous_finger_position.y,
},
'mouse_click': {
'left': {
'remaining_time': left_click_countdown.get_remaining_time(),
'total_time': left_click_countdown.get_total_time(),
},
'right': {
'remaining_time': right_click_countdown.get_remaining_time(),
'total_time': right_click_countdown.get_total_time(),
},
},
}
response = requests.post(
API_URL,
data=jsonpickle.encode(request_body),
headers={'Content-Type': 'image/jpeg'})
json = jsonpickle.decode(response.text)
processed_image = json['processed_image']
previous_finger_position = json['previous_finger_position']
mouse_move = json['mouse_move']
mouse_click_left = json['mouse_click']['left']
mouse_click_right = json['mouse_click']['right']
# Show processed image
image_array = np.frombuffer(processed_image, np.uint8)
frame = cv.imdecode(image_array, cv.IMREAD_COLOR)
cv.imshow('Processed image', frame)
# Update previous_finger_position
previous_finger_position = Vector(previous_finger_position['x'], previous_finger_position['y'])
# Move mouse
if mouse_move['dx'] != 0 or mouse_move['dy'] != 0:
mouse_controller.move(mouse_move['dx'], mouse_move['dy'])
# Click left mouse
if mouse_click_left['should_cancel']:
left_click_countdown.stop()
elif mouse_click_left['should_start']:
left_click_countdown.start()
# Click right mouse
if mouse_click_right['should_cancel']:
right_click_countdown.stop()
elif mouse_click_right['should_start']:
right_click_countdown.start()
if __name__ == '__main__':
main()