-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_identifier.py
182 lines (152 loc) · 6.43 KB
/
card_identifier.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
import cv2
import numpy as np
import sys
# Shape Separation Constants
WHITE_MIN = np.array([0, 0, 100],np.uint8)
WHITE_MAX = np.array([180, 100, 255],np.uint8)
AREA_THRESH = 7000
# Reference Features
OVAL_FEATURES = np.array([0.03155387787592858, -0.0037758243610366725, 0.205290481844915, -1.4503161754586032e-05, -2.9263679436010096e-05, 0.000142860777666745, 0.0001081177739358252])
DIAMOND_FEATURES = np.array([0.032902776926284946, -0.004827861419942702, 0.21169459653794936, -3.226527335538325e-05, 2.965936208089489e-06, 0.0001491130652801827, -6.661682099746633e-05])
SQUIGGLE_FEATURES = np.array([0.030580390546484034, 0.019392749469723414, 0.2646959376958161, 6.466367502274759e-05, 0.0005285519078694084, -0.0022415312093423457, -0.007887757425908665])
# Shading Constants
SOBEL_THRESH = 0.01
FILL_THRESH = 0.2
SHADED_COLOR_THRESH = 42
# Color Constants
COLOR_THRESH = 10
PURPLE_THRESH = 30
RED_BASELINE = np.array([20, 39, 201])
GREEN_BASELINE = np.array([67, 119, 12])
PURPLE_BASELINE = np.array([46, 20, 56])
def identify(img):
# Shape Separation
img = cv2.GaussianBlur(img, (3,3), cv2.BORDER_DEFAULT)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, img_thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# Get contours
contours, hierarchy = cv2.findContours(img_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
img_copy1 = img.copy()
# Remove contours that are inside others
contours_filtered = []
for i, contour in enumerate(contours):
if hierarchy[0,i,3] == -1:
contours_filtered.append(contour)
contours = contours_filtered
# Remove shapes that are too small
contours_area_filtered = []
for contour in contours:
if cv2.contourArea(contour) >= AREA_THRESH:
contours_area_filtered.append(contour)
contours = contours_area_filtered
img_copy2 = img.copy()
cv2.drawContours(img_copy2, contours, -1, (0, 255, 0), 2, cv2.LINE_AA)
# Find number of shapes
num_shapes = len(contours)
if num_shapes > 3:
print("scanning error - too many shapes; defaulting to 3")
num_shapes = 3
# Find shape of shapes
features = []
for contour in contours:
m = cv2.moments(contour)
features.append([
m["nu20"],
m["nu11"],
m["nu02"],
m["nu30"],
m["nu21"],
m["nu12"],
m["nu03"]
])
combined_distance = np.array([0.0,0.0,0.0])
for feature in features:
feature = np.array(feature)
oval_dist = np.linalg.norm(feature - OVAL_FEATURES)
diamond_dist = np.linalg.norm(feature - DIAMOND_FEATURES)
squiggle_dist = np.linalg.norm(feature - SQUIGGLE_FEATURES)
combined_distance += np.array([oval_dist, diamond_dist, squiggle_dist])
min_dist = np.min(combined_distance)
min_index = np.where(combined_distance == min_dist)[0][0]
shape = ["oval", "diamond", "squiggle"][min_index]
# Find Shading
# Use SobelY to find stripes
img_sobel = cv2.Sobel(img_thresh, cv2.CV_64F, 0, 1, ksize=1)
sobel_white = np.sum(img_sobel == 255)/img_sobel.size
thresh_white = np.sum(img_thresh == 255)/img_thresh.size
# If there are a lot of white on the Sobel image, it's probably striped
if sobel_white > SOBEL_THRESH/(4-num_shapes):
shading = "striped"
# If there is a lot of white on the thresh image, but not a lot
# of white on the Sobel image, it's probably solid
elif thresh_white > FILL_THRESH/(4-num_shapes):
shading = "filled"
# If there is not much white on either image, it's probably empty
else:
shading = "empty"
# Find Shading by comparing color distance to white card
# This makes sure that the card isn't actually striped and the image quality is just too bad
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask_shapes = np.zeros(img.shape[:2], np.uint8)
cv2.drawContours(mask_shapes, contours, -1, 255, -1)
# Get mean color of white card
mask_card = cv2.bitwise_not(mask_shapes)
mask_card = cv2.erode(mask_card, np.ones((3,3), np.uint8), iterations=1)
mean_card = cv2.mean(img_hsv, mask=mask_card)
print("card mean:", mean_card)
# Get mean color of shape
mean_shape = cv2.mean(img_hsv, mask=mask_shapes)
print("shape mean:", mean_shape)
distance = np.linalg.norm(np.array(mean_shape) - np.array(mean_card))
print(distance)
if distance > SHADED_COLOR_THRESH:
shading = "striped"
# Find Color
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img_thresh_card = cv2.inRange(img_hsv, WHITE_MIN, WHITE_MAX)
img_thresh_color = cv2.bitwise_not(img_thresh_card)
#cv2.imshow("thresh_color",img_thresh_color)
#cv2.waitKey()
mean_color = np.array(cv2.mean(img, mask=img_thresh_color)[:-1])
print("mean color:", mean_color)
'''
mean_card = np.mean(cv2.mean(img, mask=img_thresh_card)[:-1])
adj = np.array([255,255,255]) - np.array([mean_card, mean_card, mean_card])
print("adjustment:", adj)
mean_color = np.array(cv2.mean(img, mask=img_thresh_color)[:-1]) + adj/2
print("adjusted mean color:",mean_color)
# Boost saturation
arr = np.uint8([[mean_color]])
mean_color_hsv = cv2.cvtColor(arr, cv2.COLOR_RGB2HSV)[0][0]
mean_color_hsv[1] = 255
arr = np.uint8([[mean_color_hsv]])
mean_color = cv2.cvtColor(arr, cv2.COLOR_HSV2RGB)[0][0]
print(mean_color)
# Red
if mean_color[2] > mean_color[1]+COLOR_THRESH and mean_color[2] > mean_color[0]+COLOR_THRESH:
best_color = "red"
# Purple
elif mean_color[2] > mean_color[1]+COLOR_THRESH and mean_color[0]-mean_color[2] < PURPLE_THRESH:
best_color = "purple"
# Green
elif mean_color[1] > mean_color[0]+COLOR_THRESH and mean_color[1] > mean_color[2]+COLOR_THRESH:
best_color = "green"
else:
'''
diffs = [
np.linalg.norm(mean_color - RED_BASELINE),
np.linalg.norm(mean_color - GREEN_BASELINE),
np.linalg.norm(mean_color - PURPLE_BASELINE)
]
#print(diffs)
min_diff = min(diffs)
min_index = diffs.index(min_diff)
best_color = ["red", "green", "purple"][min_index]
return {
"num" : num_shapes,
"shape" : shape,
"shading": shading,
"color" : best_color,
}
if __name__ == "__main__":
print(identify(cv2.imread("cards/card7.png")))