/
vision.py
344 lines (293 loc) · 10.7 KB
/
vision.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
import os
import sys
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
import argparse
import cv2
import matplotlib
if os.getenv("MATPLOTLIB_USE_AGG"):
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
DO_DISPLAY = False
def display(img_defs):
""" Show images on a plot """
plt.figure(figsize=(10, 10))
horiz = len(img_defs) / 2 + 1
for idx, i in enumerate(img_defs):
plt.subplot(horiz, 2, idx + 1)
plt.imshow(i['image'], cmap='gray')
plt.title(i['title']), plt.xticks([]), plt.yticks([])
# close plot if you hit any key from terminal
plt.draw()
plt.pause(1) # <-------
raw_input("<Hit Enter To Close>")
plt.close()
def normalize_colors(I):
""" Because images are taken in different light, we need to normalize the
color balance """
In = I.astype(float)
II = In ** 2
II = np.sum(II, axis=2)
II = np.sqrt(II)
II = II[..., np.newaxis]
II = np.ma.concatenate((II, II, II), axis=2)
C = np.divide(I, II)
C = cv2.normalize(C, alpha=0, beta=255,
norm_type=cv2.NORM_MINMAX)
IC = np.uint8(C)
return IC
def to_bw(img):
threshold = 130 # TODO: May need to adjust. 115 result in all-black
GRAY = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
BW = cv2.threshold(GRAY, threshold, 255, cv2.THRESH_BINARY)[1]
return np.invert(BW)
def detect_count(img_bw):
BWt = img_bw.copy()
cntrs, hircy = cv2.findContours(BWt,
cv2.RETR_EXTERNAL, # cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
F = np.zeros(img_bw.shape, dtype=np.uint8)
MASK = np.zeros(img_bw.shape, dtype=np.uint8)
areas = [cv2.contourArea(cnt) for cnt in cntrs]
t = np.mean(filter(lambda x: x > 50, areas))
t = t * 0.9
MASKrect = None
MASKcnt = None
wasMasked = False
for idx, cnt in enumerate(cntrs):
if (areas[idx] > t):
# thickness -1 will fill the conntours
cv2.drawContours(F, [cnt], 0, (255), thickness=-1)
if (not wasMasked):
wasMasked = True
cv2.drawContours(MASK, [cnt], 0, (255), thickness=-1)
MASKcnt = cnt
MASKrect = cv2.boundingRect(cnt)
# cv2.imshow('Count', F)
# cv2.imshow('Mask', self.MASK)
cntrs, hircy = cv2.findContours(F,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
amount = len(cntrs)
return amount, MASK, MASKrect, MASKcnt
def cut_masks(img, img_bw, img_mask, mask_rect):
rect = [0, 0, 0, 0]
rect[0] = mask_rect[0] - 5
rect[1] = mask_rect[1] - 5
rect[2] = mask_rect[0] + mask_rect[2] + 5
rect[3] = mask_rect[1] + mask_rect[3] + 5
CutI = img[rect[1]:rect[3], rect[0]:rect[2]]
CutBW = img_bw[rect[1]:rect[3], rect[0]:rect[2]]
CutM = img_mask[rect[1]:rect[3], rect[0]:rect[2]]
return CutI, CutBW, CutM
def find_shading(cut_bw, cut_mask):
logger.debug('Attempting to determine shading...')
S = cv2.bitwise_and(cut_bw, cut_mask)
E = cv2.Canny(S, 90, 200, apertureSize=3)
# cv2.imshow('Edge', E)
nE = np.count_nonzero(E)
nM = np.count_nonzero(cut_mask)
dEM = float(nE) / float(nM)
logger.debug('dEM={}'.format(dEM))
# print "Mask %d, Edge %d, div %f" % (nM, nE, dEM)
shading = 'open'
if (dEM < 0.026):
shading = 'full' # solid
elif (dEM > 0.10):
shading = 'striped'
return shading
def find_shading2(img):
# edges = cv2.Canny(img,100,200)
edges = cv2.Canny(img, 100, 200, apertureSize=3)
nE = np.count_nonzero(edges)
print "non-zero edges", nE
def find_color(img, cut_i, cut_bw, cut_m):
S = cv2.bitwise_and(cut_bw, cut_m)
S = cv2.bitwise_and(cut_i, cut_i, mask=S)
# cv2.imshow('VS', S)
# ShowHist(S, self.cardInfo['color'])
HSV = cv2.cvtColor(S, cv2.COLOR_BGR2HSV)
H = HSV[:, :, 0]
nG = np.count_nonzero(cv2.inRange(H, 25, 90))
nP = np.count_nonzero(cv2.inRange(H, 140, 170))
nR = np.count_nonzero(cv2.inRange(H, 170, 255))
C = ['red', 'green', 'purple']
nC = [nR, nG, nP]
i = np.argmax(nC)
return C[i]
def find_shape(mask_cnt):
logger.debug("Determining shape...")
cnt = mask_cnt
mmnts = cv2.moments(cnt)
hu = cv2.HuMoments(mmnts)
logger.debug("Hu moments - first: {}".format(hu[0]))
# print cv2.contourArea(cnt)
symbol = ""
if (hu[0] < 0.210):
symbol = 'oval'
elif (hu[0] > 0.23):
symbol = 'squiggle'
else:
symbol = 'diamond'
return symbol
# ----------------------------------------------
# Run the analysis and interpret its results
# ----------------------------------------------
def analyze(image_path, expected=None, do_display=False):
img = cv2.imread(image_path) # load in color
edges = cv2.Canny(img, 100, 200)
edgesAperture = cv2.Canny(img, 90, 200, apertureSize=3)
normalized = normalize_colors(img)
normalized_edges = cv2.Canny(normalized, 100, 200)
img_bw = to_bw(img)
bw_edges = cv2.Canny(img_bw, 100, 200)
# find count
# TODO: separate mask creation from counting?
# TODO: Determine why 'detect_count' is failing to return masks in some
# cases -> happens if no `areas` found in `detect_count`
count, img_mask, mask_rect, mask_cnt = detect_count(img_bw)
cut_i, cut_bw, cut_m = cut_masks(img, img_bw, img_mask, mask_rect)
shading = find_shading(cut_bw, cut_m)
color = find_color(img, cut_i, cut_bw, cut_m)
shape = find_shape(mask_cnt)
actual = dict(color=color, shading=shading, shape=shape, count=count)
# Compare actual results VS expected results
if expected:
for k, v in expected.iteritems():
if actual.get(k) != v:
tmpl = "\t{}: actual = {} (expected = {})"
print tmpl.format(k, actual[k], expected[k])
# Print out images to debug the computer vision steps
if do_display:
defs = [
dict(title='original', image=img),
dict(title='normalized', image=normalized),
dict(title='edges', image=edges),
dict(title='edges (normalized)', image=normalized_edges),
dict(title='edges with apertureSize', image=edgesAperture),
dict(title='bw', image=img_bw),
dict(title='bw_edges', image=bw_edges),
dict(title='mask', image=img_mask),
dict(title='cut_i', image=cut_i),
dict(title='cut_bw', image=cut_bw),
dict(title='cut_m', image=cut_m),
]
# Open CV image display
display(defs)
return actual
def determine_expected(filename):
filename = filename.split('/')[-1]
name, ext = filename.split('.')
color, shading, shape, count = name.split('-')
count = int(count)
return dict(color=color, shading=shading, shape=shape, count=count)
COLOR_LIST = [
(0, 0, 255),
(0, 255, 0),
(255, 0, 0),
(0, 255, 255),
(255, 255, 0),
(255, 0, 255)
]
def findCards(fullpath):
""" Given a filepath to a photo containing set cards,
return a list of rectangles that outline each card """
threshold = 115
defs = []
I = cv2.imread(fullpath)
defs.append(dict(title='original', image=I))
# expected dimensions before: 2000px x 3000px
I = cv2.resize(np.rot90(I), (0, 0,), fx=0.5, fy=0.5)
defs.append(dict(title='rotated', image=I))
GRAY = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
defs.append(dict(title='gray', image=GRAY))
BW = cv2.threshold(GRAY, threshold, 255, cv2.THRESH_BINARY)[1]
defs.append(dict(title='bw', image=BW))
# BW = np.invert(BW)
BWt = BW.copy()
cntrs, hircy = cv2.findContours(BWt,
cv2.RETR_EXTERNAL, # cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
F = np.zeros(BW.shape, dtype=np.uint8)
# MASK = np.zeros(BW.shape, dtype=np.uint8)
areas = [cv2.contourArea(cnt) for cnt in cntrs]
t = np.mean(filter(lambda x: x > 50, areas))
t = t * 0.50 # decreasing from .9 to .5 works better with a tilted image
# MASKrect = None
# MASKcnt = None
# wasMasked = False
defs = []
cardRects = []
croppedCards = []
for idx, cnt in enumerate(cntrs):
if (areas[idx] > t):
# thickness -1 will fill the conntours
cv2.drawContours(F, [cnt], 0, (255), thickness=-1)
rect = cv2.boundingRect(cnt)
cardRects.append(rect)
cv2.rectangle(
I,
(rect[0],
rect[1]),
(rect[2] +
rect[0],
rect[3] +
rect[1]),
COLOR_LIST[
idx %
6],
thickness=2)
y1, y2 = rect[1], rect[1] + rect[3]
x1, x2 = rect[0], rect[0] + rect[2]
cropped = I[y1:y2, x1:x2]
croppedCards.append(cropped)
# defs.append(cropped)
# for i, c in enumerate(croppedCards):
# cv2.imshow('Image'+str(i), c)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# defs.append(dict(title='f', image=F))
# defs.append(dict(title='outlines', image=I))
# display(defs)
# return 12 sliced images, to pass to single card analysis
return cardRects
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-f',
'--file',
type=str,
required=True,
help="path to image file")
parser.add_argument('-c',
'--cv-type',
type=str,
default='find',
help="computer vision type: 'find' or 'analyze'")
parser.add_argument('--display',
action="store_true",
default=False,
help="display analysis in pop-up window.")
args = parser.parse_args()
if not os.path.exists(args.file):
print "Invalid file '{}'".format(args.file)
sys.exit(1)
# if a directory is given, recurse over all files in a directory
files = []
if os.path.isfile(args.file):
files = [args.file]
elif os.path.isdir(args.file):
dirname = args.file
files = [f for f in os.listdir(dirname) if f.endswith('.png')]
files = map(lambda f: os.path.join(dirname, f), files)
for f in files:
if args.cv_type == 'analyze':
out = analyze(f, do_display=args.display)
logger.info("Analysis of '{}':".format(f))
logger.info(out)
else:
cardRects = findCards(f)
logger.info("Number rects found = ", len(cardRects))
for c in cardRects:
logger.info(c)