-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
117 lines (106 loc) · 4.16 KB
/
calculator.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
105
106
107
108
109
110
111
112
113
114
115
116
117
import cv2
from cvzone.HandTrackingModule import HandDetector
import pyttsx3
class calculator:
def __init__(self, pos, width, height, value):
self.pos = pos
self.width = width
self.height = height
self.value = value
def drawbutton(self, img):
cv2.rectangle(img, self.pos, (self.pos[0] + self.width, self.pos[1] + self.height),
(125, 125, 225), cv2.FILLED)
cv2.rectangle(img, self.pos, (self.pos[0] + self.width, self.pos[1] + self.height),
(50, 50, 50), 3)
cv2.putText(img, self.value, (self.pos[0] + 30, self.pos[1] + 70), cv2.FONT_HERSHEY_PLAIN,
2, (50, 50, 50), 2)
def Click(self, x, y):
if self.pos[0] < x < self.pos[0] + self.width and \
self.pos[1] < y < self.pos[1] + self.height:
cv2.rectangle(img, (self.pos[0] + 3, self.pos[1] + 3),
(self.pos[0] + self.width - 3, self.pos[1] + self.height - 3),
(255, 255, 255), cv2.FILLED)
cv2.putText(img, self.value, (self.pos[0] + 25, self.pos[1] + 80), cv2.FONT_HERSHEY_PLAIN,
5, (0, 0, 0), 5)
return True
else:
return False
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say("please wait! the virtual calculator is starting")
engine.runAndWait()
buttons = [['7', '8', '9', 'C'],
['4', '5', '6', '*'],
['1', '2', '3', '+'],
['0', '-', '/', '='],
['(',')','.','del']]
buttonList = []
for x in range(4):
for y in range(5):
xpos = x * 100 + 700
ypos = y * 100 + 100
buttonList.append(calculator((xpos, ypos), 100, 100, buttons[y][x]))
Equation = ''
Counter = 0
# Webcam
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap.set(3,1280)
cap.set(4,1080)
detector = HandDetector(detectionCon=0.9, maxHands=1)
while True:
success, img = cap.read()
img = cv2.flip(img, 1)
hands,img = detector.findHands(img)
for button in buttonList:
button.drawbutton(img)
# Check for Hand
if hands:
# Find distance between fingers
lmList = hands[0]['lmList']
length, _, img = detector.findDistance(lmList[8], lmList[12], img)
# print(length)
x, y = lmList[8]
# If clicked check which button and perform action
if length < 50 and Counter == 0:
for i, button in enumerate(buttonList):
if button.Click(x, y):
myValue = buttons[int(i % 5)][int(i / 5)] # get correct number
# engine.say(myValue)
# engine.runAndWait()
if myValue == '=':
try:
Equation = str(eval(Equation))
except SyntaxError:
print("Syntax Error")
engine.say("Syntax Error")
engine.runAndWait()
Equation='Syntax Error'
elif Equation=='Syntax Error':
Equation=''
elif myValue == 'C':
Equation =''
elif myValue=='del':
Equation=Equation[:-1]
else:
Equation += myValue
Counter = 1
# to avoid multiple clicks
if Counter != 0:
Counter += 1
if Counter > 10:
Counter = 0
# Final answer
cv2.rectangle(img, (700, 20), (1100, 100),
(175, 125, 155), cv2.FILLED)
cv2.rectangle(img, (700, 20), (1100, 100),
(50, 50, 50), 3)
cv2.putText(img, Equation, (710, 80),cv2.FONT_HERSHEY_PLAIN,
3, (0, 0, 0), 3)
cv2.putText(img, 'VIRTUAL CALCULATOR -->', (50, 50), cv2.FONT_HERSHEY_PLAIN,
3, (0, 0, 0), 3)
cv2.imshow("Virtual Calculator", img)
cv2.moveWindow("Virtual Calculator", 0,0)
#close the webcam
if cv2.waitKey(10) & 0xFF == ord("q"):
break