Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Opencv
提交
b4b63cd6
O
Opencv
项目概览
Greenplum
/
Opencv
10 个月 前同步成功
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
Opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b4b63cd6
编写于
3月 13, 2013
作者:
M
marina.kolpakova
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add softcascade to people detector sample
上级
e4f55a7e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
148 addition
and
79 deletion
+148
-79
samples/cpp/CMakeLists.txt
samples/cpp/CMakeLists.txt
+1
-1
samples/cpp/peopledetect.cpp
samples/cpp/peopledetect.cpp
+147
-78
未找到文件。
samples/cpp/CMakeLists.txt
浏览文件 @
b4b63cd6
...
...
@@ -4,7 +4,7 @@
# ----------------------------------------------------------------------------
SET
(
OPENCV_CPP_SAMPLES_REQUIRED_DEPS opencv_core opencv_flann opencv_imgproc
opencv_highgui opencv_ml opencv_video opencv_objdetect opencv_photo opencv_nonfree
opencv_highgui opencv_ml opencv_video opencv_objdetect opencv_photo opencv_nonfree
opencv_softcascade
opencv_features2d opencv_calib3d opencv_legacy opencv_contrib opencv_stitching opencv_videostab
)
ocv_check_dependencies
(
${
OPENCV_CPP_SAMPLES_REQUIRED_DEPS
}
)
...
...
samples/cpp/peopledetect.cpp
浏览文件 @
b4b63cd6
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/softcascade/softcascade.hpp>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using
namespace
cv
;
using
namespace
std
;
// static void help()
// {
// printf(
// "\nDemonstrate the use of the HoG descriptor using\n"
// " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"
// "Usage:\n"
// "./peopledetect (<image_filename> | <image_list>.txt)\n\n");
// }
void
filter_rects
(
const
std
::
vector
<
cv
::
Rect
>&
candidates
,
std
::
vector
<
cv
::
Rect
>&
objects
);
int
main
(
int
argc
,
char
**
argv
)
{
Mat
img
;
FILE
*
f
=
0
;
char
_filename
[
1024
];
const
std
::
string
keys
=
"{help h usage ? | | print this message and exit }"
"{cascade c | | path to cascade xml, if empty HOG detector will be executed }"
"{frame f | | wildchart pattern to frame source}"
"{min_scale |0.4 | minimum scale to detect }"
"{max_scale |5.0 | maxamum scale to detect }"
"{total_scales |55 | prefered number of scales between min and max }"
"{write_file wf |0 | write to .txt. Disabled by default.}"
"{write_image wi |0 | write to image. Disabled by default.}"
"{show_image si |1 | show image. Enabled by default.}"
"{threshold thr |-1 | detection threshold. Detections with score less then threshold will be ignored.}"
;
cv
::
CommandLineParser
parser
(
argc
,
argv
,
keys
);
parser
.
about
(
"Soft cascade training application."
);
if
(
argc
==
1
)
if
(
parser
.
has
(
"help"
)
)
{
p
rintf
(
"Usage: peopledetect (<image_filename> | <image_list>.txt)
\n
"
);
p
arser
.
printMessage
(
);
return
0
;
}
img
=
imread
(
argv
[
1
]);
if
(
img
.
data
)
if
(
!
parser
.
check
())
{
parser
.
printErrors
();
return
1
;
}
int
wf
=
parser
.
get
<
int
>
(
"write_file"
);
if
(
wf
)
std
::
cout
<<
"resulte will be stored to .txt file with the same name as image."
<<
std
::
endl
;
int
wi
=
parser
.
get
<
int
>
(
"write_image"
);
if
(
wi
)
std
::
cout
<<
"resulte will be stored to image with the same name as input plus dt."
<<
std
::
endl
;
int
si
=
parser
.
get
<
int
>
(
"show_image"
);
float
minScale
=
parser
.
get
<
float
>
(
"min_scale"
);
float
maxScale
=
parser
.
get
<
float
>
(
"max_scale"
);
int
scales
=
parser
.
get
<
int
>
(
"total_scales"
);
int
thr
=
parser
.
get
<
int
>
(
"threshold"
);
cv
::
HOGDescriptor
hog
;
cv
::
softcascade
::
Detector
cascade
;
bool
useHOG
=
false
;
std
::
string
cascadePath
=
parser
.
get
<
std
::
string
>
(
"cascade"
);
if
(
cascadePath
.
empty
())
{
strcpy
(
_filename
,
argv
[
1
]);
useHOG
=
true
;
hog
.
setSVMDetector
(
cv
::
HOGDescriptor
::
getDefaultPeopleDetector
());
std
::
cout
<<
"going to use HOG detector."
<<
std
::
endl
;
}
else
{
f
=
fopen
(
argv
[
1
],
"rt"
);
if
(
!
f
)
cv
::
FileStorage
fs
(
cascadePath
,
cv
::
FileStorage
::
READ
);
if
(
!
fs
.
isOpened
()
)
{
fprintf
(
stderr
,
"ERROR: the specified file could not be loaded
\n
"
);
return
-
1
;
std
::
cout
<<
"Soft Cascade file "
<<
cascadePath
<<
" can't be opened."
<<
std
::
endl
<<
std
::
flush
;
return
1
;
}
cascade
=
cv
::
softcascade
::
Detector
(
minScale
,
maxScale
,
scales
,
cv
::
softcascade
::
Detector
::
DOLLAR
);
if
(
!
cascade
.
load
(
fs
.
getFirstTopLevelNode
()))
{
std
::
cout
<<
"Soft Cascade can't be parsed."
<<
std
::
endl
<<
std
::
flush
;
return
1
;
}
}
HOGDescriptor
hog
;
hog
.
setSVMDetector
(
HOGDescriptor
::
getDefaultPeopleDetector
());
namedWindow
(
"people detector"
,
1
);
std
::
string
src
=
parser
.
get
<
std
::
string
>
(
"frame"
);
std
::
vector
<
std
::
string
>
frames
;
cv
::
glob
(
parser
.
get
<
std
::
string
>
(
"frame"
),
frames
);
std
::
cout
<<
"collected "
<<
src
<<
" "
<<
frames
.
size
()
<<
" frames."
<<
std
::
endl
;
for
(;;
)
for
(
int
i
=
0
;
i
<
(
int
)
frames
.
size
();
++
i
)
{
char
*
filename
=
_filename
;
if
(
f
)
std
::
string
&
frame_sourse
=
frames
[
i
];
cv
::
Mat
frame
=
cv
::
imread
(
frame_sourse
);
if
(
frame
.
empty
())
{
if
(
!
fgets
(
filename
,
(
int
)
sizeof
(
_filename
)
-
2
,
f
))
break
;
//while(*filename && isspace(*filename))
// ++filename;
if
(
filename
[
0
]
==
'#'
)
continue
;
int
l
=
(
int
)
strlen
(
filename
);
while
(
l
>
0
&&
isspace
(
filename
[
l
-
1
]))
--
l
;
filename
[
l
]
=
'\0'
;
img
=
imread
(
filename
);
}
printf
(
"%s:
\n
"
,
filename
);
if
(
!
img
.
data
)
std
::
cout
<<
"Frame source "
<<
frame_sourse
<<
" can't be opened."
<<
std
::
endl
<<
std
::
flush
;
continue
;
}
std
::
ofstream
myfile
;
if
(
wf
)
myfile
.
open
((
frame_sourse
.
replace
(
frame_sourse
.
end
()
-
3
,
frame_sourse
.
end
(),
"txt"
)).
c_str
(),
std
::
ios
::
out
);
////
if
(
useHOG
)
{
std
::
vector
<
cv
::
Rect
>
found
,
found_filtered
;
// run the detector with default parameters. to get a higher hit-rate
// (and more false alarms, respectively), decrease the hitThreshold and
// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
hog
.
detectMultiScale
(
frame
,
found
,
0
,
cv
::
Size
(
8
,
8
),
cv
::
Size
(
32
,
32
),
1.05
,
2
);
filter_rects
(
found
,
found_filtered
);
std
::
cout
<<
"collected: "
<<
(
int
)
found_filtered
.
size
()
<<
" detections."
<<
std
::
endl
;
for
(
size_t
ff
=
0
;
ff
<
found_filtered
.
size
();
++
ff
)
{
cv
::
Rect
r
=
found_filtered
[
ff
];
cv
::
rectangle
(
frame
,
r
.
tl
(),
r
.
br
(),
cv
::
Scalar
(
0
,
255
,
0
),
3
);
fflush
(
stdout
);
vector
<
Rect
>
found
,
found_filtered
;
double
t
=
(
double
)
getTickCount
();
// run the detector with default parameters. to get a higher hit-rate
// (and more false alarms, respectively), decrease the hitThreshold and
// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
hog
.
detectMultiScale
(
img
,
found
,
0
,
Size
(
8
,
8
),
Size
(
32
,
32
),
1.05
,
2
);
t
=
(
double
)
getTickCount
()
-
t
;
printf
(
"tdetection time = %gms
\n
"
,
t
*
1000.
/
cv
::
getTickFrequency
());
size_t
i
,
j
;
for
(
i
=
0
;
i
<
found
.
size
();
i
++
)
if
(
wf
)
myfile
<<
r
.
x
<<
","
<<
r
.
y
<<
","
<<
r
.
width
<<
","
<<
r
.
height
<<
","
<<
0.
f
<<
"
\n
"
;
}
}
else
{
Rect
r
=
found
[
i
];
for
(
j
=
0
;
j
<
found
.
size
();
j
++
)
if
(
j
!=
i
&&
(
r
&
found
[
j
])
==
r
)
break
;
if
(
j
==
found
.
size
()
)
found_filtered
.
push_back
(
r
);
std
::
vector
<
cv
::
softcascade
::
Detection
>
objects
;
cascade
.
detect
(
frame
,
cv
::
noArray
(),
objects
);
std
::
cout
<<
"collected: "
<<
(
int
)
objects
.
size
()
<<
" detections."
<<
std
::
endl
;
for
(
int
obj
=
0
;
obj
<
(
int
)
objects
.
size
();
++
obj
)
{
cv
::
softcascade
::
Detection
d
=
objects
[
obj
];
if
(
d
.
confidence
>
thr
)
{
float
b
=
d
.
confidence
*
1.5
f
;
std
::
stringstream
conf
(
std
::
stringstream
::
in
|
std
::
stringstream
::
out
);
conf
<<
d
.
confidence
;
cv
::
rectangle
(
frame
,
cv
::
Rect
(
d
.
bb
.
x
,
d
.
bb
.
y
,
d
.
bb
.
width
,
d
.
bb
.
height
),
cv
::
Scalar
(
b
,
0
,
255
-
b
,
255
),
2
);
cv
::
putText
(
frame
,
conf
.
str
()
,
cv
::
Point
(
d
.
bb
.
x
+
10
,
d
.
bb
.
y
-
5
),
1
,
1.1
,
cv
::
Scalar
(
25
,
133
,
255
,
0
),
1
,
CV_AA
);
if
(
wf
)
myfile
<<
d
.
bb
.
x
<<
","
<<
d
.
bb
.
y
<<
","
<<
d
.
bb
.
width
<<
","
<<
d
.
bb
.
height
<<
","
<<
d
.
confidence
<<
"
\n
"
;
}
}
}
for
(
i
=
0
;
i
<
found_filtered
.
size
();
i
++
)
if
(
wi
)
cv
::
imwrite
(
frame_sourse
+
".dt.png"
,
frame
);
if
(
wf
)
myfile
.
close
();
if
(
si
)
{
Rect
r
=
found_filtered
[
i
];
// the HOG detector returns slightly larger rectangles than the real objects.
// so we slightly shrink the rectangles to get a nicer output.
r
.
x
+=
cvRound
(
r
.
width
*
0.1
);
r
.
width
=
cvRound
(
r
.
width
*
0.8
);
r
.
y
+=
cvRound
(
r
.
height
*
0.07
);
r
.
height
=
cvRound
(
r
.
height
*
0.8
);
rectangle
(
img
,
r
.
tl
(),
r
.
br
(),
cv
::
Scalar
(
0
,
255
,
0
),
3
);
cv
::
imshow
(
"pedestrian detector"
,
frame
);
cv
::
waitKey
(
10
);
}
imshow
(
"people detector"
,
img
);
int
c
=
waitKey
(
0
)
&
255
;
if
(
c
==
'q'
||
c
==
'Q'
||
!
f
)
break
;
}
if
(
f
)
fclose
(
f
);
if
(
si
)
cv
::
waitKey
(
0
);
return
0
;
}
void
filter_rects
(
const
std
::
vector
<
cv
::
Rect
>&
candidates
,
std
::
vector
<
cv
::
Rect
>&
objects
)
{
size_t
i
,
j
;
for
(
i
=
0
;
i
<
candidates
.
size
();
++
i
)
{
cv
::
Rect
r
=
candidates
[
i
];
for
(
j
=
0
;
j
<
candidates
.
size
();
++
j
)
if
(
j
!=
i
&&
(
r
&
candidates
[
j
])
==
r
)
break
;
if
(
j
==
candidates
.
size
())
objects
.
push_back
(
r
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录