Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
32fb7a71
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
1 年多 前同步成功
通知
696
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
32fb7a71
编写于
6月 03, 2020
作者:
C
channings
提交者:
GitHub
6月 03, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix cpp bug cannot load video (#845)
上级
8a95c4b2
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
47 addition
and
21 deletion
+47
-21
deploy/cpp/docs/linux_build.md
deploy/cpp/docs/linux_build.md
+6
-5
deploy/cpp/docs/windows_vs2019_build.md
deploy/cpp/docs/windows_vs2019_build.md
+4
-3
deploy/cpp/include/object_detector.h
deploy/cpp/include/object_detector.h
+7
-4
deploy/cpp/scripts/bootstrap.sh
deploy/cpp/scripts/bootstrap.sh
+8
-0
deploy/cpp/scripts/build.sh
deploy/cpp/scripts/build.sh
+1
-1
deploy/cpp/src/main.cc
deploy/cpp/src/main.cc
+18
-5
deploy/cpp/src/object_detector.cc
deploy/cpp/src/object_detector.cc
+3
-3
未找到文件。
deploy/cpp/docs/linux_build.md
浏览文件 @
32fb7a71
...
...
@@ -85,7 +85,7 @@ make
```
shell
sh ./scripts/build.sh
```
**注意**
: OPENCV依赖OPENBLAS,Ubuntu用户需确认系统是否已存在
`libopenblas.so`
。如未安装,可执行apt-get install libopenblas-dev进行安装。
### Step5: 预测及可视化
编译成功后,预测入口程序为
`build/main`
其主要命令参数说明如下:
...
...
@@ -95,9 +95,10 @@ make
| image_path | 要预测的图片文件路径 |
| video_path | 要预测的视频文件路径 |
| use_gpu | 是否使用 GPU 预测, 支持值为0或1(默认值为0)|
| gpu_id | 指定进行推理的GPU device id(默认值为0)|
| --run_mode |使用GPU时,默认为fluid, 可选(fluid/trt_fp32/trt_fp16)|
**注意**
:
如果同时设置了
`video_path`
和
`image_path`
,程序仅预测
`video_path`
。
**注意**
:
如果同时设置了
`video_path`
和
`image_path`
,程序仅预测
`video_path`
。
`样例一`
:
...
...
@@ -111,7 +112,7 @@ make
`样例二`
:
```
shell
#使用 `GPU`预测视频`/root/projects/videos/test.
avi
`
./build/main
--model_dir
=
/root/projects/models/yolov3_darknet
--video_path
=
/root/projects/images/test.
avi
--use_gpu
=
1
#使用 `GPU`预测视频`/root/projects/videos/test.
mp4
`
./build/main
--model_dir
=
/root/projects/models/yolov3_darknet
--video_path
=
/root/projects/images/test.
mp4
--use_gpu
=
1
```
视频文件
`可视化预测结果`
会保存在当前目录下
`output.avi
`
文件中。
视频文件
目前支持
`.mp4`
格式的预测,
`可视化预测结果`
会保存在当前目录下
`output.mp4
`
文件中。
deploy/cpp/docs/windows_vs2019_build.md
浏览文件 @
32fb7a71
...
...
@@ -96,6 +96,7 @@ cd D:\projects\PaddleDetection\deploy\cpp\out\build\x64-Release
| image_path | 要预测的图片文件路径 |
| video_path | 要预测的视频文件路径 |
| use_gpu | 是否使用 GPU 预测, 支持值为0或1(默认值为0)|
| gpu_id | 指定进行推理的GPU device id(默认值为0)|
**注意**
:如果同时设置了
`video_path`
和
`image_path`
,程序仅预测
`video_path`
。
...
...
@@ -111,8 +112,8 @@ cd D:\projects\PaddleDetection\deploy\cpp\out\build\x64-Release
`样例二`
:
```
shell
#使用`GPU`测试视频 `D:\\videos\\test.
avi
`
.
\m
ain
--model_dir
=
D:
\\
models
\\
yolov3_darknet
--video_path
=
D:
\\
videos
\\
test.
jpeg
--use_gpu
=
1
#使用`GPU`测试视频 `D:\\videos\\test.
mp4
`
.
\m
ain
--model_dir
=
D:
\\
models
\\
yolov3_darknet
--video_path
=
D:
\\
videos
\\
test.
mp4
--use_gpu
=
1
```
视频文件
`可视化预测结果`
会保存在当前目录下
`output.avi
`
文件中。
视频文件
目前支持
`.mp4`
格式的预测,
`可视化预测结果`
会保存在当前目录下
`output.mp4
`
文件中。
deploy/cpp/include/object_detector.h
浏览文件 @
32fb7a71
...
...
@@ -54,12 +54,14 @@ cv::Mat VisualizeResult(const cv::Mat& img,
class
ObjectDetector
{
public:
explicit
ObjectDetector
(
const
std
::
string
&
model_dir
,
bool
use_gpu
=
false
,
const
std
::
string
&
run_mode
=
"fluid"
)
{
explicit
ObjectDetector
(
const
std
::
string
&
model_dir
,
bool
use_gpu
=
false
,
const
std
::
string
&
run_mode
=
"fluid"
,
const
int
gpu_id
=
0
)
{
config_
.
load_config
(
model_dir
);
threshold_
=
config_
.
draw_threshold_
;
preprocessor_
.
Init
(
config_
.
preprocess_info_
,
config_
.
arch_
);
LoadModel
(
model_dir
,
use_gpu
,
config_
.
min_subgraph_size_
,
1
,
run_mode
);
LoadModel
(
model_dir
,
use_gpu
,
config_
.
min_subgraph_size_
,
1
,
run_mode
,
gpu_id
);
}
// Load Paddle inference model
...
...
@@ -68,7 +70,8 @@ class ObjectDetector {
bool
use_gpu
,
const
int
min_subgraph_size
,
const
int
batch_size
=
1
,
const
std
::
string
&
run_mode
=
"fluid"
);
const
std
::
string
&
run_mode
=
"fluid"
,
const
int
gpu_id
=
0
);
// Run predictor
void
Predict
(
...
...
deploy/cpp/scripts/bootstrap.sh
浏览文件 @
32fb7a71
# download pre-compiled opencv lib
#OPENCV_URL=https://bj.bcebos.com/paddleseg/deploy/opencv3.4.6gcc4.8ffmpeg.tar.gz2
#if [ ! -d "./deps/opencv3.4.6gcc4.8ffmpeg/" ]; then
# mkdir -p deps
# cd deps
# wget -c ${OPENCV_URL}
# tar xvfj opencv3.4.6gcc4.8ffmpeg.tar.gz2
# cd ..
#fi
OPENCV_URL
=
https://paddleseg.bj.bcebos.com/deploy/docker/opencv3gcc4.8.tar.bz2
if
[
!
-d
"./deps/opencv3gcc4.8"
]
;
then
mkdir
-p
deps
...
...
deploy/cpp/scripts/build.sh
浏览文件 @
32fb7a71
...
...
@@ -18,7 +18,7 @@ CUDNN_LIB=/path/to/cudnn/lib/
# OPENCV 路径, 如果使用自带预编译版本可不修改
sh
$(
pwd
)
/scripts/bootstrap.sh
# 下载预编译版本的opencv
OPENCV_DIR
=
$(
pwd
)
/deps/opencv3
gcc4.8
/
OPENCV_DIR
=
$(
pwd
)
/deps/opencv3
.4.6gcc4.8ffmpeg
/
# 以下无需改动
rm
-rf
build
...
...
deploy/cpp/src/main.cc
浏览文件 @
32fb7a71
...
...
@@ -25,7 +25,8 @@ DEFINE_string(model_dir, "", "Path of inference model");
DEFINE_string
(
image_path
,
""
,
"Path of input image"
);
DEFINE_string
(
video_path
,
""
,
"Path of input video"
);
DEFINE_bool
(
use_gpu
,
false
,
"Infering with GPU or CPU"
);
DEFINE_string
(
run_mode
,
"fluid"
,
"mode of running(fluid/trt_fp32/trt_fp16)"
);
DEFINE_string
(
run_mode
,
"fluid"
,
"Mode of running(fluid/trt_fp32/trt_fp16)"
);
DEFINE_int32
(
gpu_id
,
0
,
"Device id of GPU to execute"
);
void
PredictVideo
(
const
std
::
string
&
video_path
,
PaddleDetection
::
ObjectDetector
*
det
)
{
...
...
@@ -44,9 +45,9 @@ void PredictVideo(const std::string& video_path,
// Create VideoWriter for output
cv
::
VideoWriter
video_out
;
std
::
string
video_out_path
=
"output.
avi
"
;
std
::
string
video_out_path
=
"output.
mp4
"
;
video_out
.
open
(
video_out_path
.
c_str
(),
CV_FOURCC
(
'M'
,
'J'
,
'P'
,
'G'
)
,
0x00000021
,
video_fps
,
cv
::
Size
(
video_width
,
video_height
),
true
);
...
...
@@ -60,6 +61,7 @@ void PredictVideo(const std::string& video_path,
auto
colormap
=
PaddleDetection
::
GenerateColorMap
(
labels
.
size
());
// Capture all frames and do inference
cv
::
Mat
frame
;
int
frame_id
=
0
;
while
(
capture
.
read
(
frame
))
{
if
(
frame
.
empty
())
{
break
;
...
...
@@ -67,7 +69,18 @@ void PredictVideo(const std::string& video_path,
det
->
Predict
(
frame
,
&
result
);
cv
::
Mat
out_im
=
PaddleDetection
::
VisualizeResult
(
frame
,
result
,
labels
,
colormap
);
for
(
const
auto
&
item
:
result
)
{
printf
(
"In frame id %d, we detect: class=%d confidence=%.2f rect=[%d %d %d %d]
\n
"
,
frame_id
,
item
.
class_id
,
item
.
confidence
,
item
.
rect
[
0
],
item
.
rect
[
1
],
item
.
rect
[
2
],
item
.
rect
[
3
]);
}
video_out
.
write
(
out_im
);
frame_id
+=
1
;
}
capture
.
release
();
video_out
.
release
();
...
...
@@ -97,7 +110,7 @@ void PredictImage(const std::string& image_path,
std
::
vector
<
int
>
compression_params
;
compression_params
.
push_back
(
CV_IMWRITE_JPEG_QUALITY
);
compression_params
.
push_back
(
95
);
cv
::
imwrite
(
"output.jp
e
g"
,
vis_img
,
compression_params
);
cv
::
imwrite
(
"output.jpg"
,
vis_img
,
compression_params
);
printf
(
"Visualized output saved as output.jpeg
\n
"
);
}
...
...
@@ -118,7 +131,7 @@ int main(int argc, char** argv) {
// Load model and create a object detector
PaddleDetection
::
ObjectDetector
det
(
FLAGS_model_dir
,
FLAGS_use_gpu
,
FLAGS_run_mode
);
FLAGS_run_mode
,
FLAGS_gpu_id
);
// Do inference on input video or image
if
(
!
FLAGS_video_path
.
empty
())
{
PredictVideo
(
FLAGS_video_path
,
&
det
);
...
...
deploy/cpp/src/object_detector.cc
浏览文件 @
32fb7a71
...
...
@@ -21,13 +21,14 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
bool
use_gpu
,
const
int
min_subgraph_size
,
const
int
batch_size
,
const
std
::
string
&
run_mode
)
{
const
std
::
string
&
run_mode
,
const
int
gpu_id
)
{
paddle
::
AnalysisConfig
config
;
std
::
string
prog_file
=
model_dir
+
OS_PATH_SEP
+
"__model__"
;
std
::
string
params_file
=
model_dir
+
OS_PATH_SEP
+
"__params__"
;
config
.
SetModel
(
prog_file
,
params_file
);
if
(
use_gpu
)
{
config
.
EnableUseGpu
(
100
,
0
);
config
.
EnableUseGpu
(
100
,
gpu_id
);
if
(
run_mode
!=
"fluid"
)
{
auto
precision
=
paddle
::
AnalysisConfig
::
Precision
::
kFloat32
;
if
(
run_mode
==
"trt_fp16"
)
{
...
...
@@ -187,7 +188,6 @@ void ObjectDetector::Predict(const cv::Mat& im,
if
(
output_size
<
6
)
{
std
::
cerr
<<
"[WARNING] No object detected."
<<
std
::
endl
;
return
true
;
}
output_data_
.
resize
(
output_size
);
out_tensor
->
copy_to_cpu
(
output_data_
.
data
());
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录