forked from phospho-app/phosphobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_hand.py
More file actions
85 lines (68 loc) · 1.88 KB
/
Copy pathwave_hand.py
File metadata and controls
85 lines (68 loc) · 1.88 KB
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
import cv2
import sys
import time
import signal
import requests
import mediapipe as mp # type: ignore
# Configurations
PI_IP: str = "127.0.0.1"
PI_PORT: int = 80
# Initialize MediaPipe Hand tracking
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7
)
# Handle Ctrl+C to exit the program gracefully
def signal_handler(sig, frame):
print("\nExiting gracefully...")
cap.release()
cv2.destroyAllWindows()
hands.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Function to call the API
def call_to_api(endpoint: str, data: dict = {}):
response = requests.post(f"http://{PI_IP}:{PI_PORT}/move/{endpoint}", json=data)
return response.json()
def wave_motion():
points = 5
for _ in range(2):
for i in range(points):
call_to_api(
"absolute",
{
"x": 0,
"y": 2 * (-1) ** i,
"z": 0,
"rx": -90,
"ry": 0,
"rz": 0,
"open": i % 2 == 0,
},
)
time.sleep(0.2)
call_to_api("init")
cap = cv2.VideoCapture(0)
last_wave_time: float = 0
WAVE_COOLDOWN = 3
try:
while True:
success, image = cap.read()
if not success:
continue
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
current_time = time.time()
if (
results.multi_hand_landmarks
and current_time - last_wave_time > WAVE_COOLDOWN
):
wave_motion()
last_wave_time = current_time
cv2.imshow("Hand Detection", image)
cv2.waitKey(1)
except KeyboardInterrupt:
print("\nExiting gracefully...")
finally:
cap.release()
cv2.destroyAllWindows()
hands.close()