-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (64 loc) · 2.35 KB
/
main.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
import random
import cv2, math, time
import cvzone
import numpy as np
from cvzone.HandTrackingModule import HandDetector
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
#Cam
capture = cv2.VideoCapture(0)
capture.set(3, 1280)
capture.set(4, 720)
detector = HandDetector(detectionCon=0.8, maxHands=1)
x = [300, 245, 200, 170, 145, 130, 112, 103, 93, 87, 80, 75, 70, 67, 62, 59, 57]
y = []
x = np.reshape(x, (-1,1))
for i in range(20, 105, 5):
y.append(i)
poly_regs = PolynomialFeatures(degree=2)
x_poly = poly_regs.fit_transform(x)
lin_reg = LinearRegression()
lin_reg.fit(x_poly, y)
cx, cy = 200, 200
color = (255, 0, 255)
count = 0
points = 0
initial_time = time.time()
while True:
aux, image = capture.read()
hands = detector.findHands(image, draw=False)
if time.time() - initial_time < 30:
if hands:
hand_points = hands[0]['lmList']
x, y, w, h = hands[0]['bbox']
x1 = hand_points[5][0]
y1 = hand_points[5][1]
x2 = hand_points[17][0]
y2 = hand_points[17][1]
distance = math.sqrt(abs(x2-x1)**2 + abs(y2-y1)**2)
distance_poly = poly_regs.fit_transform([[distance]])
cm = lin_reg.predict(distance_poly)
print(distance, " : ", cm)
if cm < 35 and (x < cx < x + w) and (y < cy < y + h):
count = 1
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
cvzone.putTextRect(image, f'{int(cm)} cm', (x+5, y-10))
cv2.circle(image, (cx, cy), 30, color, cv2.FILLED)
cv2.circle(image, (cx, cy), 10, (0, 0, 0), cv2.FILLED)
cv2.circle(image, (cx, cy), 20, (0, 0, 0), 2)
cv2.circle(image, (cx, cy), 30, (255, 255, 255), 3)
if count:
count += 1
color = (0, 255, 0)
if count == 3:
cx = random.randint(100, 1100)
cy = random.randint(100, 600)
color = (255, 0, 255)
count = 0
points += 1
cvzone.putTextRect(image, f'Time: {30 - (int(time.time() - initial_time))}', (45, 650), scale=3, offset=25)
cvzone.putTextRect(image, f'Points: {points}', (1000, 75), scale=3, offset=25)
else:
pass
cv2.imshow("Hand Detector", image)
cv2.waitKey(1)