Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (51 loc) · 1.83 KB
/
Copy pathmain.py
File metadata and controls
67 lines (51 loc) · 1.83 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
import cv2
from cvzone.HandTrackingModule import HandDetector
import button
cap = cv2.VideoCapture(0)
cap.set(3, 1280) # Set width
cap.set(4, 720) # Set height
buttonListValues = [["7", "8", "9", "*"],
["4", "5", "6", "-"],
["1", "2", "3", "+"],
["0", "/", ".", "="]]
buttonList = []
for i in range(4):
for j in range(4):
xpos = i * 100 + 800
ypos = j * 100 + 200
buttonList.append(button.Button(
(xpos, ypos), 100, 100, buttonListValues[j][i]))
detector = HandDetector(detectionCon=0.8, maxHands=1)
calculator = ""
delayCounter = 0
while True:
success, frame = cap.read()
frame = cv2.flip(frame, 1)
hands, frame = detector.findHands(frame, flipType=False)
cv2.rectangle(frame, (800, 200), (1200, 100), (255, 255, 255), cv2.FILLED)
cv2.rectangle(frame, (800, 200), (1200, 100), (0, 0, 0), cv2.LINE_4)
for button in buttonList:
button.draw(frame)
if hands:
lmList = hands[0]['lmList']
length, _, frame = detector.findDistance(
lmList[4][:2], lmList[8][:2], frame)
if length < 50:
x, y = lmList[8][:2]
for i, button in enumerate(buttonList):
if (button.checkClick(frame, x, y) and delayCounter == 0):
if button.text == "=":
calculator = str(eval(calculator))
else:
calculator += button.text
delayCounter = 1
if delayCounter > 0:
delayCounter += 1
if delayCounter > 10:
delayCounter = 0
cv2.putText(frame, calculator, (820, 140),
cv2.FONT_HERSHEY_PLAIN, 3, (50, 50, 50), 3)
cv2.imshow("Webcam Feed", frame)
key = cv2.waitKey(1)
if key == ord('c'):
calculator = ""