This repository has been archived by the owner on Feb 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
detect_coins.py
254 lines (206 loc) · 8.13 KB
/
detect_coins.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
# todo: try classification using one-dimensional data clustering of radii
# http://journal.r-project.org/archive/2011-2/RJournal_2011-2_Wang+Song.pdf
# import classifier
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
import math
import numpy as np
import argparse
import glob
import cv2
# construct argument parser and parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
# resize image while retaining aspect ratio
d = 1024 / image.shape[1]
dim = (1024, int(image.shape[0] * d))
image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
# create a copy of the image to display results
output = image.copy()
# convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# improve contrast accounting for differences in lighting conditions:
# create a CLAHE object to apply contrast limiting adaptive histogram equalization
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
gray = clahe.apply(gray)
def calcHistogram(img):
# create mask
m = np.zeros(img.shape[:2], dtype="uint8")
(w, h) = (int(img.shape[1] / 2), int(img.shape[0] / 2))
cv2.circle(m, (w, h), 60, 255, -1)
# calcHist expects a list of images, color channels, mask, bins, ranges
h = cv2.calcHist([img], [0, 1, 2], m, [8, 8, 8], [0, 256, 0, 256, 0, 256])
# return normalized "flattened" histogram
return cv2.normalize(h, h).flatten()
def calcHistFromFile(file):
img = cv2.imread(file)
return calcHistogram(img)
# define Enum class
class Enum(tuple): __getattr__ = tuple.index
# Enumerate material types for use in classifier
Material = Enum(('Copper', 'Brass', 'Euro1', 'Euro2'))
# locate sample image files
sample_images_copper = glob.glob("sample_images/copper/*")
sample_images_brass = glob.glob("sample_images/brass/*")
sample_images_euro1 = glob.glob("sample_images/euro1/*")
sample_images_euro2 = glob.glob("sample_images/euro2/*")
# define training data and labels
X = []
y = []
# compute and store training data and labels
for i in sample_images_copper:
X.append(calcHistFromFile(i))
y.append(Material.Copper)
for i in sample_images_brass:
X.append(calcHistFromFile(i))
y.append(Material.Brass)
for i in sample_images_euro1:
X.append(calcHistFromFile(i))
y.append(Material.Euro1)
for i in sample_images_euro2:
X.append(calcHistFromFile(i))
y.append(Material.Euro2)
# instantiate classifier
# Multi-layer Perceptron
# score: 0.974137931034
clf = MLPClassifier(solver="lbfgs")
# split samples into training and test data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=.2)
# train and score classifier
clf.fit(X_train, y_train)
score = int(clf.score(X_test, y_test) * 100)
print("Classifier mean accuracy: ", score)
# blur the image using Gaussian blurring, where pixels closer to the center
# contribute more "weight" to the average, first argument is the source image,
# second argument is kernel size, third one is sigma (0 for autodetect)
# we use a 7x7 kernel and let OpenCV detect sigma
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
# circles: A vector that stores x, y, r for each detected circle.
# src_gray: Input image (grayscale)
# CV_HOUGH_GRADIENT: Defines the detection method.
# dp = 2.2: The inverse ratio of resolution
# min_dist = 100: Minimum distance between detected centers
# param_1 = 200: Upper threshold for the internal Canny edge detector
# param_2 = 100*: Threshold for center detection.
# min_radius = 50: Minimum radius to be detected.
# max_radius = 120: Maximum radius to be detected.
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=2.2, minDist=100,
param1=200, param2=100, minRadius=50, maxRadius=120)
def predictMaterial(roi):
# calculate feature vector for region of interest
hist = calcHistogram(roi)
# predict material type
s = clf.predict([hist])
# return predicted material type
return Material[int(s)]
# todo: refactor
diameter = []
materials = []
coordinates = []
count = 0
if circles is not None:
# append radius to list of diameters (we don't bother to multiply by 2)
for (x, y, r) in circles[0, :]:
diameter.append(r)
# convert coordinates and radii to integers
circles = np.round(circles[0, :]).astype("int")
# loop over coordinates and radii of the circles
for (x, y, d) in circles:
count += 1
# add coordinates to list
coordinates.append((x, y))
# extract region of interest
roi = image[y - d:y + d, x - d:x + d]
# try recognition of material type and add result to list
material = predictMaterial(roi)
materials.append(material)
# write masked coin to file
if False:
m = np.zeros(roi.shape[:2], dtype="uint8")
w = int(roi.shape[1] / 2)
h = int(roi.shape[0] / 2)
cv2.circle(m, (w, h), d, (255), -1)
maskedCoin = cv2.bitwise_and(roi, roi, mask=m)
cv2.imwrite("extracted/01coin{}.png".format(count), maskedCoin)
# draw contour and results in the output image
cv2.circle(output, (x, y), d, (0, 255, 0), 2)
cv2.putText(output, material,
(x - 40, y), cv2.FONT_HERSHEY_PLAIN,
1.5, (0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# get biggest diameter
biggest = max(diameter)
i = diameter.index(biggest)
# scale everything according to maximum diameter
# todo: this should be chosen by the user
if materials[i] == "Euro2":
diameter = [x / biggest * 25.75 for x in diameter]
scaledTo = "Scaled to 2 Euro"
elif materials[i] == "Brass":
diameter = [x / biggest * 24.25 for x in diameter]
scaledTo = "Scaled to 50 Cent"
elif materials[i] == "Euro1":
diameter = [x / biggest * 23.25 for x in diameter]
scaledTo = "Scaled to 1 Euro"
elif materials[i] == "Copper":
diameter = [x / biggest * 21.25 for x in diameter]
scaledTo = "Scaled to 5 Cent"
else:
scaledTo = "unable to scale.."
i = 0
total = 0
while i < len(diameter):
d = diameter[i]
m = materials[i]
(x, y) = coordinates[i]
t = "Unknown"
# compare to known diameters with some margin for error
if math.isclose(d, 25.75, abs_tol=1.25) and m == "Euro2":
t = "2 Euro"
total += 200
elif math.isclose(d, 23.25, abs_tol=2.5) and m == "Euro1":
t = "1 Euro"
total += 100
elif math.isclose(d, 19.75, abs_tol=1.25) and m == "Brass":
t = "10 Cent"
total += 10
elif math.isclose(d, 22.25, abs_tol=1.0) and m == "Brass":
t = "20 Cent"
total += 20
elif math.isclose(d, 24.25, abs_tol=2.5) and m == "Brass":
t = "50 Cent"
total += 50
elif math.isclose(d, 16.25, abs_tol=1.25) and m == "Copper":
t = "1 Cent"
total += 1
elif math.isclose(d, 18.75, abs_tol=1.25) and m == "Copper":
t = "2 Cent"
total += 2
elif math.isclose(d, 21.25, abs_tol=2.5) and m == "Copper":
t = "5 Cent"
total += 5
# write result on output image
cv2.putText(output, t,
(x - 40, y + 22), cv2.FONT_HERSHEY_PLAIN,
1.5, (255, 255, 255), thickness=2, lineType=cv2.LINE_AA)
i += 1
# resize output image while retaining aspect ratio
d = 768 / output.shape[1]
dim = (768, int(output.shape[0] * d))
image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
output = cv2.resize(output, dim, interpolation=cv2.INTER_AREA)
# write summary on output image
cv2.putText(output, scaledTo,
(5, output.shape[0] - 40), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), lineType=cv2.LINE_AA)
cv2.putText(output, "Coins detected: {}, EUR {:2}".format(count, total / 100),
(5, output.shape[0] - 24), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), lineType=cv2.LINE_AA)
cv2.putText(output, "Classifier mean accuracy: {}%".format(score),
(5, output.shape[0] - 8), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), lineType=cv2.LINE_AA)
# show output and wait for key to terminate program
cv2.imshow("Output", np.hstack([image, output]))
cv2.waitKey(0)