-
Notifications
You must be signed in to change notification settings - Fork 18
/
Offside_detection.py
385 lines (300 loc) · 12 KB
/
Offside_detection.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# USAGE
# To detect offside in a pre-recorded video, type the following in terminal:
# python Offside_detection.py -v 'name of video file'
#
# To detect offside from live camera feed:
# python Offside_detection.py
#
# while running the program, press 'i' to input and 'q' to quit
import cv2
import numpy as np
import track_utils
from collections import deque
import math
frame = None
orig_frame = None
roi_hist_A, roi_hist_B = None, None
roi = None
team = None
teamA = np.array([])
teamB = np.array([])
teamB_new = np.array([])
teamA_new = np.array([])
pts = []
minDist = 0
prevTeam = None
prevPasser = -1
M = None
op = None
limits = None
ball_center = None
kernel = np.array([[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0]], dtype=np.uint8)
prevgrad = 0
passes = 0
vel, prev_vel = 0, 0
pts_ball = deque()
def trackBall():
global grad, prevgrad, passes, ball_center, pts_ball, frame, vel, prev_vel, prevPasser, prevTeam, minDist
pts_ball.appendleft(ball_center)
if len(pts_ball) > 2:
if len(pts_ball) > 20:
for i in xrange(1, 20):
cv2.line(frame, pts_ball[i - 1], pts_ball[i], (0, 0, 255), 2, cv2.LINE_AA)
else:
for i in xrange(1, len(pts_ball)):
cv2.line(frame, pts_ball[i - 1], pts_ball[i], (0, 0, 255), 2, cv2.LINE_AA)
l = len(pts_ball)
if l >= 10:
grad = np.arctan2((pts_ball[9][1] - pts_ball[0][1]), (pts_ball[9][0] - pts_ball[0][0]))
grad = grad * (180.0 / np.pi)
grad %= 360
vel = math.sqrt((pts_ball[9][1] - pts_ball[0][1]) ** 2 + (pts_ball[9][0] - pts_ball[0][0]) ** 2) / 10
if (math.fabs(grad - prevgrad) >= 20):
# or math.fabs(vel-prev_vel) >= 7:
# detectPlayers()
# print("a " + str(len(teamA)) + " b " + str(len(teamB)))
if len(teamA) != 0 and len(teamB) != 0:
getCoordinates()
detectPasser()
# print(passerIndex)
if ((prevTeam != team) or (passerIndex != prevPasser)) and minDist < 10000:
# print(minDist)
# print(str(team) + str(passerIndex))
if (team == 'A'):
detectOffside()
else:
print('Not offside')
passes += 1
#print('Ball Passed ' + str(passes))
prevPasser = passerIndex
prevTeam = team
prevgrad = grad
prev_vel = vel
def detectPlayers():
global frame, roi_hist_A, roi_hist_B, teamA, teamB
teamA = []
teamB = []
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cnt_thresh = 180
if roi_hist_A is not None:
backProjA = cv2.calcBackProject([hsv], [0, 1], roi_hist_A, [0, 180, 0, 256], 1)
maskA = track_utils.applyMorphTransforms2(backProjA)
#cv2.imshow('mask a', maskA)
cnts = cv2.findContours(maskA.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts) > 0:
c = sorted(cnts, key=cv2.contourArea, reverse=True)
for i in range(len(c)):
if cv2.contourArea(c[i]) < cnt_thresh:
break
x, y, w, h = cv2.boundingRect(c[i])
h += 5
y -= 5
if h < 0.8 * w:
continue
elif h / float(w) > 3:
continue
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
M = cv2.moments(c[i])
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
foot = (center[0], int(center[1] + h * 1.5))
teamA.append(foot)
cv2.circle(frame, foot, 5, (0, 0, 255), -1)
if roi_hist_B is not None:
backProjB = cv2.calcBackProject([hsv], [0, 1], roi_hist_B, [0, 180, 0, 256], 1)
maskB = track_utils.applyMorphTransforms2(backProjB)
#cv2.imshow('mask b', maskB)
cnts = cv2.findContours(maskB.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts) > 0:
c = sorted(cnts, key=cv2.contourArea, reverse=True)
for i in range(len(c)):
if cv2.contourArea(c[i]) < cnt_thresh:
break
x, y, w, h = cv2.boundingRect(c[i])
h += 5
y -= 5
if h < 0.9 * w:
continue
elif h / float(w) > 3:
continue
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
M = cv2.moments(c[i])
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
foot = (center[0], int(center[1] + h * 1.2))
teamB.append(foot)
cv2.circle(frame, foot, 5, (0, 0, 255), -1)
def selectPoints(event, x, y, flag, param):
global pts, frame, orig_frame
if event == cv2.EVENT_LBUTTONUP:
if len(pts) < 8:
pts.append([x, y])
cv2.circle(frame, (x, y), 5, (0, 0, 255), -1)
else:
print('You have already selected 4 points')
def getBoundaryPoints():
global frame, pts
end_pts = []
cv2.namedWindow('input field')
cv2.setMouseCallback('input field', selectPoints)
while True:
cv2.imshow('input field', frame)
key = cv2.waitKey(1) & 0xFF
if len(pts) >= 8:
pts = np.array(pts, dtype=np.float32)
pts[:, 1] *= (-1)
for i in range(0, 5, 2):
m1 = (pts[i + 1][1] - pts[i][1]) / (pts[i + 1][0] - pts[i][0])
m2 = (pts[i + 3][1] - pts[i + 2][1]) / (pts[i + 3][0] - pts[i + 2][0])
A = np.array([[m1, -1], [m2, -1]])
A_inv = np.linalg.inv(A)
B = np.array([pts[i][1] - m1 * pts[i][0], pts[i + 2][1] - m2 * pts[i + 2][0]])
B *= (-1)
p = np.dot(A_inv, B)
end_pts.append(np.int16(p))
m1 = (pts[7][1] - pts[6][1]) / (pts[7][0] - pts[6][0])
m2 = (pts[1][1] - pts[0][1]) / (pts[1][0] - pts[0][0])
A = np.array([[m1, -1], [m2, -1]])
A_inv = np.linalg.inv(A)
B = np.array([pts[6][1] - m1 * pts[6][0], pts[0][1] - m2 * pts[0][0]])
B *= (-1)
p = np.dot(A_inv, B)
end_pts.append(np.int16(p))
end_pts = np.array(end_pts)
end_pts[:, 1] *= (-1)
break
elif key == ord("q"):
break
cv2.destroyWindow('input field')
return end_pts
def getCoordinates():
global M, teamA, teamB, op, teamB_new, teamA_new, ball_new, ball_center
teamB_new = np.array([])
teamA_new = np.array([])
op = orig_op.copy()
if ball_center is not None:
new = np.dot(M, [ball_center[0], ball_center[1], 1])
ball_new = [new[0] / new[2], new[1] / new[2]]
op = cv2.circle(op, (int(ball_new[0]), int(ball_new[1])), 3, (255, 0, 0), -1)
if len(teamB) > 0:
for i in range(len(teamB)):
new_pt = np.dot(M, [teamB[i][0], teamB[i][1], 1])
teamB_new = np.append(teamB_new, [new_pt[0] / new_pt[2], new_pt[1] / new_pt[2]])
teamB_new = np.int16(teamB_new).reshape(-1, 2)
for i in range(len(teamB)):
op = cv2.circle(op, (teamB_new[i][0], teamB_new[i][1]), 5, (0, 255, 0), -1)
if len(teamA) > 0:
for i in range(len(teamA)):
new_pt = np.dot(M, [teamA[i][0], teamA[i][1], 1])
teamA_new = np.append(teamA_new, [new_pt[0] / new_pt[2], new_pt[1] / new_pt[2]])
teamA_new = np.int16(teamA_new).reshape(-1, 2)
for i in range(len(teamA)):
op = cv2.circle(op, (teamA_new[i][0], teamA_new[i][1]), 5, (0, 0, 255), -1)
def drawOffsideLine():
global M, teamB_new, op, frame
if len(teamB_new) > 0:
M_inv = np.linalg.inv(M)
last_def = np.argmin(teamB_new[:,0])
p1 = np.dot(M_inv, [teamB_new[last_def][0], 0, 1])
p2 = np.dot(M_inv, [teamB_new[last_def][0], op.shape[0] - 1, 1])
pts = [(int(p1[0] / p1[2]), int(p1[1] / p1[2])), (int(p2[0] / p2[2]), int(p2[1] / p2[2]))]
frame = cv2.line(frame, pts[0], pts[1], (255, 0, 0), 2)
def closest_node(node, nodes):
nodes = np.asarray(nodes)
node = np.array([node[0], node[1]])
# print(nodes)
# print(node)
dist_2 = np.sum((nodes - node) ** 2, axis=1)
return np.argmin(dist_2)
def detectPasser():
global ball_new, teamA, teamB, passerIndex, team, minDist
teamA_min_ind = closest_node(ball_new, teamA_new)
teamB_min_ind = closest_node(ball_new, teamB_new)
# print(np.asarray(teamA[teamA_min_ind]))
# print(np.asarray(ball_center))
teamA_min = np.sum(([np.asarray(teamA_new[teamA_min_ind])] - np.asarray(ball_new)) ** 2, axis=1)
teamB_min = np.sum(([np.asarray(teamB_new[teamB_min_ind])] - np.asarray(ball_new)) ** 2, axis=1)
minDist = min(teamB_min, teamA_min)
if (teamA_min < teamB_min):
# print("Ball passed by TeamA player")
passerIndex = teamA_min_ind
# print(passerIndex)
team = 'A'
else:
# print("Ball passed by TeamB player")
passerIndex = teamB_min_ind
team = 'B'
def detectOffside():
global teamA_new, teamB_new, passerIndex
if len(teamB_new) > 0:
if len(teamA_new) > 0:
# teamA_new.sort()
teamB_new.sort()
# print(teamA_new)
if (teamB_new[0][0] > teamA_new[passerIndex][0]):
# if (teamB[0][0] > teamA[passerIndex][0]):
# print(passerIndex)
# Assuming no goalie
print('Offside')
else:
print('Not Offside')
else:
print('Not Offside')
else:
print('Not Offside')
if __name__ == '__main__':
args = track_utils.getArguements()
if not args.get("video", False):
camera = cv2.VideoCapture(0)
else:
camera = cv2.VideoCapture(args["video"])
orig_op = cv2.imread('soccer_half_field.jpeg')
op = orig_op.copy()
fgbg = cv2.createBackgroundSubtractorMOG2(history=20, detectShadows=False)
flag = False
while True:
(grabbed, frame) = camera.read()
if args.get("video") and not grabbed:
break
frame = track_utils.resize(frame, width=400)
orig_frame = frame.copy()
frame2 = track_utils.removeBG(orig_frame.copy(), fgbg)
detectPlayers()
if roi is not None:
ball_center, cnt = track_utils.detectBallThresh(frame2, limits)
if cnt is not None:
(x, y), radius = cv2.minEnclosingCircle(cnt)
cv2.circle(frame, (int(x), int(y)), int(radius), (255, 255, 0), 2)
cv2.circle(frame, ball_center, 2, (0, 0, 255), -1)
trackBall()
if M is not None:
src = np.int32(src)
for i in range(4):
frame = cv2.circle(frame.copy(), (src[i][0], src[i][1]), 3, (255, 0, 255), -1)
cv2.polylines(frame, np.int32([src]), True, (255, 0, 0), 2, cv2.LINE_AA)
getCoordinates()
drawOffsideLine()
cv2.imshow('camera view', frame)
cv2.imshow('top view', op)
if flag:
t = 1
else:
t = 100
key = cv2.waitKey(t) & 0xFF
if key == ord("q"):
break
elif key == ord('i') and (roi_hist_A is None or roi_hist_B is None):
flag = True
roi_hist_A, roi_hist_B = track_utils.getHist(frame)
roi = track_utils.getROIvid(orig_frame, 'input ball')
if roi is not None:
limits = track_utils.getLimits(roi)
src = getBoundaryPoints()
src = np.float32(src)
dst = np.float32([[0, 0], [0, op.shape[0]], [op.shape[1], op.shape[0]], [op.shape[1], 0]])
M = cv2.getPerspectiveTransform(src, dst)
camera.release()
cv2.destroyAllWindows()