-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame3.py
More file actions
209 lines (149 loc) · 6.91 KB
/
game3.py
File metadata and controls
209 lines (149 loc) · 6.91 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pickle
import cv2
import cvzone
import numpy as np
from cvzone.HandTrackingModule import HandDetector
import time
######################################
cam_id = 1
width, height = 1920, 1080
map_file_path = "D:\python programs\interactive_map\map.p"
countries_file_path = "D:\python programs\interactive_map\countries.p"
######################################
file_obj = open(map_file_path, 'rb')
map_points = pickle.load(file_obj)
file_obj.close()
print(f"Loaded map coordinates.")
if countries_file_path:
file_obj = open(countries_file_path, 'rb')
polygons = pickle.load(file_obj)
file_obj.close()
print(f"Loaded {len(polygons)} countries.")
else:
polygons = []
cap = cv2.VideoCapture(cam_id)
cap.set(3, width)
cap.set(4, height)
counter = 0
detector = HandDetector(staticMode=False,
maxHands=1,
modelComplexity=1,
detectionCon=0.5,
minTrackCon=0.5)
questions = [["Which country has the largest area?", "Russia"],
["Where does the famous La Tomatina Festival take place?", "Spain"],
["Which country is a continent?", "Australia"],
["Which country has the largest statue ?", "India"],
["Which country 2028 Olympic Games will be held?", "USA"],
["Which country is called the Land of Rising Sun?", "Japan"],
]
selected_country = None
country_entry_times = {}
counter_country = 0
counter_answer = 0
current_question = 0
start_counter = False
answer_color = (0, 0, 255)
total_score = 0
def warp_image(img, points, size=[1600,800]):
pts1 = np.float32([points[0], points[1], points[2], points[3]])
pts2 = np.float32([[0, 0], [size[0], 0], [0, size[1]], [size[0], size[1]]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
imgOutput = cv2.warpPerspective(img, matrix, (size[0], size[1]))
return imgOutput, matrix
def warp_single_point(point, matrix):
point_homogeneous = np.array([[point[0], point[1], 1]], dtype=np.float32)
point_homogeneous_transformed = np.dot(matrix, point_homogeneous.T).T
point_warped = point_homogeneous_transformed[0, :2] / point_homogeneous_transformed[0, 2]
return point_warped
def get_finger_location(img,imgWarped):
hands, img = detector.findHands(img, draw=False, flipType=True)
if hands:
hand1 = hands[0]
indexFinger = hand1["lmList"][8][0:2]
# cv2.circle(img,indexFinger,5,(255,0,255),cv2.FILLED)
warped_point = warp_single_point(indexFinger, matrix)
warped_point = int(warped_point[0]), int(warped_point[1])
print(indexFinger,warped_point)
cv2.circle(imgWarped, warped_point, 5, (255, 0, 0), cv2.FILLED)
else:
warped_point = None
return warped_point
def inverse_warp_image(img, imgOverlay, map_points):
map_points = np.array(map_points, dtype=np.float32)
destination_points = np.array([[0, 0], [imgOverlay.shape[1] - 1, 0], [0, imgOverlay.shape[0] - 1],
[imgOverlay.shape[1] - 1, imgOverlay.shape[0] - 1]], dtype=np.float32)
M = cv2.getPerspectiveTransform(destination_points, map_points)
warped_overlay = cv2.warpPerspective(imgOverlay, M, (img.shape[1], img.shape[0]))
result = cv2.addWeighted(img, 1, warped_overlay, 0.65, 0, warped_overlay)
return result
def create_overlay_image(polygons, warped_point, imgOverlay):
country_selected = None
green_duration_threshold = 2.0
for polygon, name in polygons:
polygon_np = np.array(polygon, np.int32).reshape((-1, 1, 2))
result = cv2.pointPolygonTest(polygon_np, warped_point, False)
if result >= 0:
if name not in country_entry_times:
country_entry_times[name] = time.time()
time_in_country = time.time() - country_entry_times[name]
if time_in_country >= green_duration_threshold:
color = (0, 255, 0)
country_selected = name
else:
country_selected = None
color = (255, 0, 255)
angle = int((time_in_country / green_duration_threshold) * 360)
cv2.ellipse(imgOverlay, (warped_point[0], warped_point[1] - 100),
(50, 50), 0, 0, angle, color,
thickness=-1)
cv2.polylines(imgOverlay, [np.array(polygon)], isClosed=True, color=color, thickness=2)
cv2.fillPoly(imgOverlay, [np.array(polygon)], color)
cvzone.putTextRect(imgOverlay, name, polygon[0], scale=1, thickness=1)
cvzone.putTextRect(imgOverlay, name, (0, 100), scale=8, thickness=5)
else:
country_entry_times.pop(name, None)
return imgOverlay, country_selected
def check_answer(name, current_question, img, total_score):
global counter_answer, start_counter, answer_color
if current_question == len(questions):
cvzone.putTextRect(img, f"Your score is {total_score}/{len(questions)}", (620, 410), scale=5, thickness=5)
return current_question, total_score
if name != None:
if name == questions[current_question][1]:
start_counter = 'CORRECT'
answer_color = (0, 255, 0)
else:
start_counter = 'WRONG'
answer_color = (0, 0, 255)
if start_counter:
counter_answer += 1
if counter_answer != 0:
cvzone.putTextRect(img, start_counter, (800, 500), colorR=answer_color)
if counter_answer == 70:
counter_answer = 0
current_question += 1
if start_counter == "CORRECT":
total_score += 1
start_counter = False
return current_question, total_score
while True:
success, img = cap.read()
imgWarped, matrix = warp_image(img, map_points)
imgOutput = img.copy()
warped_point = get_finger_location(img,imgWarped)
h, w, _ = imgWarped.shape
imgOverlay = np.zeros((h, w, 3), dtype=np.uint8)
selected_country = None
if warped_point:
imgOverlay, selected_country = create_overlay_image(polygons, warped_point, imgOverlay)
imgOutput = inverse_warp_image(img, imgOverlay, map_points)
if current_question != len(questions):
cvzone.putTextRect(imgOutput, questions[current_question][0], (0, 100))
current_question, total_score = check_answer(selected_country, current_question, imgOutput, total_score)
# imgStacked = cvzone.stackImages([img, imgWarped,imgOutput,imgOverlay], 2, 0.3)
# cv2.imshow("Stacked Image", imgStacked)
# cv2.imshow("Original Image", img)
# cv2.imshow("Warped Image", imgWarped)
cv2.imshow("Output Image", imgOutput)
key = cv2.waitKey(1)