-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkout.py
172 lines (149 loc) · 5.41 KB
/
checkout.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import cv2
from cvzone.HandTrackingModule import HandDetector
import cvzone
import pyautogui
CAM_HEIGHT = 720
CAM_WIDTH = 1280
cap = cv2.VideoCapture(0)
cap.set(3, CAM_WIDTH)
cap.set(4, CAM_HEIGHT)
detector = HandDetector(maxHands=1, detectionCon=0.8)
# PAGES
pagesDict = {
"start": {
"start": (0, 0, CAM_WIDTH, CAM_HEIGHT)
},
"checkout": {
"remove": (1000, 50, 1175, 200),
"loyalty": (800, 225, 975, 375),
"help": (1000, 225, 1175, 375),
"pay": (800, 425, 1175, 675),
"code": (800, 50, 975, 200)
},
"pay": {
"credit": (900, 100, 1150, 250),
"debit": (900, 300, 1150, 450),
"cancel": (900, 500, 1150, 650)
},
"help": {
"Help is on the way": (100, 50, 'not a button', False),
"cancel": (900, 500, 1150, 650)
},
"remove": {
"Please scan the item you would like to remove": (50, 50, 'not a button', False),
"cancel": (900, 500, 1150, 650)
},
"loyalty": {
"Please scan your loyalty card": (100, 50, 'not a button', False),
"cancel": (900, 500, 1150, 650)
},
"code": {
"1": (80, 100, 280, 250),
"2": (400, 100, 600, 250),
"3": (720, 100, 920, 250),
"enter": (1020, 100, 1220, 250),
"4": (80, 300, 280, 450),
"5": (400, 300, 600, 450),
"6": (720, 300, 920, 450),
"back": (1020, 300, 1220, 450),
"7": (80, 500, 280, 650),
"8": (400, 500, 600, 650),
"9": (720, 500, 920, 650),
"0": (1020, 500, 1220, 650)
}
}
pageNum = 0
pages = []
pressed = False
bkey = None
class Page():
def __init__(self, name, buttons):
self.name = name
self.buttons = buttons
def update(self, cursor, pageNum, pressed, bkey):
# if on start page, move to checkout page
if not pageNum:
pressed = False
bkey = None
return 1, pressed, bkey
# if a button has been pressed, check for release
elif pressed:
release_length, release_info, release_img = detector.findDistance(lmList[8], lmList[12], img)
# if button has been released,
if release_length > 60:
print(bkey)
if bkey == "pay":
pyautogui.click(x=140, y=590)
pageNum = 2
elif bkey == "help":
pyautogui.click(x=140, y=490)
pageNum = 3
elif bkey == "remove":
pyautogui.click(x=140, y=380)
pageNum = 4
elif bkey == "loyalty":
pyautogui.click(x=140, y=200)
pageNum = 5
elif bkey == "code":
pyautogui.click(x=140, y=270)
pageNum = 6
else:
pageNum = 1
if bkey == "cancel":
pyautogui.click(x=940, y=58)
pressed = False
bkey = None
return pageNum, pressed, bkey
# check each button to see if it has been pressed
for name in self.buttons:
x1, y1, x2, y2 = self.buttons[name]
# only consider buttons for updating pages
if x2 != 'not a button':
# if button has been pressed, turn it green, save the button name and set button pressed status to true
if x1 < cursor[0] < x2 and y1 < cursor[1] < y2:
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), cv2.FILLED)
bkey = name
pressed = True
return pageNum, pressed, bkey
for key in pagesDict:
pages.append(Page(key, pagesDict[key]))
while cap.isOpened:
success, img = cap.read()
img = cv2.flip(img, 1)
hands, img = detector.findHands(img, flipType=False)
page = pages[pageNum]
for key in page.buttons:
x1, y1, x2, y2 = page.buttons[key]
# behaviour for buttons (in blue)
if x2 != 'not a button':
img = cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), -1)
img = cv2.putText(img, key, ((x1 + x2) // 2 - 50, (y1 + y2) // 2), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
2)
else:
# behaviour for text boxes with information (in magenta)
cvzone.putTextRect(img, key, [x1, y1], 3, 4)
if hands:
lmList = hands[0]['lmList']
cursor = lmList[8]
# Palm Gesture to Start Checkout
if pageNum == 0:
pairs = [(5, 8), (9, 12), (13, 16), (17, 20), (1, 4), (0, 9)]
lengths = []
for pair in pairs:
length, info, img = detector.findDistance(lmList[pair[0]], lmList[pair[1]], img)
lengths.append(length)
if all(length>100 for length in lengths):
pageNum, pressed, bkey = page.update(cursor, pageNum, pressed, bkey)
pyautogui.click(x=950, y=50)
# Click Gesture
else:
length, info, img = detector.findDistance(lmList[8], lmList[12], img)
if length < 60:
pageNum, pressed, bkey = page.update(cursor, pageNum, pressed, bkey)
elif pressed:
pageNum, pressed, bkey = page.update(cursor, pageNum, pressed, bkey)
cv2.imshow("Img", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()