Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
licenseplates
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
licenseplates
Commits
24b7b1d7
Commit
24b7b1d7
authored
13 years ago
by
Jayke Meijer
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of github.com:taddeus/licenseplates
parents
ccfd5c3b
9749ba2d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Classifier.py
+9
-23
9 additions, 23 deletions
src/Classifier.py
src/ClassifierTest.py
+5
-3
5 additions, 3 deletions
src/ClassifierTest.py
with
14 additions
and
26 deletions
src/Classifier.py
+
9
−
23
View file @
24b7b1d7
from
svmutil
import
svm_train
,
svm_problem
,
svm_parameter
,
svm_predict
,
\
svm_save_model
,
svm_load_model
from
cPickle
import
dump
,
load
class
Classifier
:
def
__init__
(
self
,
c
=
None
,
filename
=
None
):
if
filename
:
# If a filename is given, load a modl from the fiven filename
self
.
model
=
svm_load_model
(
filename
+
'
-model
'
)
f
=
file
(
filename
+
'
-characters
'
,
'
r
'
)
self
.
character_map
=
load
(
f
)
f
.
close
()
# If a filename is given, load a model from the given filename
self
.
model
=
svm_load_model
(
filename
)
else
:
self
.
param
=
svm_parameter
()
self
.
param
.
kernel_type
=
2
self
.
param
.
kernel_type
=
2
# Radial kernel type
self
.
param
.
C
=
c
self
.
character_map
=
{}
self
.
model
=
None
def
save
(
self
,
filename
):
"""
Save the SVM model in the given filename.
"""
svm_save_model
(
filename
+
'
-model
'
,
self
.
model
)
f
=
file
(
filename
+
'
-characters
'
,
'
w+
'
)
dump
(
self
.
character_map
,
f
)
f
.
close
()
svm_save_model
(
filename
,
self
.
model
)
def
train
(
self
,
learning_set
):
"""
Train the classifier with a list of character objects that have
...
...
@@ -34,22 +26,16 @@ class Classifier:
for
i
,
char
in
enumerate
(
learning_set
):
print
'
Training
"
%s
"
-- %d of %d (%d%% done)
'
\
%
(
char
.
value
,
i
+
1
,
l
,
int
(
100
*
(
i
+
1
)
/
l
))
# Map the character to an integer for use in the SVM model
if
char
.
value
not
in
self
.
character_map
:
self
.
character_map
[
char
.
value
]
=
len
(
self
.
character_map
)
classes
.
append
(
self
.
character_map
[
char
.
value
])
%
(
char
.
value
,
i
+
1
,
l
,
int
(
100
*
(
i
+
1
)
/
l
))
classes
.
append
(
float
(
ord
(
char
.
value
)))
features
.
append
(
char
.
get_feature_vector
())
problem
=
svm_problem
(
classes
,
features
)
self
.
model
=
svm_train
(
problem
,
self
.
param
)
def
classify
(
self
,
character
):
"""
Classify a character object
and assig
n its value.
"""
"""
Classify a character object
, retur
n its value.
"""
predict
=
lambda
x
:
svm_predict
([
0
],
[
x
],
self
.
model
)[
0
][
0
]
prediction
=
predict
(
character
.
get_feature_vector
())
prediction
_class
=
predict
(
character
.
get_feature_vector
())
for
value
,
svm_class
in
self
.
character_map
.
iteritems
():
if
svm_class
==
prediction
:
return
value
return
chr
(
int
(
prediction_class
))
This diff is collapsed.
Click to expand it.
src/ClassifierTest.py
+
5
−
3
View file @
24b7b1d7
...
...
@@ -21,18 +21,20 @@ print 'loaded %d chars' % len(chars)
dump
(
chars
,
file
(
'
chars
'
,
'
w+
'
))
#----------------------------------------------------------------
chars
=
load
(
file
(
'
chars
'
,
'
r
'
))
chars
=
load
(
file
(
'
chars
'
,
'
r
'
))
[:
500
]
learned
=
[]
learning_set
=
[]
test_set
=
[]
for
char
in
chars
:
if
learned
.
count
(
char
.
value
)
>
80
:
if
learned
.
count
(
char
.
value
)
>
12
:
test_set
.
append
(
char
)
else
:
learning_set
.
append
(
char
)
learned
.
append
(
char
.
value
)
#print 'Learning set:', [c.value for c in learning_set]
#print 'Test set:', [c.value for c in test_set]
dump
(
learning_set
,
file
(
'
learning_set
'
,
'
w+
'
))
dump
(
test_set
,
file
(
'
test_set
'
,
'
w+
'
))
#----------------------------------------------------------------
...
...
@@ -52,7 +54,7 @@ for i, char in enumerate(test_set):
prediction
=
classifier
.
classify
(
char
)
if
char
.
value
==
prediction
:
print
'
:
) -
-----> Successfully recognized
"
%s
"'
%
char
.
value
,
print
'
:-----> Successfully recognized
"
%s
"'
%
char
.
value
,
matches
+=
1
else
:
print
'
:( Expected character
"
%s
"
, got
"
%s
"'
\
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment