Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
24c614f6
Commit
24c614f6
authored
Mar 19, 2013
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added load/save feature for SVM classifier in letter_recog sample
parent
a06af5ca
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
12 deletions
+32
-12
letter_recog.cpp
samples/cpp/letter_recog.cpp
+32
-12
No files found.
samples/cpp/letter_recog.cpp
View file @
24c614f6
...
@@ -131,7 +131,7 @@ int build_rtrees_classifier( char* data_filename,
...
@@ -131,7 +131,7 @@ int build_rtrees_classifier( char* data_filename,
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
return
-
1
;
return
-
1
;
}
}
printf
(
"The classifier %s is loaded.
\n
"
,
data_filename
);
printf
(
"The classifier %s is loaded.
\n
"
,
filename_to_load
);
}
}
else
else
{
{
...
@@ -262,7 +262,7 @@ int build_boost_classifier( char* data_filename,
...
@@ -262,7 +262,7 @@ int build_boost_classifier( char* data_filename,
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
return
-
1
;
return
-
1
;
}
}
printf
(
"The classifier %s is loaded.
\n
"
,
data_filename
);
printf
(
"The classifier %s is loaded.
\n
"
,
filename_to_load
);
}
}
else
else
{
{
...
@@ -403,7 +403,7 @@ int build_mlp_classifier( char* data_filename,
...
@@ -403,7 +403,7 @@ int build_mlp_classifier( char* data_filename,
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
return
-
1
;
return
-
1
;
}
}
printf
(
"The classifier %s is loaded.
\n
"
,
data_filename
);
printf
(
"The classifier %s is loaded.
\n
"
,
filename_to_load
);
}
}
else
else
{
{
...
@@ -639,10 +639,11 @@ int build_nbayes_classifier( char* data_filename )
...
@@ -639,10 +639,11 @@ int build_nbayes_classifier( char* data_filename )
}
}
static
static
int
build_svm_classifier
(
char
*
data_filename
)
int
build_svm_classifier
(
char
*
data_filename
,
const
char
*
filename_to_save
,
const
char
*
filename_to_load
)
{
{
CvMat
*
data
=
0
;
CvMat
*
data
=
0
;
CvMat
*
responses
=
0
;
CvMat
*
responses
=
0
;
CvMat
*
train_resp
=
0
;
CvMat
train_data
;
CvMat
train_data
;
int
nsamples_all
=
0
,
ntrain_samples
=
0
;
int
nsamples_all
=
0
,
ntrain_samples
=
0
;
int
var_count
;
int
var_count
;
...
@@ -666,13 +667,29 @@ int build_svm_classifier( char* data_filename )
...
@@ -666,13 +667,29 @@ int build_svm_classifier( char* data_filename )
ntrain_samples
=
(
int
)(
nsamples_all
*
0.1
);
ntrain_samples
=
(
int
)(
nsamples_all
*
0.1
);
var_count
=
data
->
cols
;
var_count
=
data
->
cols
;
// train classifier
// Create or load Random Trees classifier
printf
(
"Training the classifier (may take a few minutes)...
\n
"
);
if
(
filename_to_load
)
cvGetRows
(
data
,
&
train_data
,
0
,
ntrain_samples
);
{
CvMat
*
train_resp
=
cvCreateMat
(
ntrain_samples
,
1
,
CV_32FC1
);
// load classifier from the specified file
for
(
int
i
=
0
;
i
<
ntrain_samples
;
i
++
)
svm
.
load
(
filename_to_load
);
train_resp
->
data
.
fl
[
i
]
=
responses
->
data
.
fl
[
i
];
ntrain_samples
=
0
;
svm
.
train
(
&
train_data
,
train_resp
,
0
,
0
,
param
);
if
(
svm
.
get_var_count
()
==
0
)
{
printf
(
"Could not read the classifier %s
\n
"
,
filename_to_load
);
return
-
1
;
}
printf
(
"The classifier %s is loaded.
\n
"
,
filename_to_load
);
}
else
{
// train classifier
printf
(
"Training the classifier (may take a few minutes)...
\n
"
);
cvGetRows
(
data
,
&
train_data
,
0
,
ntrain_samples
);
train_resp
=
cvCreateMat
(
ntrain_samples
,
1
,
CV_32FC1
);
for
(
int
i
=
0
;
i
<
ntrain_samples
;
i
++
)
train_resp
->
data
.
fl
[
i
]
=
responses
->
data
.
fl
[
i
];
svm
.
train
(
&
train_data
,
train_resp
,
0
,
0
,
param
);
}
// classification
// classification
std
::
vector
<
float
>
_sample
(
var_count
*
(
nsamples_all
-
ntrain_samples
));
std
::
vector
<
float
>
_sample
(
var_count
*
(
nsamples_all
-
ntrain_samples
));
...
@@ -705,6 +722,9 @@ int build_svm_classifier( char* data_filename )
...
@@ -705,6 +722,9 @@ int build_svm_classifier( char* data_filename )
printf
(
"true_resp = %f%%
\n
"
,
(
float
)
true_resp
/
(
nsamples_all
-
ntrain_samples
)
*
100
);
printf
(
"true_resp = %f%%
\n
"
,
(
float
)
true_resp
/
(
nsamples_all
-
ntrain_samples
)
*
100
);
if
(
filename_to_save
)
svm
.
save
(
filename_to_save
);
cvReleaseMat
(
&
train_resp
);
cvReleaseMat
(
&
train_resp
);
cvReleaseMat
(
&
result
);
cvReleaseMat
(
&
result
);
cvReleaseMat
(
&
data
);
cvReleaseMat
(
&
data
);
...
@@ -775,7 +795,7 @@ int main( int argc, char *argv[] )
...
@@ -775,7 +795,7 @@ int main( int argc, char *argv[] )
method
==
4
?
method
==
4
?
build_nbayes_classifier
(
data_filename
)
:
build_nbayes_classifier
(
data_filename
)
:
method
==
5
?
method
==
5
?
build_svm_classifier
(
data_filename
)
:
build_svm_classifier
(
data_filename
,
filename_to_save
,
filename_to_load
)
:
-
1
)
<
0
)
-
1
)
<
0
)
{
{
help
();
help
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment