-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialintegersend.py
50 lines (42 loc) · 1.72 KB
/
serialintegersend.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
import cv2
import mediapipe as mp
import time
import math
import serial
# Open serial connection to Arduino
ser = serial.Serial("COM3", 9600) # Replace 'COMX' with the appropriate port
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
currenttime = 0
prevtime = 0
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
h, w, c = img.shape
if id == 8:
x1, y1 = int(lm.x * w), int(lm.y * h)
cv2.circle(img, (x1, y1), 1, (255, 255, 0), cv2.FILLED)
if id == 0:
x2, y2 = int(lm.x * w), int(lm.y * h)
cv2.circle(img, (x2, y2), 1, (255, 255, 0), cv2.FILLED)
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 1)
length = math.hypot(x2 - x1, y2 - y1)
cv2.putText(img, str(int(length)), (500, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 2)
# Convert the integer 'length' to bytes and send it with a newline character
ser.write(str(int(length)).encode() + b'\n')
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
currenttime = time.time()
fps = 1 / (currenttime - prevtime)
prevtime = currenttime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 2)
cv2.imshow("image ", img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()