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
45822265
Commit
45822265
authored
Jun 17, 2011
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
work on obj_detect.py sample (in progress...)
parent
48a5599c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
46 deletions
+54
-46
obj_detect.py
samples/python2/obj_detect.py
+54
-46
No files found.
samples/python2/obj_detect.py
View file @
45822265
...
@@ -3,17 +3,16 @@ import cv2, cv
...
@@ -3,17 +3,16 @@ import cv2, cv
import
common
import
common
def
detect
(
img
,
cascade
):
def
detect
(
img
,
cascade
):
min_size
=
(
20
,
20
)
rects
=
cascade
.
detectMultiScale
(
img
,
scaleFactor
=
1.1
,
minNeighbors
=
5
,
minSize
=
(
30
,
30
))
haar_scale
=
1.1
min_neighbors
=
3
haar_flags
=
0
rects
=
cascade
.
detectMultiScale
(
img
,
haar_scale
,
min_neighbors
,
haar_flags
,
min_size
)
if
len
(
rects
)
==
0
:
if
len
(
rects
)
==
0
:
return
return
[]
rects
[:,
2
:]
+=
rects
[:,:
2
]
rects
[:,
2
:]
+=
rects
[:,:
2
]
return
rects
return
rects
def
detect_turned
(
img
,
cascade
):
def
detect_turned
(
img
,
cascade
):
img
=
cv2
.
cvtColor
(
img
,
cv
.
CV_BGR2GRAY
)
img
=
cv2
.
equalizeHist
(
img
)
img_t
=
cv2
.
transpose
(
img
)
img_t
=
cv2
.
transpose
(
img
)
img_cw
=
cv2
.
flip
(
img_t
,
1
)
img_cw
=
cv2
.
flip
(
img_t
,
1
)
img_ccw
=
cv2
.
flip
(
img_t
,
0
)
img_ccw
=
cv2
.
flip
(
img_t
,
0
)
...
@@ -22,58 +21,68 @@ def detect_turned(img, cascade):
...
@@ -22,58 +21,68 @@ def detect_turned(img, cascade):
r_ccw
=
detect
(
img_ccw
,
cascade
)
r_ccw
=
detect
(
img_ccw
,
cascade
)
h
,
w
=
img
.
shape
[:
2
]
h
,
w
=
img
.
shape
[:
2
]
if
r_cw
is
not
None
:
rects
=
[]
r_cw
[:,[
0
,
2
]]
=
h
-
r_cw
[:,[
0
,
2
]]
-
1
rects
+=
[(
x1
,
y1
,
x2
,
y2
,
1
,
0
)
for
x1
,
y1
,
x2
,
y2
in
r
]
r_cw
=
r_cw
[:,[
1
,
0
,
3
,
2
]]
rects
+=
[(
y1
,
h
-
x1
-
1
,
y2
,
h
-
x2
-
1
,
0
,
-
1
)
for
x1
,
y1
,
x2
,
y2
in
r_cw
]
if
r_ccw
is
not
None
:
rects
+=
[(
w
-
y1
-
1
,
x1
,
w
-
y2
-
1
,
x2
,
0
,
1
)
for
x1
,
y1
,
x2
,
y2
in
r_ccw
]
r_ccw
[:,[
1
,
3
]]
=
w
-
r_ccw
[:,[
1
,
3
]]
-
1
r_ccw
=
r_ccw
[:,[
1
,
0
,
3
,
2
]]
rects
=
np
.
vstack
(
[
a
for
a
in
[
r
,
r_cw
,
r_ccw
]
if
a
is
not
None
]
)
return
rects
return
rects
def
process_image
(
fn
,
cascade
):
def
process_image
(
fn
,
cascade
,
extract_faces
=
True
):
pass
img
=
cv2
.
imread
(
fn
)
h
,
w
=
img
.
shape
[:
2
]
scale
=
max
(
h
,
w
)
/
512.0
small
=
cv2
.
resize
(
img
,
(
int
(
w
/
scale
),
int
(
h
/
scale
)),
interpolation
=
cv2
.
INTER_AREA
)
rects
=
detect_turned
(
small
,
cascade
)
for
i
,
(
x1
,
y1
,
x2
,
y2
,
vx
,
vy
)
in
enumerate
(
rects
):
cv2
.
rectangle
(
small
,
(
x1
,
y1
),
(
x2
,
y2
),
(
0
,
255
,
0
))
cv2
.
circle
(
small
,
(
x1
,
y1
),
2
,
(
0
,
0
,
255
),
-
1
)
cv2
.
putText
(
small
,
str
(
i
),
((
x1
+
x2
)
/
2
,
(
y1
+
y2
)
/
2
),
cv2
.
FONT_HERSHEY_PLAIN
,
1.0
,
(
0
,
255
,
0
))
rects
=
np
.
float32
(
rects
)
.
reshape
(
-
1
,
6
)
rects
[:,:
4
]
=
np
.
around
(
rects
[:,:
4
]
*
scale
)
faces
=
[]
if
extract_faces
:
path
,
name
,
ext
=
common
.
splitfn
(
fn
)
face_sz
=
256
for
i
,
r
in
enumerate
(
rects
):
p1
,
p2
,
u
=
r
.
reshape
(
3
,
2
)
v
=
np
.
float32
(
[
-
u
[
1
],
u
[
0
]]
)
w
=
np
.
abs
(
p2
-
p1
)
.
max
()
fscale
=
w
/
face_sz
p0
=
0.5
*
(
p1
+
p2
-
w
*
(
u
+
v
))
M
=
np
.
float32
([
u
*
fscale
,
v
*
fscale
,
p0
])
.
T
face
=
cv2
.
warpAffine
(
img
,
M
,
(
face_sz
,
face_sz
),
flags
=
cv2
.
WARP_INVERSE_MAP
|
cv2
.
INTER_AREA
)
faces
.
append
(
face
)
cv2
.
imwrite
(
'out/
%
s_
%02
d.bmp'
%
(
name
,
i
),
face
)
return
small
,
rects
,
faces
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
import
sys
import
sys
import
getopt
import
getopt
from
glob
import
glob
args
,
img_mask
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'cascade='
])
args
,
img_mask
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'cascade='
])
args
=
dict
(
args
)
args
=
dict
(
args
)
# "../../data/haarcascades/haarcascade_frontalface_default.xml" #haarcascade_frontalface_default
cascade_fn
=
args
.
get
(
'--cascade'
,
"../../data/haarcascades/haarcascade_frontalface_alt.xml"
)
cascade_fn
=
args
.
get
(
'--cascade'
,
"../../data/haarcascades/haarcascade_frontalface_alt.xml"
)
cascade
=
cv2
.
CascadeClassifier
(
cascade_fn
)
cascade
=
cv2
.
CascadeClassifier
(
cascade_fn
)
mask
=
'D:/Dropbox/Photos/2011-06-12 aero/img_08[2-9]*.jpg'
img
=
cv2
.
imread
(
'test.jpg'
)
for
fn
in
glob
(
mask
):
h
,
w
=
img
.
shape
[:
2
]
print
fn
r
=
512.0
/
max
(
h
,
w
)
vis
,
rects
,
faces
=
process_image
(
fn
,
cascade
)
small
=
cv2
.
resize
(
img
,
(
int
(
w
*
r
),
int
(
h
*
r
)),
interpolation
=
cv2
.
INTER_AREA
)
cv2
.
imshow
(
'img'
,
vis
)
rects
=
detect_turned
(
small
,
cascade
)
cv2
.
waitKey
(
100
)
print
rects
for
x1
,
y1
,
x2
,
y2
in
rects
:
cv2
.
rectangle
(
small
,
(
x1
,
y1
),
(
x2
,
y2
),
(
0
,
255
,
0
))
cv2
.
circle
(
small
,
(
x1
,
y1
),
2
,
(
0
,
0
,
255
),
-
1
)
cv2
.
imshow
(
'img'
,
small
)
cv2
.
waitKey
()
#vis, rects = process_image('test.jpg', cascade)
'''
#print rects
#cv2.imshow('img', vis)
cv2
.
waitKey
()
img = cv2.imread('test.jpg')
h, w = img.shape[:2]
r = 512.0 / max(h, w)
small = cv2.resize(img, (w*r, h*r), interpolation=cv2.INTER_AREA)
cv2.imshow('img', small)
cv2.waitKey()
'''
\ No newline at end of file
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