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
b987154e
Commit
b987154e
authored
Jun 27, 2012
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
digits_video.py prints warning if trained classifier (should be created by digits.py) not found
parent
3804ca3e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
58 deletions
+69
-58
digits_video.py
samples/python2/digits_video.py
+69
-58
No files found.
samples/python2/digits_video.py
View file @
b987154e
import
numpy
as
np
import
cv2
#import video
import
digits
import
os
import
video
from
common
import
mosaic
#cap = video.create_capture()
cap
=
cv2
.
VideoCapture
(
0
)
model
=
digits
.
SVM
()
model
.
load
(
'digits_svm.dat'
)
SZ
=
20
while
True
:
ret
,
frame
=
cap
.
read
()
gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
bin
=
cv2
.
adaptiveThreshold
(
gray
,
255
,
cv2
.
ADAPTIVE_THRESH_MEAN_C
,
cv2
.
THRESH_BINARY_INV
,
31
,
10
)
bin
=
cv2
.
medianBlur
(
bin
,
3
)
contours
,
_
=
cv2
.
findContours
(
bin
.
copy
(),
cv2
.
RETR_LIST
,
cv2
.
CHAIN_APPROX_SIMPLE
)
boxes
=
[]
for
cnt
in
contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
cnt
)
if
h
<
20
or
h
>
60
or
1.2
*
h
<
w
:
continue
cv2
.
rectangle
(
frame
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
(
0
,
255
,
0
))
sub
=
bin
[
y
:,
x
:][:
h
,:
w
]
#sub = ~cv2.equalizeHist(sub)
#_, sub_bin = cv2.threshold(sub, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
s
=
1.1
*
h
/
SZ
m
=
cv2
.
moments
(
sub
)
m00
=
m
[
'm00'
]
if
m00
/
255
<
0.1
*
w
*
h
or
m00
/
255
>
0.9
*
w
*
h
:
continue
#frame[y:,x:][:h,:w] = sub[...,np.newaxis]
c1
=
np
.
float32
([
m
[
'm10'
],
m
[
'm01'
]])
/
m00
c0
=
np
.
float32
([
SZ
/
2
,
SZ
/
2
])
t
=
c1
-
s
*
c0
A
=
np
.
zeros
((
2
,
3
),
np
.
float32
)
A
[:,:
2
]
=
np
.
eye
(
2
)
*
2
A
[:,
2
]
=
t
sub1
=
cv2
.
warpAffine
(
sub
,
A
,
(
SZ
,
SZ
),
flags
=
cv2
.
WARP_INVERSE_MAP
|
cv2
.
INTER_LINEAR
)
sub1
=
digits
.
deskew
(
sub1
)
sample
=
np
.
float32
(
sub1
)
.
reshape
(
1
,
SZ
*
SZ
)
/
255.0
digit
=
model
.
predict
(
sample
)[
0
]
cv2
.
putText
(
frame
,
'
%
d'
%
digit
,
(
x
,
y
),
cv2
.
FONT_HERSHEY_PLAIN
,
1.0
,
(
200
,
0
,
0
),
thickness
=
1
)
boxes
.
append
(
sub1
)
if
len
(
boxes
)
>
0
:
cv2
.
imshow
(
'box'
,
mosaic
(
10
,
boxes
))
cv2
.
imshow
(
'frame'
,
frame
)
cv2
.
imshow
(
'bin'
,
bin
)
if
cv2
.
waitKey
(
1
)
==
27
:
break
def
main
():
cap
=
video
.
create_capture
()
classifier_fn
=
'digits_svm.dat'
if
not
os
.
path
.
exists
(
classifier_fn
):
print
'"
%
s" not found, run digits.py first'
%
classifier_fn
return
model
=
digits
.
SVM
()
model
.
load
(
'digits_svm.dat'
)
SZ
=
20
while
True
:
ret
,
frame
=
cap
.
read
()
gray
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2GRAY
)
bin
=
cv2
.
adaptiveThreshold
(
gray
,
255
,
cv2
.
ADAPTIVE_THRESH_MEAN_C
,
cv2
.
THRESH_BINARY_INV
,
31
,
10
)
bin
=
cv2
.
medianBlur
(
bin
,
3
)
contours
,
_
=
cv2
.
findContours
(
bin
.
copy
(),
cv2
.
RETR_LIST
,
cv2
.
CHAIN_APPROX_SIMPLE
)
boxes
=
[]
for
cnt
in
contours
:
x
,
y
,
w
,
h
=
cv2
.
boundingRect
(
cnt
)
if
h
<
20
or
h
>
60
or
1.2
*
h
<
w
:
continue
cv2
.
rectangle
(
frame
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
(
0
,
255
,
0
))
sub
=
bin
[
y
:,
x
:][:
h
,:
w
]
#sub = ~cv2.equalizeHist(sub)
#_, sub_bin = cv2.threshold(sub, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
s
=
1.1
*
h
/
SZ
m
=
cv2
.
moments
(
sub
)
m00
=
m
[
'm00'
]
if
m00
/
255
<
0.1
*
w
*
h
or
m00
/
255
>
0.9
*
w
*
h
:
continue
#frame[y:,x:][:h,:w] = sub[...,np.newaxis]
c1
=
np
.
float32
([
m
[
'm10'
],
m
[
'm01'
]])
/
m00
c0
=
np
.
float32
([
SZ
/
2
,
SZ
/
2
])
t
=
c1
-
s
*
c0
A
=
np
.
zeros
((
2
,
3
),
np
.
float32
)
A
[:,:
2
]
=
np
.
eye
(
2
)
*
2
A
[:,
2
]
=
t
sub1
=
cv2
.
warpAffine
(
sub
,
A
,
(
SZ
,
SZ
),
flags
=
cv2
.
WARP_INVERSE_MAP
|
cv2
.
INTER_LINEAR
)
sub1
=
digits
.
deskew
(
sub1
)
sample
=
np
.
float32
(
sub1
)
.
reshape
(
1
,
SZ
*
SZ
)
/
255.0
digit
=
model
.
predict
(
sample
)[
0
]
cv2
.
putText
(
frame
,
'
%
d'
%
digit
,
(
x
,
y
),
cv2
.
FONT_HERSHEY_PLAIN
,
1.0
,
(
200
,
0
,
0
),
thickness
=
1
)
boxes
.
append
(
sub1
)
if
len
(
boxes
)
>
0
:
cv2
.
imshow
(
'box'
,
mosaic
(
10
,
boxes
))
cv2
.
imshow
(
'frame'
,
frame
)
cv2
.
imshow
(
'bin'
,
bin
)
if
cv2
.
waitKey
(
1
)
==
27
:
break
if
__name__
==
'__main__'
:
main
()
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