Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Opencv
提交
1f214d23
O
Opencv
项目概览
Greenplum
/
Opencv
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
1f214d23
编写于
10月 27, 2017
作者:
V
Vadim Pisarevsky
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9952 from catree:add_resnet_ssd_face.cpp
上级
ede39b85
b4813e6b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
160 addition
and
0 deletion
+160
-0
samples/dnn/resnet_ssd_face.cpp
samples/dnn/resnet_ssd_face.cpp
+160
-0
未找到文件。
samples/dnn/resnet_ssd_face.cpp
0 → 100644
浏览文件 @
1f214d23
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using
namespace
cv
;
using
namespace
cv
::
dnn
;
#include <iostream>
#include <cstdlib>
using
namespace
std
;
const
size_t
inWidth
=
300
;
const
size_t
inHeight
=
300
;
const
double
inScaleFactor
=
1.0
;
const
Scalar
meanVal
(
104.0
,
177.0
,
123.0
);
const
char
*
about
=
"This sample uses Single-Shot Detector "
"(https://arxiv.org/abs/1512.02325) "
"with ResNet-10 architecture to detect faces on camera/video/image.
\n
"
"More information about the training is available here: "
"<OPENCV_SRC_DIR>/samples/dnn/face_detector/how_to_train_face_detector.txt
\n
"
".caffemodel model's file is available here: "
"<OPENCV_SRC_DIR>/samples/dnn/face_detector/res10_300x300_ssd_iter_140000.caffemodel
\n
"
".prototxt file is available here: "
"<OPENCV_SRC_DIR>/samples/dnn/face_detector/deploy.prototxt
\n
"
;
const
char
*
params
=
"{ help | false | print usage }"
"{ proto | | model configuration (deploy.prototxt) }"
"{ model | | model weights (res10_300x300_ssd_iter_140000.caffemodel) }"
"{ camera_device | 0 | camera device number }"
"{ video | | video or image for detection }"
"{ min_confidence | 0.5 | min confidence }"
;
int
main
(
int
argc
,
char
**
argv
)
{
CommandLineParser
parser
(
argc
,
argv
,
params
);
if
(
parser
.
get
<
bool
>
(
"help"
))
{
cout
<<
about
<<
endl
;
parser
.
printMessage
();
return
0
;
}
String
modelConfiguration
=
parser
.
get
<
string
>
(
"proto"
);
String
modelBinary
=
parser
.
get
<
string
>
(
"model"
);
//! [Initialize network]
dnn
::
Net
net
=
readNetFromCaffe
(
modelConfiguration
,
modelBinary
);
//! [Initialize network]
if
(
net
.
empty
())
{
cerr
<<
"Can't load network by using the following files: "
<<
endl
;
cerr
<<
"prototxt: "
<<
modelConfiguration
<<
endl
;
cerr
<<
"caffemodel: "
<<
modelBinary
<<
endl
;
cerr
<<
"Models are available here:"
<<
endl
;
cerr
<<
"<OPENCV_SRC_DIR>/samples/dnn/face_detector"
<<
endl
;
cerr
<<
"or here:"
<<
endl
;
cerr
<<
"https://github.com/opencv/opencv/tree/master/samples/dnn/face_detector"
<<
endl
;
exit
(
-
1
);
}
VideoCapture
cap
;
if
(
parser
.
get
<
String
>
(
"video"
).
empty
())
{
int
cameraDevice
=
parser
.
get
<
int
>
(
"camera_device"
);
cap
=
VideoCapture
(
cameraDevice
);
if
(
!
cap
.
isOpened
())
{
cout
<<
"Couldn't find camera: "
<<
cameraDevice
<<
endl
;
return
-
1
;
}
}
else
{
cap
.
open
(
parser
.
get
<
String
>
(
"video"
));
if
(
!
cap
.
isOpened
())
{
cout
<<
"Couldn't open image or video: "
<<
parser
.
get
<
String
>
(
"video"
)
<<
endl
;
return
-
1
;
}
}
for
(;;)
{
Mat
frame
;
cap
>>
frame
;
// get a new frame from camera/video or read image
if
(
frame
.
empty
())
{
waitKey
();
break
;
}
if
(
frame
.
channels
()
==
4
)
cvtColor
(
frame
,
frame
,
COLOR_BGRA2BGR
);
//! [Prepare blob]
Mat
inputBlob
=
blobFromImage
(
frame
,
inScaleFactor
,
Size
(
inWidth
,
inHeight
),
meanVal
,
false
,
false
);
//Convert Mat to batch of images
//! [Prepare blob]
//! [Set input blob]
net
.
setInput
(
inputBlob
,
"data"
);
//set the network input
//! [Set input blob]
//! [Make forward pass]
Mat
detection
=
net
.
forward
(
"detection_out"
);
//compute output
//! [Make forward pass]
vector
<
double
>
layersTimings
;
double
freq
=
getTickFrequency
()
/
1000
;
double
time
=
net
.
getPerfProfile
(
layersTimings
)
/
freq
;
Mat
detectionMat
(
detection
.
size
[
2
],
detection
.
size
[
3
],
CV_32F
,
detection
.
ptr
<
float
>
());
ostringstream
ss
;
ss
<<
"FPS: "
<<
1000
/
time
<<
" ; time: "
<<
time
<<
" ms"
;
putText
(
frame
,
ss
.
str
(),
Point
(
20
,
20
),
0
,
0.5
,
Scalar
(
0
,
0
,
255
));
float
confidenceThreshold
=
parser
.
get
<
float
>
(
"min_confidence"
);
for
(
int
i
=
0
;
i
<
detectionMat
.
rows
;
i
++
)
{
float
confidence
=
detectionMat
.
at
<
float
>
(
i
,
2
);
if
(
confidence
>
confidenceThreshold
)
{
int
xLeftBottom
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
3
)
*
frame
.
cols
);
int
yLeftBottom
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
4
)
*
frame
.
rows
);
int
xRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
5
)
*
frame
.
cols
);
int
yRightTop
=
static_cast
<
int
>
(
detectionMat
.
at
<
float
>
(
i
,
6
)
*
frame
.
rows
);
Rect
object
((
int
)
xLeftBottom
,
(
int
)
yLeftBottom
,
(
int
)(
xRightTop
-
xLeftBottom
),
(
int
)(
yRightTop
-
yLeftBottom
));
rectangle
(
frame
,
object
,
Scalar
(
0
,
255
,
0
));
ss
.
str
(
""
);
ss
<<
confidence
;
String
conf
(
ss
.
str
());
String
label
=
"Face: "
+
conf
;
int
baseLine
=
0
;
Size
labelSize
=
getTextSize
(
label
,
FONT_HERSHEY_SIMPLEX
,
0.5
,
1
,
&
baseLine
);
rectangle
(
frame
,
Rect
(
Point
(
xLeftBottom
,
yLeftBottom
-
labelSize
.
height
),
Size
(
labelSize
.
width
,
labelSize
.
height
+
baseLine
)),
Scalar
(
255
,
255
,
255
),
CV_FILLED
);
putText
(
frame
,
label
,
Point
(
xLeftBottom
,
yLeftBottom
),
FONT_HERSHEY_SIMPLEX
,
0.5
,
Scalar
(
0
,
0
,
0
));
}
}
imshow
(
"detections"
,
frame
);
if
(
waitKey
(
1
)
>=
0
)
break
;
}
return
0
;
}
// main
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录