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
a718f2e6
Commit
a718f2e6
authored
Jan 13, 2020
by
berak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ml/python: fix digits samples(3.4)
parent
c2096771
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
12 deletions
+22
-12
digits.py
samples/python/digits.py
+13
-7
digits_video.py
samples/python/digits_video.py
+9
-5
No files found.
samples/python/digits.py
View file @
a718f2e6
...
...
@@ -70,13 +70,8 @@ def deskew(img):
img
=
cv
.
warpAffine
(
img
,
M
,
(
SZ
,
SZ
),
flags
=
cv
.
WARP_INVERSE_MAP
|
cv
.
INTER_LINEAR
)
return
img
class
StatModel
(
object
):
def
load
(
self
,
fn
):
self
.
model
.
load
(
fn
)
# Known bug: https://github.com/opencv/opencv/issues/4969
def
save
(
self
,
fn
):
self
.
model
.
save
(
fn
)
class
KNearest
(
StatModel
):
class
KNearest
(
object
):
def
__init__
(
self
,
k
=
3
):
self
.
k
=
k
self
.
model
=
cv
.
ml
.
KNearest_create
()
...
...
@@ -88,7 +83,13 @@ class KNearest(StatModel):
_retval
,
results
,
_neigh_resp
,
_dists
=
self
.
model
.
findNearest
(
samples
,
self
.
k
)
return
results
.
ravel
()
class
SVM
(
StatModel
):
def
load
(
self
,
fn
):
self
.
model
=
cv
.
ml
.
KNearest_load
(
fn
)
def
save
(
self
,
fn
):
self
.
model
.
save
(
fn
)
class
SVM
(
object
):
def
__init__
(
self
,
C
=
1
,
gamma
=
0.5
):
self
.
model
=
cv
.
ml
.
SVM_create
()
self
.
model
.
setGamma
(
gamma
)
...
...
@@ -102,6 +103,11 @@ class SVM(StatModel):
def
predict
(
self
,
samples
):
return
self
.
model
.
predict
(
samples
)[
1
]
.
ravel
()
def
load
(
self
,
fn
):
self
.
model
=
cv
.
ml
.
SVM_load
(
fn
)
def
save
(
self
,
fn
):
self
.
model
.
save
(
fn
)
def
evaluate_model
(
model
,
digits
,
samples
,
labels
):
resp
=
model
.
predict
(
samples
)
...
...
samples/python/digits_video.py
View file @
a718f2e6
#!/usr/bin/env python
'''
Digit recognition from video.
Run digits.py before, to train and save the SVM.
Usage:
digits_video.py [{camera_id|video_file}]
'''
# Python 2/3 compatibility
from
__future__
import
print_function
...
...
@@ -28,11 +36,7 @@ def main():
print
(
'"
%
s" not found, run digits.py first'
%
classifier_fn
)
return
if
True
:
model
=
cv
.
ml
.
SVM_load
(
classifier_fn
)
else
:
model
=
cv
.
ml
.
SVM_create
()
model
.
load_
(
classifier_fn
)
#Known bug: https://github.com/opencv/opencv/issues/4969
model
=
cv
.
ml
.
SVM_load
(
classifier_fn
)
while
True
:
_ret
,
frame
=
cap
.
read
()
...
...
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