-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame1.py
More file actions
122 lines (85 loc) · 3.96 KB
/
game1.py
File metadata and controls
122 lines (85 loc) · 3.96 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
import pickle
import cv2
import cvzone
import numpy as np
from cvzone.HandTrackingModule import HandDetector
######################################
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)
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 create_overlay_image(polygons, warped_point, imgOverlay):
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:
cv2.polylines(imgOverlay, [np.array(polygon)], isClosed=True, color=(0, 255, 0), thickness=2)
cv2.fillPoly(imgOverlay, [np.array(polygon)], (0, 255, 0))
cvzone.putTextRect(imgOverlay, name, polygon[0], scale=1, thickness=1)
cvzone.putTextRect(imgOverlay, name, (0, 100), scale=8, thickness=5)
return imgOverlay
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
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)
if warped_point:
imgOverlay = create_overlay_image(polygons, warped_point, imgOverlay)
imgOutput = inverse_warp_image(img, imgOverlay, map_points)
cv2.imshow("Output Image", imgOutput)
key = cv2.waitKey(1)