-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinateAdjustments2.py
330 lines (224 loc) · 8.43 KB
/
coordinateAdjustments2.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
#import subprocess
#import sys
import cv2
import os
import numpy as np
import time
from time import sleep
import copy
#subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'opencv-python'])
print(cv2.__version__)
print('Esc - End')
# define framerate and period of framerate
framerate = 236
showTime = int((1 / framerate) * 1000)
# define camera width and height
sWidth = 640
sHeight = 360
# thresholds for circle dection
highThresh = 75
accumThresh = 30
# min and max radius of circle
minRad = 2
maxRad = 15
# minimum distance between mutliple circles allowed
minDist = 1000
# threshold for filtering out noise when subtracting images
differenceThresh = 150
# threshold for filtering out erroneous circles
# circles that appear at an angle too different from the previous pair will be filtered out
angleThresh = 10
# threshold for list length
# if angle list is > threshold, ball has been detected
listThresh = 4
# amount of time to sleep after ball detection
sleepOnSuccess = 4
lastSuccessTime = time.time() - sleepOnSuccess
# To ensure multiples aren't added
circleAdded = False
# red and green colors for circle display
red = (0, 0, 255)
green = (0, 255, 0)
blue = (255, 0, 255)
white = (255, 255, 255)
# for easy file access
#rootDir = '../testData/fastCamCaps/'
#rootDir = '../testData/756/'
rootDir = '../testData/diamondData/zone3/'
fileList = os.listdir(rootDir)
path = fileList[2]
print(path)
#decisionLine = sHeight - 300
decisionLine = sHeight / 2
slopeThresh = sWidth / sHeight
def evaluateZone(circleList):
if len(circleList) <= 0:
print('circle length <= %d' % (len(circleList)))
return
sumSlope = 0
sumRad = 0
sumX = 0
sumY = 0
zeroDivs = 0
for i in range(0, len(circleList) - 1):
c1 = circleList[i][0]
c2 = circleList[i + 1][0]
x1, y1 = c1[1], c1[0]
x2, y2 = c2[1], c2[0]
if x2 - x1 == 0:
continue
try:
slope = ((y2 - y1) / (x2 - x1))
if slope < slopeThresh or slope > -slopeThresh:
sumSlope += slope
sumX += x1
sumY += y1
sumRad += c1[2]
except ZeroDivisionError:
zeroDivs += 1
sumRad += circleList[len(circleList) - 1][0][2]
sumX += circleList[len(circleList) - 1][0][1]
sumY += circleList[len(circleList) - 1][0][0]
avgRad = int(sumRad / len(circleList))
avgSlope = sumSlope / (len(circleList) - zeroDivs)
avgX = sumX / len(circleList)
avgY = sumY / len(circleList)
first = circleList[0][0]
last = circleList[len(circleList) - 1][0]
#avgSlope = (last[2] - first[2]) / (last[1] - first[1])
print('slope: %f' % avgSlope)
#intercept = (avgX + (avgY * avgSlope))
intercept = -(avgX * avgSlope) + avgY
print(avgSlope)
print(avgRad)
zoneList = []
midPoint = sWidth / 2
for i in range(-3, 4):
zoneList.append([midPoint - avgRad + (i * (2 * avgRad)), midPoint + avgRad + (i * (2 * avgRad))])
posAtDecisionLine = (intercept + (avgSlope * decisionLine))
print('Position at Line: %f' % (posAtDecisionLine))
zone = 0
print(zoneList)
for i in range(0, len(zoneList)):
leftBound = zoneList[i][0]
rightBound = zoneList[i][1]
if posAtDecisionLine > leftBound and posAtDecisionLine < rightBound:
# add 1 to offset index
zone = i + 1
break
return (avgSlope, intercept), zone, zoneList
# video capture, takes file path as arg
# integer value for integrated webcam / usb cameras
#vidcap = cv2.VideoCapture(rootDir + path)
vidcap = cv2.VideoCapture(0)
vidcap.set(cv2.CAP_PROP_FPS, framerate)
vidcap.set(cv2.CAP_PROP_FRAME_WIDTH, sWidth)
vidcap.set(cv2.CAP_PROP_FRAME_HEIGHT, sHeight)
subtractor = cv2.createBackgroundSubtractorMOG2(history = 100, varThreshold = 50, detectShadows = False)
def updateBackground():
for i in range(0, 100):
success, f = vidcap.read()
if not success:
continue
subtractor.apply(f)
# function for reading a frame
# returns boolean for succes/fail and the frame as an ndarray
success, prevFrame = vidcap.read()
sWidth = prevFrame.shape[1]
sHeight = prevFrame.shape[0]
print(sWidth, sHeight)
# create windows
cv2.namedWindow('Circle Detection')
cv2.namedWindow('Difference')
# list of previous circles
prevCircles = []
n = 0
framesWithoutCircleThresh = 30
framesSinceLastCircle = 0
ballDetectedLastFrame = False
# main loop of algorithm
while success:
# read in frame
success, frame = vidcap.read()
# exit on fail
if success == False:
break
diff = subtractor.apply(frame)
ballDetected = False
if (time.time() - lastSuccessTime) > sleepOnSuccess:
# perform circle detection
circles = cv2.HoughCircles(diff, cv2.HOUGH_GRADIENT, 2, minDist, param1 = highThresh, param2 = accumThresh, minRadius = minRad, maxRadius = maxRad)
# perform algorithm
if circles is not None:
x = circles[0][0][0]
y = circles[0][0][1]
if y > sHeight / 2:
if x > 430 or x < 200:
#print('ignoring %d %d' %(x, y))
continue
prevCircles.append(circles[0])
framesSinceLastCircle = 0
else:
framesSinceLastCircle += 1
if framesSinceLastCircle > framesWithoutCircleThresh:
if len(prevCircles) >= 2:
(slope, intercept), zone, zoneList = evaluateZone(prevCircles)
lastSuccessTime = time.time()
prevCircles.clear()
#slope *= -1
slope = slope / 1
cv2.line(frame, (int(intercept), 0), (int(intercept + (slope * sHeight)), sHeight), green, 3)
for z in zoneList:
cv2.line(frame, (int(z[0]), 0), (int(z[0]), sHeight), red, 1)
cv2.line(frame, (int(z[1]), 0), (int(z[1]), sHeight), red, 1)
cv2.line(frame, (0, int(decisionLine)), (sWidth, int(decisionLine)), blue, 2)
#cv2.line(frame, (int(sWidth / 2), 0), (int(sWidth / 2), sHeight), white, 2)
cv2.imshow('Circle Detection', frame)
print('Zone: %s' % (zone))
key = cv2.waitKey(0)
updateBackground()
prevCircles.clear()
angles = []
if len(prevCircles) > 1:
for i in range(1, len(prevCircles)):
#c1 = prevCircles[0][0]
c1 = prevCircles[i-1][0]
c2 = prevCircles[i][0]
xDist = c1[0] - c2[0]
yDist = c1[1] - c2[1]
if not(xDist == 0):
theta = np.degrees(np.arctan(yDist / xDist))
angles.append(theta)
if len(prevCircles) > 2:
ballDetected = True
# convert diff to color so we can lay green circles on top
diff = cv2.cvtColor(diff, cv2.COLOR_GRAY2BGR)
# draw circles on images
if prevCircles is not None:
try:
convertedPrevCircles = np.uint16(np.around(prevCircles))
except:
continue
# loop through all found circles
for i in convertedPrevCircles:
i = i[0]
# (i[0], i[1]) is (x, y); i[2] is radius (radius green circle)
cv2.circle(frame, (i[0], i[1]), i[2], green, 2)
# (i[0], i[1]) is (x, y); 2 is radius (center red dot)
cv2.circle(frame, (i[0], i[1]), 2, red, 3)
# same but circling on diff
cv2.circle(diff, (i[0], i[1]), i[2], green, 2)
cv2.circle(diff, (i[0], i[1]), 2, red, 3)
ballDetectedLastFrame = ballDetected
# show iamges to windows
cv2.imshow('Circle Detection', frame)
cv2.imshow('Difference', diff)
#key = cv2.waitKey(0)
key = cv2.waitKey(showTime)
if key == 27 or not success:
break
# destroy windows and release file/camera handle
# important, don't remove
cv2.destroyAllWindows()
vidcap.release()
print('done')