Commit c5b4ef7c authored by Richard Torenvliet's avatar Richard Torenvliet

Merge branch 'master' of github.com:taddeus/licenseplates

Conflicts:
	docs/report.tex
parents 6dbb4aa6 2e1b2e8c
...@@ -15,9 +15,3 @@ images/BBB ...@@ -15,9 +15,3 @@ images/BBB
images/Images images/Images
images/Infos images/Infos
images/licenseplates images/licenseplates
chars
learning_set
test_set
classifier
classifier-model
classifier-characters
...@@ -45,10 +45,8 @@ in classifying characters on a license plate. ...@@ -45,10 +45,8 @@ in classifying characters on a license plate.
In short our program must be able to do the following: In short our program must be able to do the following:
\begin{enumerate} \begin{enumerate}
\item Use a perspective transformation to obtain an upfront view of license
plate.
\item Reduce noise where possible to ensure maximum readability.
\item Extracting characters using the location points in the xml file. \item Extracting characters using the location points in the xml file.
\item Reduce noise where possible to ensure maximum readability.
\item Transforming a character to a normal form. \item Transforming a character to a normal form.
\item Creating a local binary pattern histogram vector. \item Creating a local binary pattern histogram vector.
\item Matching the found vector with a learning set. \item Matching the found vector with a learning set.
...@@ -60,7 +58,7 @@ In short our program must be able to do the following: ...@@ -60,7 +58,7 @@ In short our program must be able to do the following:
The actual purpose of this project is to check if LBP is capable of recognizing The actual purpose of this project is to check if LBP is capable of recognizing
license plate characters. We knew the LBP implementation would be pretty license plate characters. We knew the LBP implementation would be pretty
simple. Thus an advantage had to be its speed compared with other license plate simple. Thus an advantage had to be its speed compared with other license plate
recognition implementations, but the uncertainity of whether we could get some recognition implementations, but the uncertainty of whether we could get some
results made us pick Python. We felt Python would not restrict us as much in results made us pick Python. We felt Python would not restrict us as much in
assigning tasks to each member of the group. In addition, when using the assigning tasks to each member of the group. In addition, when using the
correct modules to handle images, Python can be decent in speed. correct modules to handle images, Python can be decent in speed.
...@@ -70,48 +68,46 @@ correct modules to handle images, Python can be decent in speed. ...@@ -70,48 +68,46 @@ correct modules to handle images, Python can be decent in speed.
Now we know what our program has to be capable of, we can start with the Now we know what our program has to be capable of, we can start with the
implementations. implementations.
\subsection{Extracting a letter}
Rewrite this section once we have implemented this properly.
%NO LONGER VALID!
%Because we are already given the locations of the characters, we only need to
%transform those locations using the same perspective transformation used to
%create a front facing license plate. The next step is to transform the
%characters to a normalized manner. The size of the letter W is used as a
%standard to normalize the width of all the characters, because W is the widest
%character of the alphabet. We plan to also normalize the height of characters,
%the best manner for this is still to be determined.
%\begin{enumerate}
% \item Crop the image in such a way that the character precisely fits the
% image.
% \item Scale the image to a standard height.
% \item Extend the image on either the left or right side to a certain width.
%\end{enumerate}
%The resulting image will always have the same size, the character contained
%will always be of the same height, and the character will always be positioned
%at either the left of right side of the image.
\subsection{Transformation} \subsection{Transformation}
A simple perspective transformation will be sufficient to transform and resize A simple perspective transformation will be sufficient to transform and resize
the plate to a normalized format. The corner positions of license plates in the the characters to a normalized format. The corner positions of characters in the
dataset are supplied together with the dataset. dataset are supplied together with the dataset.
\subsection{Extracting a letter}
NO LONGER VALID!
Because we are already given the locations of the characters, we only need to
transform those locations using the same perspective transformation used to
create a front facing license plate. The next step is to transform the
characters to a normalized manner. The size of the letter W is used as a
standard to normalize the width of all the characters, because W is the widest
character of the alphabet. We plan to also normalize the height of characters,
the best manner for this is still to be determined.
\begin{enumerate}
\item Crop the image in such a way that the character precisely fits the
image.
\item Scale the image to a standard height.
\item Extend the image on either the left or right side to a certain width.
\end{enumerate}
The resulting image will always have the same size, the character contained
will always be of the same height, and the character will alway be positioned
at either the left of right side of the image.
\subsection{Reducing noise} \subsection{Reducing noise}
Small amounts of noise will probably be suppressed by usage of a Gaussian Small amounts of noise will probably be suppressed by usage of a Gaussian
filter. A real problem occurs in very dirty license plates, where branches and filter. A real problem occurs in very dirty license plates, where branches and
dirt over a letter could radically change the local binary pattern. A question dirt over a letter could radically change the local binary pattern. A question
we can ask ourselves here, is whether we want to concentrate ourselves on these we can ask ourselves here, is whether we want to concentrate ourselves on these
exceptional cases. By law, license plates have to be readable. Therefore, we exceptional cases. By law, license plates have to be readable. However, the
will first direct our attention at getting a higher score in the 'regular' test provided dataset showed that this does not means they always are. We will have
set before addressing these cases. Considered the fact that the LBP algorithm to see how the algorithm performs on these plates, however we have good hopes
divides a letter into a lot of cells, there is a good change that a great that our method will get a good score on dirty plates, as long as a big enough
number of cells will still match the learning set, and thus still return the part of the license plate remains readable.
correct character as a best match. Therefore, we expect the algorithm to be
very robust when dealing with noisy images.
\subsection{Local binary patterns} \subsection{Local binary patterns}
Once we have separate digits and characters, we intent to use Local Binary Once we have separate digits and characters, we intent to use Local Binary
...@@ -128,9 +124,9 @@ form where the pattern is circular. ...@@ -128,9 +124,9 @@ form where the pattern is circular.
\begin{itemize} \begin{itemize}
\item Determine the size of the square where the local patterns are being \item Determine the size of the square where the local patterns are being
registered. For explanation purposes let the square be 3 x 3. \\ registered. For explanation purposes let the square be 3 x 3. \\
\item The grayscale value of the middle pixel is used a threshold. Every value \item The grayscale value of the middle pixel is used as threshold. Every
of the pixel around the middle pixel is evaluated. If it's value is greater value of the pixel around the middle pixel is evaluated. If it's value is
than the threshold it will be become a one else a zero. greater than the threshold it will be become a one else a zero.
\begin{figure}[h!] \begin{figure}[h!]
\center \center
...@@ -176,27 +172,29 @@ order. Starting with dividing the pattern in to cells of size 16. ...@@ -176,27 +172,29 @@ order. Starting with dividing the pattern in to cells of size 16.
result is a feature vector of the image. result is a feature vector of the image.
\item Feed these vectors to a support vector machine. This will ''learn'' which \item Feed these vectors to a support vector machine. This will ''learn'' which
<<<<<<< HEAD
vector indicates what vector is which character. vector indicates what vector is which character.
=======
vectors indicate what letter.
>>>>>>> 2e1b2e8c8db4f802d203791a6f03eeca7d0aff70
\end{itemize} \end{itemize}
To our knowledge, LBP has yet not been used in this manner before. Therefore, To our knowledge, LBP has yet not been used in this manner before. Therefore,
it will be the first thing to implement, to see if it lives up to the it will be the first thing to implement, to see if it lives up to the
expectations. When the proof of concept is there, it can be used in the final expectations. When the proof of concept is there, it can be used in a final
program. program.
Important to note is that due to the normalization of characters before Later we will show that taking a histogram over the entire image (basically
applying LBP. Therefore, no further normalization is needed on the histograms. working with just one cell) gives us the best results.
Given the LBP of a character, a Support Vector Machine can be used to classify
the character to a character in a learning set. The SVM uses
\subsection{Matching the database} \subsection{Matching the database}
Given the LBP of a character, a Support Vector Machine can be used to classify Given the LBP of a character, a Support Vector Machine can be used to classify
the character to a character in a learning set. The SVM uses the collection of the character to a character in a learning set. The SVM uses a concatenation
histograms of an image as a feature vector. The SVM can be trained with a of each cell in an image as a feature vector (in the case we check the entire
subsection of the given dataset called the ''Learning set''. Once trained, the image no concatenation has to be done of course. The SVM can be trained with a
subset of the given dataset called the ''Learning set''. Once trained, the
entire classifier can be saved as a Pickle object\footnote{See entire classifier can be saved as a Pickle object\footnote{See
\url{http://docs.python.org/library/pickle.html}} for later usage. \url{http://docs.python.org/library/pickle.html}} for later usage.
...@@ -205,11 +203,11 @@ entire classifier can be saved as a Pickle object\footnote{See ...@@ -205,11 +203,11 @@ entire classifier can be saved as a Pickle object\footnote{See
In this section we will describe our implementations in more detail, explaining In this section we will describe our implementations in more detail, explaining
choices we made. choices we made.
\subsection{Licenseplate retrieval} \subsection{Character retrieval}
In order to retrieve the license plate from the entire image, we need to In order to retrieve the characters from the entire image, we need to
perform a perspective transformation. However, to do this, we need to know the perform a perspective transformation. However, to do this, we need to know the
coordinates of the four corners of the licenseplate. For our dataset, this is coordinates of the four corners of each character. For our dataset, this is
stored in XML files. So, the first step is to read these XML files. stored in XML files. So, the first step is to read these XML files.
\paragraph*{XML reader} \paragraph*{XML reader}
...@@ -247,9 +245,9 @@ NormalizedImage. When these actions have been completed for each character the ...@@ -247,9 +245,9 @@ NormalizedImage. When these actions have been completed for each character the
license plate is usable in the rest of the code. license plate is usable in the rest of the code.
\paragraph*{Perspective transformation} \paragraph*{Perspective transformation}
Once we retrieved the cornerpoints of the license plate, we feed those to a Once we retrieved the cornerpoints of the character, we feed those to a
module that extracts the (warped) license plate from the original image, and module that extracts the (warped) character from the original image, and
creates a new image where the license plate is cut out, and is transformed to a creates a new image where the character is cut out, and is transformed to a
rectangle. rectangle.
\subsection{Noise reduction} \subsection{Noise reduction}
...@@ -277,13 +275,6 @@ the dirt, and the fact that the characters are very black, the shape of the ...@@ -277,13 +275,6 @@ the dirt, and the fact that the characters are very black, the shape of the
characters will still be conserved in the LBP, even if there is dirt characters will still be conserved in the LBP, even if there is dirt
surrounding the character. surrounding the character.
\subsection{Character retrieval}
The retrieval of the character is done the same as the retrieval of the license
plate, by using a perspective transformation. The location of the characters on
the license plate is also available in de XML file, so this is parsed from that
as well.
\subsection{Creating Local Binary Patterns and feature vector} \subsection{Creating Local Binary Patterns and feature vector}
Every pixel is a center pixel and it is also a value to evaluate but not at the Every pixel is a center pixel and it is also a value to evaluate but not at the
same time. Every pixel is evaluated as shown in the explanation same time. Every pixel is evaluated as shown in the explanation
...@@ -337,9 +328,7 @@ value, and what value we decided on. ...@@ -337,9 +328,7 @@ value, and what value we decided on.
The first parameter to decide on, is the $\sigma$ used in the Gaussian blur. To The first parameter to decide on, is the $\sigma$ used in the Gaussian blur. To
find this parameter, we tested a few values, by checking visually what value find this parameter, we tested a few values, by checking visually what value
removed most noise out of the image, while keeping the edges sharp enough to removed most noise out of the image, while keeping the edges sharp enough to
work with. By checking in the neighbourhood of the value that performed best, work with. It turned out the best value is $\sigma = 0.5$.
we where able to 'zoom in' on what we thought was the best value. It turned out
that this was $\sigma = ?$.
\subsection{Parameter \emph{cell size}} \subsection{Parameter \emph{cell size}}
...@@ -352,8 +341,9 @@ be enough cells to properly describe the different areas of the character, and ...@@ -352,8 +341,9 @@ be enough cells to properly describe the different areas of the character, and
the feature vectors will not have enough elements.\\ the feature vectors will not have enough elements.\\
\\ \\
In order to find this parameter, we used a trial-and-error technique on a few In order to find this parameter, we used a trial-and-error technique on a few
basic cell sizes, being ?, 16, ?. We found that the best result was reached by cell sizes. During this testing, we discovered that a lot better score was
using ??. reached when we take the histogram over the entire image, so with a single
cell. therefor, we decided to work without cells.
\subsection{Parameters $\gamma$ \& $c$} \subsection{Parameters $\gamma$ \& $c$}
...@@ -374,7 +364,8 @@ checks for each combination of values what the score is. The combination with ...@@ -374,7 +364,8 @@ checks for each combination of values what the score is. The combination with
the highest score is then used as our parameters, and the entire SVM will be the highest score is then used as our parameters, and the entire SVM will be
trained using those parameters.\\ trained using those parameters.\\
\\ \\
We found that the best values for these parameters are $c=?$ and $\gamma =?$. We found that the best values for these parameters are $c = ?$ and
$\gamma = ?$.
\section{Results} \section{Results}
......
*.dat
results.txt
from LocalBinaryPatternizer import LocalBinaryPatternizer from LocalBinaryPatternizer import LocalBinaryPatternizer as LBP
class Character: class Character:
def __init__(self, value, corners, image, filename=None): def __init__(self, value, corners, image, filename=None):
...@@ -7,7 +7,14 @@ class Character: ...@@ -7,7 +7,14 @@ class Character:
self.image = image self.image = image
self.filename = filename self.filename = filename
def get_feature_vector(self): def get_single_cell_feature_vector(self):
pattern = LocalBinaryPatternizer(self.image) if hasattr(self, 'feature'):
return
self.feature = LBP(self.image).single_cell_features_vector()
def get_feature_vector(self, cell_size=None):
pattern = LBP(self.image) if cell_size == None \
else LBP(self.image, cell_size)
return pattern.create_features_vector() return pattern.create_features_vector()
from svmutil import svm_train, svm_problem, svm_parameter, svm_predict, \ from svmutil import svm_train, svm_problem, svm_parameter, svm_predict, \
svm_save_model, svm_load_model svm_save_model, svm_load_model, RBF
class Classifier: class Classifier:
def __init__(self, c=None, gamma=None, filename=None): def __init__(self, c=None, gamma=None, filename=None, cell_size=12):
self.cell_size = cell_size
if filename: if filename:
# If a filename is given, load a model from the given filename # If a filename is given, load a model from the given filename
self.model = svm_load_model(filename) self.model = svm_load_model(filename)
...@@ -11,8 +13,8 @@ class Classifier: ...@@ -11,8 +13,8 @@ class Classifier:
raise Exception('Please specify both C and gamma.') raise Exception('Please specify both C and gamma.')
else: else:
self.param = svm_parameter() self.param = svm_parameter()
self.param.kernel_type = 2 # Radial kernel type
self.param.C = c # Soft margin self.param.C = c # Soft margin
self.param.kernel_type = RBF # Radial kernel type
self.param.gamma = gamma # Parameter for radial kernel self.param.gamma = gamma # Parameter for radial kernel
self.model = None self.model = None
...@@ -28,10 +30,12 @@ class Classifier: ...@@ -28,10 +30,12 @@ class Classifier:
l = len(learning_set) l = len(learning_set)
for i, char in enumerate(learning_set): for i, char in enumerate(learning_set):
print 'Training "%s" -- %d of %d (%d%% done)' \ print 'Found "%s" -- %d of %d (%d%% done)' \
% (char.value, i + 1, l, int(100 * (i + 1) / l)) % (char.value, i + 1, l, int(100 * (i + 1) / l))
classes.append(float(ord(char.value))) classes.append(float(ord(char.value)))
features.append(char.get_feature_vector()) #features.append(char.get_feature_vector())
char.get_single_cell_feature_vector()
features.append(char.feature)
problem = svm_problem(classes, features) problem = svm_problem(classes, features)
self.model = svm_train(problem, self.param) self.model = svm_train(problem, self.param)
...@@ -48,9 +52,12 @@ class Classifier: ...@@ -48,9 +52,12 @@ class Classifier:
return float(matches) / len(test_set) return float(matches) / len(test_set)
def classify(self, character): def classify(self, character, true_value=None):
"""Classify a character object, return its value.""" """Classify a character object, return its value."""
predict = lambda x: svm_predict([0], [x], self.model)[0][0] true_value = 0 if true_value == None else ord(true_value)
prediction_class = predict(character.get_feature_vector()) #x = character.get_feature_vector(self.cell_size)
character.get_single_cell_feature_vector()
p = svm_predict([true_value], [character.feature], self.model)
prediction_class = int(p[0][0])
return chr(int(prediction_class)) return chr(prediction_class)
...@@ -18,19 +18,23 @@ class GrayscaleImage: ...@@ -18,19 +18,23 @@ class GrayscaleImage:
self.data = data self.data = data
def __iter__(self): def __iter__(self):
self.__i_x = -1 for y in xrange(self.data.shape[0]):
self.__i_y = 0 for x in xrange(self.data.shape[1]):
return self yield y, x, self.data[y, x]
def next(self): #self.__i_x = -1
self.__i_x += 1 #self.__i_y = 0
if self.__i_x == self.width: #return self
self.__i_x = 0
self.__i_y += 1 #def next(self):
if self.__i_y == self.height: # self.__i_x += 1
raise StopIteration # if self.__i_x == self.width:
# self.__i_x = 0
return self.__i_y, self.__i_x, self[self.__i_y, self.__i_x] # self.__i_y += 1
# if self.__i_y == self.height:
# raise StopIteration
# return self.__i_y, self.__i_x, self[self.__i_y, self.__i_x]
def __getitem__(self, position): def __getitem__(self, position):
return self.data[position] return self.data[position]
......
...@@ -16,6 +16,12 @@ class Histogram: ...@@ -16,6 +16,12 @@ class Histogram:
def get_bin_index(self, number): def get_bin_index(self, number):
return (number - self.min) / ((self.max - self.min) / len(self.bins)) return (number - self.min) / ((self.max - self.min) / len(self.bins))
def normalize(self):
minimum = min(self.bins)
self.bins = map(lambda b: b - minimum, self.bins)
maximum = float(max(self.bins))
self.bins = map(lambda b: b / maximum, self.bins)
def intersect(self, other): def intersect(self, other):
h1 = self.bins h1 = self.bins
h2 = other.bins h2 = other.bins
......
...@@ -6,24 +6,20 @@ class LocalBinaryPatternizer: ...@@ -6,24 +6,20 @@ class LocalBinaryPatternizer:
def __init__(self, image, cell_size=16): def __init__(self, image, cell_size=16):
self.cell_size = cell_size self.cell_size = cell_size
self.image = image self.image = image
self.setup_histograms()
def setup_histograms(self): def setup_histograms(self):
cells_in_width = int(ceil(self.image.width / float(self.cell_size))) cells_in_width = int(ceil(self.image.width / float(self.cell_size)))
cells_in_height = int(ceil(self.image.height / float(self.cell_size))) cells_in_height = int(ceil(self.image.height / float(self.cell_size)))
self.features = [] self.histograms = []
for i in xrange(cells_in_height): for i in xrange(cells_in_height):
self.features.append([]) self.histograms.append([])
for j in xrange(cells_in_width):
self.features[i].append(Histogram(256,0,256))
def create_features_vector(self): for j in xrange(cells_in_width):
''' Walk around the pixels in clokwise order, shifting 1 bit less self.histograms[i].append(Histogram(256, 0, 256))
at each neighbour starting at 7 in the top-left corner. This gives a
8-bit feature number of a pixel'''
for y, x, value in self.image:
pattern = (self.is_pixel_darker(y - 1, x - 1, value) << 7) \ def local_binary_pattern(self, y, x, value):
return (self.is_pixel_darker(y - 1, x - 1, value) << 7) \
| (self.is_pixel_darker(y - 1, x , value) << 6) \ | (self.is_pixel_darker(y - 1, x , value) << 6) \
| (self.is_pixel_darker(y - 1, x + 1, value) << 5) \ | (self.is_pixel_darker(y - 1, x + 1, value) << 5) \
| (self.is_pixel_darker(y , x + 1, value) << 4) \ | (self.is_pixel_darker(y , x + 1, value) << 4) \
...@@ -32,8 +28,15 @@ class LocalBinaryPatternizer: ...@@ -32,8 +28,15 @@ class LocalBinaryPatternizer:
| (self.is_pixel_darker(y + 1, x - 1, value) << 1) \ | (self.is_pixel_darker(y + 1, x - 1, value) << 1) \
| (self.is_pixel_darker(y , x - 1, value) << 0) | (self.is_pixel_darker(y , x - 1, value) << 0)
def create_features_vector(self):
'''Walk around the pixels in clokwise order, shifting 1 bit less at
each neighbour starting at 7 in the top-left corner. This gives a 8-bit
feature number of a pixel'''
self.setup_histograms()
for y, x, value in self.image:
cy, cx = self.get_cell_index(y, x) cy, cx = self.get_cell_index(y, x)
self.features[cy][cx].add(pattern) self.histograms[cy][cx].add(self.local_binary_pattern(y, x, value))
return self.get_features_as_array() return self.get_features_as_array()
...@@ -44,4 +47,27 @@ class LocalBinaryPatternizer: ...@@ -44,4 +47,27 @@ class LocalBinaryPatternizer:
return (y / self.cell_size, x / self.cell_size) return (y / self.cell_size, x / self.cell_size)
def get_features_as_array(self): def get_features_as_array(self):
return [h.bins for h in [h for sub in self.features for h in sub]][0] f = []
# Concatenate all histogram bins
for row in self.histograms:
for hist in row:
f.extend(hist.bins)
return f
#return [h.bins for h in [h for sub in self.histograms for h in sub]][0]
def get_single_histogram(self):
"""Create a single histogram of the local binary patterns in the
image."""
h = Histogram(256, 0, 256)
for y, x, value in self.image:
h.add(self.local_binary_pattern(y, x, value))
h.normalize()
return h
def single_cell_features_vector(self):
return self.get_single_histogram().bins
...@@ -5,7 +5,8 @@ from GaussianFilter import GaussianFilter ...@@ -5,7 +5,8 @@ from GaussianFilter import GaussianFilter
class NormalizedCharacterImage(GrayscaleImage): class NormalizedCharacterImage(GrayscaleImage):
def __init__(self, image=None, data=None, size=(60, 40), blur=1.1, crop_threshold=0.9): def __init__(self, image=None, data=None, size=(60, 40), blur=1.1, \
crop_threshold=0.9):
if image != None: if image != None:
GrayscaleImage.__init__(self, data=deepcopy(image.data)) GrayscaleImage.__init__(self, data=deepcopy(image.data))
elif data != None: elif data != None:
...@@ -13,18 +14,17 @@ class NormalizedCharacterImage(GrayscaleImage): ...@@ -13,18 +14,17 @@ class NormalizedCharacterImage(GrayscaleImage):
self.blur = blur self.blur = blur
self.crop_threshold = crop_threshold self.crop_threshold = crop_threshold
self.size = size self.size = size
self.gausse_filter() self.gaussian_filter()
self.increase_contrast() self.increase_contrast()
self.crop_to_letter() #self.crop_to_letter()
self.resize() #self.resize()
def increase_contrast(self): def increase_contrast(self):
self.data -= self.data.min() self.data -= self.data.min()
self.data /= self.data.max() self.data = self.data.astype(float) / self.data.max()
def gausse_filter(self): def gaussian_filter(self):
filter = GaussianFilter(1.1) GaussianFilter(self.blur).filter(self)
filter.filter(self)
def crop_to_letter(self): def crop_to_letter(self):
cropper = LetterCropper(0.9) cropper = LetterCropper(0.9)
......
C = [2 ** p for p in xrange(-5, 16, 2)]: #!/usr/bin/python
Y = [2 ** p for p in xrange(-15, 4, 2)] from cPickle import load
best_result = 0 from Classifier import Classifier
C = [float(2 ** p) for p in xrange(-5, 16, 2)]
Y = [float(2 ** p) for p in xrange(-15, 4, 2)]
best_classifier = None best_classifier = None
learning_set = load(file('learning_set', 'r')) print 'Loading learning set...'
test_set = load(file('test_set', 'r')) learning_set = load(file('learning_set.dat', 'r'))
print 'Learning set:', [c.value for c in learning_set]
print 'Loading test set...'
test_set = load(file('test_set.dat', 'r'))
print 'Test set:', [c.value for c in test_set]
# Perform a grid-search on different combinations of soft margin and gamma # Perform a grid-search on different combinations of soft margin and gamma
results = []
best = (0,)
i = 0
for c in C: for c in C:
for y in Y: for y in Y:
classifier = Classifier(c=c, gamma=y) classifier = Classifier(c=c, gamma=y)
classifier.train(learning_set) classifier.train(learning_set)
result = classifier.test(test_set) result = classifier.test(test_set)
if result > best_result: if result > best[0]:
best_classifier = classifier best = (result, c, y, classifier)
results.append(result)
i += 1
print '%d of %d, c = %f, gamma = %f, result = %d%%' \
% (i, len(C) * len(Y), c, y, int(round(result * 100)))
i = 0
print '\n c\y',
for y in Y:
print '| %f' % y,
print
for c in C:
print ' %7s' % c,
for y in Y:
print '| %8d' % int(round(results[i] * 100)),
i += 1
print
print 'c = %f, gamma = %f, result = %d%%' % (c, y, int(result * 100)) print '\nBest result: %.3f%% for C = %f and gamma = %f' % best[:3]
best_classifier.save('best_classifier') best[3].save('classifier.dat')
#!/usr/bin/python
from os import listdir
from cPickle import dump
from pylab import imshow, show
from GrayscaleImage import GrayscaleImage
from NormalizedCharacterImage import NormalizedCharacterImage
from Character import Character
c = []
for char in sorted(listdir('../images/LearningSet')):
for image in sorted(listdir('../images/LearningSet/' + char)):
f = '../images/LearningSet/' + char + '/' + image
image = GrayscaleImage(f)
norm = NormalizedCharacterImage(image, blur=1, size=(48, 36))
#imshow(norm.data, cmap='gray')
#show()
character = Character(char, [], norm)
character.get_single_cell_feature_vector()
c.append(character)
print char
dump(c, open('characters.dat', 'w+'))
...@@ -3,7 +3,7 @@ from pylab import subplot, show, imshow, axis ...@@ -3,7 +3,7 @@ from pylab import subplot, show, imshow, axis
from cPickle import load from cPickle import load
x, y = 25, 25 x, y = 25, 25
chars = load(file('chars', 'r'))[:(x * y)] chars = load(file('characters.dat', 'r'))[:(x * y)]
for i in range(x): for i in range(x):
for j in range(y): for j in range(y):
......
#!/usr/bin/python
from pylab import subplot, show, imshow, axis
from cPickle import load
chars = filter(lambda c: c.value == 'A', load(file('chars', 'r')))
for i in range(10):
for j in range(3):
index = j * 10 + i
subplot(10, 3, index + 1)
axis('off')
imshow(chars[index].image.data, cmap='gray')
show()
...@@ -3,57 +3,56 @@ from xml_helper_functions import xml_to_LicensePlate ...@@ -3,57 +3,56 @@ from xml_helper_functions import xml_to_LicensePlate
from Classifier import Classifier from Classifier import Classifier
from cPickle import dump, load from cPickle import dump, load
chars = [] chars = load(file('characters.dat', 'r'))
learning_set = []
for i in range(9): test_set = []
for j in range(100):
try:
filename = '%04d/00991_%04d%02d' % (i, i, j)
print 'loading file "%s"' % filename
# is nog steeds een licensePlate object, maar die is nu heel anders :P
plate = xml_to_LicensePlate(filename)
if hasattr(plate, 'characters'):
chars.extend(plate.characters)
except:
print 'epic fail'
print 'loaded %d chars' % len(chars) #s = {}
#
#for char in chars:
# if char.value not in s:
# s[char.value] = [char]
# else:
# s[char.value].append(char)
#
#for value, chars in s.iteritems():
# learning_set += chars[::2]
# test_set += chars[1::2]
dump(chars, file('chars', 'w+'))
#----------------------------------------------------------------
chars = load(file('chars', 'r'))[:500]
learned = [] learned = []
learning_set = []
test_set = []
for char in chars: for char in chars:
if learned.count(char.value) > 12: if learned.count(char.value) == 70:
test_set.append(char) test_set.append(char)
else: else:
learning_set.append(char) learning_set.append(char)
learned.append(char.value) learned.append(char.value)
#print 'Learning set:', [c.value for c in learning_set] print 'Learning set:', [c.value for c in learning_set]
#print 'Test set:', [c.value for c in test_set] print 'Test set:', [c.value for c in test_set]
dump(learning_set, file('learning_set', 'w+')) print 'Saving learning set...'
dump(test_set, file('test_set', 'w+')) dump(learning_set, file('learning_set.dat', 'w+'))
print 'Saving test set...'
dump(test_set, file('test_set.dat', 'w+'))
#---------------------------------------------------------------- #----------------------------------------------------------------
learning_set = load(file('learning_set', 'r')) print 'Loading learning set'
learning_set = load(file('learning_set.dat', 'r'))
# Train the classifier with the learning set # Train the classifier with the learning set
classifier = Classifier(c=30, gamma=1) classifier = Classifier(c=512, gamma=.125, cell_size=12)
classifier.train(learning_set) classifier.train(learning_set)
classifier.save('classifier') classifier.save('classifier.dat')
print 'Saved classifier'
#---------------------------------------------------------------- #----------------------------------------------------------------
classifier = Classifier(filename='classifier') print 'Loading classifier'
test_set = load(file('test_set', 'r')) classifier = Classifier(filename='classifier.dat')
print 'Loading test set'
test_set = load(file('test_set.dat', 'r'))
l = len(test_set) l = len(test_set)
matches = 0 matches = 0
for i, char in enumerate(test_set): for i, char in enumerate(test_set):
prediction = classifier.classify(char) prediction = classifier.classify(char, char.value)
if char.value == prediction: if char.value == prediction:
print ':-----> Successfully recognized "%s"' % char.value, print ':-----> Successfully recognized "%s"' % char.value,
......
#!/usr/bin/python
from GrayscaleImage import GrayscaleImage from GrayscaleImage import GrayscaleImage
from NormalizedCharacterImage import NormalizedCharacterImage from NormalizedCharacterImage import NormalizedCharacterImage
......
...@@ -5,32 +5,39 @@ from GrayscaleImage import GrayscaleImage ...@@ -5,32 +5,39 @@ from GrayscaleImage import GrayscaleImage
from cPickle import load from cPickle import load
from numpy import zeros, resize from numpy import zeros, resize
chars = load(file('chars', 'r'))[::2] chars = load(file('characters.dat', 'r'))[::2]
left = None left = None
right = None right = None
for c in chars: s = {}
if c.value == '8':
if left == None: for char in chars:
left = c.image if char.value not in s:
elif right == None: s[char.value] = [char]
right = c.image
else: else:
break s[char.value].append(char)
left = s['F'][2].image
right = s['A'][0].image
size = 16 size = 12
d = (left.size[0] * 4, left.size[1] * 4) d = (left.size[0] * 4, left.size[1] * 4)
#GrayscaleImage.resize(left, d) #GrayscaleImage.resize(left, d)
#GrayscaleImage.resize(right, d) #GrayscaleImage.resize(right, d)
p1 = LocalBinaryPatternizer(left, size) p1 = LocalBinaryPatternizer(left, size)
h1 = p1.get_single_histogram()
p1.create_features_vector() p1.create_features_vector()
p1 = p1.features p1 = p1.features
p2 = LocalBinaryPatternizer(right, size) p2 = LocalBinaryPatternizer(right, size)
h2 = p2.get_single_histogram()
p2.create_features_vector() p2.create_features_vector()
p2 = p2.features p2 = p2.features
total_intersect = h1.intersect(h2)
s = (len(p1), len(p1[0])) s = (len(p1), len(p1[0]))
match = zeros(left.shape) match = zeros(left.shape)
m = 0 m = 0
...@@ -52,6 +59,7 @@ for y in range(s[0]): ...@@ -52,6 +59,7 @@ for y in range(s[0]):
m += intersect m += intersect
print 'Match: %d%%' % int(m / (s[0] * s[1]) * 100) print 'Match: %d%%' % int(m / (s[0] * s[1]) * 100)
print 'Single histogram instersection: %d%%' % int(total_intersect * 100)
subplot(311) subplot(311)
imshow(left.data, cmap='gray') imshow(left.data, cmap='gray')
......
#!/usr/bin/python
from GaussianFilter import GaussianFilter from GaussianFilter import GaussianFilter
from GrayscaleImage import GrayscaleImage from GrayscaleImage import GrayscaleImage
......
#!/usr/bin/python
from Histogram import Histogram from Histogram import Histogram
his = Histogram(10, 10, 110) his = Histogram(10, 10, 110)
......
#!/usr/bin/python
from GrayscaleImage import GrayscaleImage from GrayscaleImage import GrayscaleImage
from LocalBinaryPatternizer import LocalBinaryPatternizer from LocalBinaryPatternizer import LocalBinaryPatternizer
......
#!/usr/bin/python
from LetterCropper import LetterCropper from LetterCropper import LetterCropper
from GrayscaleImage import GrayscaleImage from GrayscaleImage import GrayscaleImage
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment