-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap_combo_v1.py
247 lines (181 loc) · 7.06 KB
/
ap_combo_v1.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 24 01:43:33 2019
@author: ariji
"""
# Importing bassic necessary packages
import cv2 as cv
import numpy as np
import math
from datetime import date
import os
os.chdir("D:/L_Learning/computer vision/project 2/openCV")
# This is a wrapper for openCV basics
import basic_function as bf
# Border detection codes
import ap_angle_border as ad
# --- #
# Importing yolo folder
yolo_path = "D:/L_Learning/computer vision/project 2/openCV/yolo"
os.chdir(yolo_path)
import sys
sys.path.insert(1, yolo_path)
import yolo_people_detection as ypd
#--#
#Changing the working directory back to what was earlier
import os
os.chdir("D:/L_Learning/computer vision/project 2/openCV")
# -- #
frame_no = 71
img = cv.imread("vid2frame/all frames/"+ "frame_v2_" + str(frame_no) + ".png")
bf.show_image(img)
def combo(img, prev_img_pos = "Undefined"):
###Part 1: Tensorflow API people detection, refer to 'tfpeople detection.py'
img2 = cv.resize(img, None, fx = 0.4, fy = 0.4)
people_coordinate = ypd.yolo_people(img2, threshold=0.1, showImage= False)
pedals = people_coordinate[2] # Returning the pedals only
#---#
###Part 2: Adjusted coordinates
# Adjust the coordinate as it was multiplied by 0.4 before yolo
pedals_adj = [(int(x[0]*2.5), int(x[1]*2.5)) for x in pedals]
# Checking whether pedals are properly adjusted
# img3 = img.copy()
# for x in pedals_adj:
# cv.circle(img3, x, 2, (0, 0, 255), 3)
# bf.show_image(img3)
#---#
###Part 3: Border detection and angle detection: refer to 'ap border.py'
img4 = img.copy()
red = ad.angle_detection(img4, m = 1) # Red line (default)
yellow = ad.angle_detection(img4,
lower_mask= [20, 100, 100],
upper_mask = [30, 255, 255], m = 1) #yellow line
blue = ad.angle_detection(img4,
lower_mask=[110,50,50],
upper_mask = [130,255,255], m = 1, st = 65) # blue line
#---#
###Part 4: Angle between the borders
angle = []
for x in [(blue, red), (red, yellow), (yellow, blue)]:
if None not in x:
a = bf.angle(line1= x[0][0][0][0], line2 = x[1][0][0][0])
angle.append(a)
else:
angle.append(None)
ang_blue_red, ang_yel_red, ang_yel_blue = angle[0], angle[1], angle[2]
#---#
###Part 5: Perpendicular distance from red, blue, yellow line of the pedals
# Saving three different distances(with side_of_origin of course!) for each pedal point
dist_red, dist_yel, dist_blue = [], [], []
for points in pedals_adj:
temp = []
for x in (red, yellow, blue):
if x is not None:
temp.append(bf.distance(points, x[0][0][0]))
else:
temp.append((None, None))
dist_red.append(temp[0])
dist_yel.append(temp[1])
dist_blue.append(temp[2])
# Deciding if the upper yellow or the side yellow is captured (Noot doen yet, To be added)
# Removing the audiences (Not final yet: Need to add some filters based on yellow line, if required)
# It has to be automated whether the upper yellow or,
# the right hand side yellow border has been captured in yel
index_final = []
for i in range(len(pedals_adj)):
if red is not None:
if dist_red[i][1] == True and dist_yel[i][1] == False:
index_final.append(i)
else:
pass
else:
if dist_yel[i][1] == False:
index_final.append(i)
#Checking if the audiences are really removed
img5 = img.copy()
for x in index_final:
cv.circle(img5, pedals_adj[x], 2, (0, 0, 255), 3)
cv.putText(img5,str(x), pedals_adj[x], cv.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2)
# bf.show_image(img5)
#---#
###Part 6: Categorizing discrete sections on the field
# The entire field will be divided into three parts.
# Position = L: Left hand side of the field where ang_hor > pi/2
# Position = R: Right hand side
# Position = Undefined
position = "Undefined" #Initialization
if red is None:
if blue is not None:
if blue[1][0] < math.pi/2:
position = "L"
else:
position = "Undefined"
else:
if red[1][0] < math.pi / 2:
position = "R"
else:
position = "L"
if position == "Undefined":
position = prev_img_pos
#---#
###Part 7: SCALING : Mapping pedals and distances on the template rink
# This scaling is very inportant part!
# As ultimately the coordinates depend on how accurate the scaling is.
fin_red = [dist_red[i] for i in index_final]
fin_blue = [dist_blue[i] for i in index_final]
fin_yel = [dist_yel[i] for i in index_final]
for i in range(len(pedals_adj)):
if red is not None:
dist_red[i][0] = 1*dist_red[i][0]
if yellow is not None:
dist_yel[i][0] = 0.725*dist_yel[i][0]
if blue is not None:
dist_blue[i][0] = 0.725 * dist_blue[i][0]
#Scaling
# dist_blue[i][0] = 0.725 * dist_blue[i][0]
# dist_red[i][0] = 2.27*dist_red[i][0]
# dist_yel[i][0] = 2.27*dist_yel[i][0]
#---#
###Part 8: PLacing the players in based on scaled final coordinates
# Please refer to the conditional file: ap_if_else_chain.py which contains the chain of if-elses
coordinate_final = []
with open("ap_if_else_chain.py") as file:
exec(file.read())
#---#
###Part 9: Drawing of final image that is to be returned
template2 = template.copy()
for w, i in zip(coordinate_final, index_final):
if w is not None:
cv.circle(template2, w, 15, (0, 255, 0), 2)
cv.putText(template2, str(i), w, cv.FONT_HERSHEY_SIMPLEX, 0.8, 255, 2)
# bf.show_image(template2)
temp3 = cv.resize(template2, (640, 360))
vis = np.concatenate((img5, temp3), axis = 0)
# bf.show_image(vis)
#---#
return([template2, vis])
frame="rink template"
template = cv.imread(frame+".png")
bf.show_image(template)
frame_no = 18
img = cv.imread("vid2frame/"+ "frame_v2_" + str(frame_no) + ".png")
bf.show_image(img)
l = combo(img)
bf.show_image(l)
today = str(date.today())
bf.createFolder("vid2frame/"+today)
for i in range(58, 65):
img = cv.imread("vid2frame/all frames/"+ "frame_v2_" + str(i) + ".png")
try:
final_image = combo(img)
cv.imwrite("vid2frame/" + today +"/comb_" + str(i) + ".png", final_image[1])
except:
pass
print(i)
# #showing the frame
# cv.imshow("Final", final_image)
#
# #Option to quit
# if cv.waitKey(1) & 0xFF == ord('q'):
# break
cv.destroyAllWindows()