-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageTest.py
More file actions
272 lines (213 loc) · 9.66 KB
/
imageTest.py
File metadata and controls
272 lines (213 loc) · 9.66 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
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
import cv2
import numpy as np
import math
import colorsys
import socket
from window_info import *
cam = cv2.VideoCapture(0)
global img
frameNum = []
numClicked = 0
numBalls = 0
hsv_values = []
hsv_values2 = []
hsv_ranges = []
positions = []
existsLine = 0
xVelocity = []
yVelocity = []
xVelocityPrev = []
yVelocityPrev = []
numThrown = 0
sock1 = socket.socket()
host = 'localhost'
port1 = 2343
sock1.connect((host, port1))
sock2 = socket.socket()
port2 = 2344
sock2.connect((host, port2))
def main(live):
cam = cv2.VideoCapture(0)
global img
global frameNum
global numClicked
global numBalls
global hsv_values
global hsv_values2
global hsv_ranges
global positions
global existsLine
global xVelocity
global yVelocity
global xVelocityPrev
global yVelocityPrev
global numThrown
cam = cv2.VideoCapture(0)
frameNum = []
numClicked = 0
numBalls = 0
hsv_values = []
hsv_values2 = []
hsv_ranges = []
positions = []
existsLine = 0
xVelocity = []
yVelocity = []
xVelocityPrev = []
yVelocityPrev = []
yHeight = 300
isAbove = []
numThrown = 0
def onmouse(event, x, y, flags, param):
global numBalls
global numClicked
if event == cv2.EVENT_LBUTTONDOWN:
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
(h,s,v) = cv2.split(img2)
# print(h[y][x])
# print(countSelected)
hsv_values.append([h[y][x],s[y][x],v[y][x]])
# print (hsv_values)
numClicked+=1
if numClicked == 3:
numBalls+=1
hsv_ranges.append(max(hsv_values[numBalls*3-3][0], hsv_values[numBalls*3-2][0], hsv_values[numBalls*3-1][0]))
hsv_ranges[numBalls*3-3] -= min(hsv_values[numBalls*3-3][0], hsv_values[numBalls*3-2][0], hsv_values[numBalls*3-1][0])
hsv_ranges.append(max(hsv_values[numBalls*3-3][1], hsv_values[numBalls*3-2][1], hsv_values[numBalls*3-1][1]))
hsv_ranges[numBalls*3-2] -= min(hsv_values[numBalls*3-3][1], hsv_values[numBalls*3-2][1], hsv_values[numBalls*3-1][1])
hsv_ranges.append(max(hsv_values[numBalls*3-3][2], hsv_values[numBalls*3-2][2], hsv_values[numBalls*3-1][2]))
hsv_ranges[numBalls*3-1] -= min(hsv_values[numBalls*3-3][2], hsv_values[numBalls*3-2][2], hsv_values[numBalls*3-1][2])
hsv_values2.append([np.uint8(round((np.int(hsv_values[numBalls*3-3][0]) + np.int(hsv_values[numBalls*3-2][0]) + np.int(hsv_values[numBalls*3-1][0]))//3)),
np.uint8(round((np.int(hsv_values[numBalls*3-3][1]) + np.int(hsv_values[numBalls*3-2][1]) + np.int(hsv_values[numBalls*3-1][1]))//3)),
np.uint8(round((np.int(hsv_values[numBalls*3-3][2]) + np.int(hsv_values[numBalls*3-2][2]) + np.int(hsv_values[numBalls*3-1][2]))//3))])
numClicked = 0
positions.append([[0, 0]])
frameNum.append(0)
xVelocity.append(0)
yVelocity.append(0)
xVelocityPrev.append(0)
yVelocityPrev.append(0)
isAbove.append(0)
while True:
if numClicked < 1:
__,img = cam.read()
img = cv2.resize(img, (hgt, wid))
img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow(prgmName, img)
cv2.setMouseCallback(prgmName, onmouse)
ch = chr(0xFF & cv2.waitKey(5))
if ch == 'q':
return
elif ch ==' ':
break
def hueUpper (hue, range):
if hue<179-range*(3/4)+15:
return int (hue)+range*(3/4)+15
else:
return 179
def hueLower (hue, range):
if hue>range*(3/4)-15:
return int (hue)-range*(3/4)-15
else:
return 0
def satValUpper (satVal, range):
if satVal<255-range*(3/4)+20:
return int (satVal)+range*(3/4)+20
else:
return 255
def satValLower (satVal, range):
if satVal>range*(3/4)-20:
return int (satVal)-range*(3/4)-2
else:
return 0
while True:
_, imgCam = cam.read()
imgCam=cv2.resize(imgCam,(hgt,wid))
imgCam = cv2.resize(imgCam, (0, 0), None, .25, .25)
imgCam=cv2.rotate(imgCam, cv2.ROTATE_90_CLOCKWISE)
hsv=cv2.cvtColor(imgCam,cv2.COLOR_BGR2HSV)
img = imgCam
(height,width,depth) = img.shape
fakeImage = np.zeros((height, width, depth), np.uint8)
mask=np.zeros((img.shape[0],img.shape[1],1), np.uint8)
for values in range(numBalls):
kernel = np.ones((3, 3), np.uint8)
# hsv_filtered = cv2.morphologyEx(hsv, cv2.MORPH_OPEN, kernel)
# hsv_filtered2 = cv2.GaussianBlur(hsv_filtered, (17,17), 0)
threshold = cv2.inRange(hsv, (hueLower(hsv_values2[values][0], hsv_ranges[3 * values]),
(satValLower(hsv_values2[values][1], hsv_ranges[3 * values + 1])), 50),
(hueUpper(hsv_values2[values][0], hsv_ranges[3 * values]),
(satValUpper(hsv_values2[values][1], hsv_ranges[3 * values + 1])), 225))
threshold_filtered = cv2.morphologyEx(threshold, cv2.MORPH_OPEN, kernel)
existsLine = 0
contrs, hier = cv2.findContours(threshold_filtered, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contrs) != 0:
for i in range(len(contrs)):
if len(contrs[i]) >= 5:
existsLine = 1
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
backEndImg = img
cv2.drawContours(backEndImg, contrs, -1, (150, 10, 255), 3)
ellipse = cv2.fitEllipse(contrs[i])
cv2.ellipse(backEndImg, ellipse, (0, 255, 0), 2)
cv2.circle(backEndImg, (int(ellipse[0][0]), int(ellipse[0][1])), 10, (255, 255, 255))
positions[values].append([int(ellipse[0][0]), int(ellipse[0][1])])
frameNum[values] += 1
if frameNum[values] >= 2:
(height, width, depth) = img.shape
nonImage = np.zeros((height, width, depth), np.uint8)
# # print (xVelocity[values])
# if yVelocity[values] < 0 and yVelocityPrev[values] > 0:
# print (isAbove[values])
if (positions[values][frameNum[values]][1]>yHeight and isAbove[values] == 1):
isAbove[values] = 0
if (positions[values][frameNum[values]][1]<yHeight and isAbove[values] == 0):
numThrown+=1
isAbove[values] = 1
print (numThrown)
for j in range(2, frameNum[values]):
lineColor = np.uint8(
[[[hsv_values2[values][0], hsv_values2[values][1], hsv_values2[values][2]]]])
#
# print(cv2.cvtColor(lineColor, cv2.COLOR_HSV2BGR)[0][0][0])
bgrColor = cv2.cvtColor(lineColor, cv2.COLOR_HSV2BGR)
actualbgr = bgrColor[0][0]
aaa = tuple([int(x) for x in actualbgr])
cv2.line(nonImage, (positions[values][j - 1][0], positions[values][j - 1][1]),
(positions[values][j][0], positions[values][j][1]),
aaa , thickness=5)
fakeImage = cv2.addWeighted(fakeImage, 1, nonImage, 1/3, 0)
break
if existsLine == 0:
if frameNum[values] >= 2:
(height, width, depth) = img.shape
nonImage = np.zeros((height, width, depth), np.uint8)
for j in range(2, frameNum[values]):
lineColor = np.uint8(
[[[hsv_values2[values][0], hsv_values2[values][1], hsv_values2[values][2]]]])
#
# print(cv2.cvtColor(lineColor, cv2.COLOR_HSV2BGR)[0][0][0])
bgrColor = cv2.cvtColor(lineColor, cv2.COLOR_HSV2BGR)
actualbgr = bgrColor[0][0]
aaa = tuple([int(x) for x in actualbgr])
cv2.line(nonImage, (positions[values][j - 1][0], positions[values][j - 1][1]),
(positions[values][j][0], positions[values][j][1]),
aaa, thickness=5)
fakeImage = cv2.addWeighted(fakeImage, 1, nonImage, 1 / 3, 0)
mask = cv2.bitwise_or(mask, threshold)
img = cv2.addWeighted(fakeImage, 0.9, imgCam, 0.1, 100)
# else:
# # optional to "delete" the small contours
# cv2.drawContours(img, contrs, -1, (0, 0, 0), -1)1
# cv2.drawContours(img, contrs, -1, (0, 255, 0), 3)
#
maskedimg = cv2.bitwise_and(img,img,mask=mask)
imgLeft = np.vstack((imgCam, maskedimg))
imgRight = np.vstack((backEndImg, img))
imgFull = np.hstack((imgLeft, imgRight))
cv2.imshow(prgmName, imgFull)
ch = chr(0xFF & cv2.waitKey(1))
if ch == 'q':
break
#RGB code: R: 175 G: 15 B: 17
# HSV: 359.25° 91.43% 68.63%