From 1652af5c7ddc0c1a6e4b2ed8026ea4c3479f4d7b Mon Sep 17 00:00:00 2001 From: channings Date: Thu, 6 Aug 2020 14:45:01 +0800 Subject: [PATCH] deploy code support camera (#1003) * deploy code support camera --- deploy/cpp/docs/linux_build.md | 13 +++++++------ deploy/cpp/docs/windows_vs2019_build.md | 5 +++-- deploy/cpp/src/main.cc | 11 ++++++++--- deploy/python/README.md | 5 +++-- deploy/python/infer.py | 23 ++++++++++++++++++----- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/deploy/cpp/docs/linux_build.md b/deploy/cpp/docs/linux_build.md index 1ff2e158c..f7ed83cab 100644 --- a/deploy/cpp/docs/linux_build.md +++ b/deploy/cpp/docs/linux_build.md @@ -91,11 +91,12 @@ make 编译成功后,预测入口程序为`build/main`其主要命令参数说明如下: | 参数 | 说明 | | ---- | ---- | -| model_dir | 导出的预测模型所在路径 | -| image_path | 要预测的图片文件路径 | -| video_path | 要预测的视频文件路径 | -| use_gpu | 是否使用 GPU 预测, 支持值为0或1(默认值为0)| -| gpu_id | 指定进行推理的GPU device id(默认值为0)| +| --model_dir | 导出的预测模型所在路径 | +| --image_path | 要预测的图片文件路径 | +| --video_path | 要预测的视频文件路径 | +| --camera_id | Option | 用来预测的摄像头ID,默认为-1(表示不使用摄像头预测)| +| --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`。 @@ -107,7 +108,7 @@ make ./build/main --model_dir=/root/projects/models/yolov3_darknet --image_path=/root/projects/images/test.jpeg ``` -图片文件`可视化预测结果`会保存在当前目录下`output.jpeg`文件中。 +图片文件`可视化预测结果`会保存在当前目录下`output.jpg`文件中。 `样例二`: diff --git a/deploy/cpp/docs/windows_vs2019_build.md b/deploy/cpp/docs/windows_vs2019_build.md index cb7dcfc97..111ae50e3 100644 --- a/deploy/cpp/docs/windows_vs2019_build.md +++ b/deploy/cpp/docs/windows_vs2019_build.md @@ -67,7 +67,7 @@ fluid_inference | 参数名 | 含义 | | ---- | ---- | | *CUDA_LIB | CUDA的库路径 | -| CUDNN_LIB | CUDNN的库路径 | +| *CUDNN_LIB | CUDNN的库路径 | | OPENCV_DIR | OpenCV的安装路径, | | PADDLE_DIR | Paddle预测库的路径 | @@ -95,6 +95,7 @@ cd D:\projects\PaddleDetection\deploy\cpp\out\build\x64-Release | model_dir | 导出的预测模型所在路径 | | image_path | 要预测的图片文件路径 | | video_path | 要预测的视频文件路径 | +| use_camera | 是否预测摄像头 | | use_gpu | 是否使用 GPU 预测, 支持值为0或1(默认值为0)| | gpu_id | 指定进行推理的GPU device id(默认值为0)| @@ -107,7 +108,7 @@ cd D:\projects\PaddleDetection\deploy\cpp\out\build\x64-Release .\main --model_dir=D:\\models\\yolov3_darknet --image_path=D:\\images\\test.jpeg ``` -图片文件`可视化预测结果`会保存在当前目录下`output.jpeg`文件中。 +图片文件`可视化预测结果`会保存在当前目录下`output.jpg`文件中。 `样例二`: diff --git a/deploy/cpp/src/main.cc b/deploy/cpp/src/main.cc index 94e74022a..6ad626498 100644 --- a/deploy/cpp/src/main.cc +++ b/deploy/cpp/src/main.cc @@ -27,12 +27,17 @@ 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_int32(gpu_id, 0, "Device id of GPU to execute"); +DEFINE_int32(camera_id, -1, "Device id of camera to predict"); void PredictVideo(const std::string& video_path, PaddleDetection::ObjectDetector* det) { // Open video cv::VideoCapture capture; - capture.open(video_path.c_str()); + if (FLAGS_camera_id != -1){ + capture.open(FLAGS_camera_id); + }else{ + capture.open(video_path.c_str()); + } if (!capture.isOpened()) { printf("can not open video : %s\n", video_path.c_str()); return; @@ -111,7 +116,7 @@ void PredictImage(const std::string& image_path, compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); compression_params.push_back(95); cv::imwrite("output.jpg", vis_img, compression_params); - printf("Visualized output saved as output.jpeg\n"); + printf("Visualized output saved as output.jpg\n"); } int main(int argc, char** argv) { @@ -133,7 +138,7 @@ int main(int argc, char** argv) { PaddleDetection::ObjectDetector det(FLAGS_model_dir, FLAGS_use_gpu, FLAGS_run_mode, FLAGS_gpu_id); // Do inference on input video or image - if (!FLAGS_video_path.empty()) { + if (!FLAGS_video_path.empty() or FLAGS_use_camera) { PredictVideo(FLAGS_video_path, &det); } else if (!FLAGS_image_path.empty()) { PredictImage(FLAGS_image_path, &det); diff --git a/deploy/python/README.md b/deploy/python/README.md index 9c810ae1d..9bb3bc9ef 100644 --- a/deploy/python/README.md +++ b/deploy/python/README.md @@ -42,8 +42,9 @@ python deploy/python/infer.py --model_dir=/path/to/models --image_file=/path/to/ | 参数 | 是否必须|含义 | |-------|-------|----------| | --model_dir | Yes|上述导出的模型路径 | -| --image_file | Yes |需要预测的图片 | -| --video_file | Yes |需要预测的视频 | +| --image_file | Option |需要预测的图片 | +| --video_file | Option |需要预测的视频 | +| --camera_id | Option | 用来预测的摄像头ID,默认为-1(表示不使用摄像头预测,可设置为:0 - (摄像头数目-1) ),预测过程中在可视化界面按`q`退出输出预测结果到:output/output.mp4| | --use_gpu |No|是否GPU,默认为False| | --run_mode |No|使用GPU时,默认为fluid, 可选(fluid/trt_fp32/trt_fp16)| | --threshold |No|预测得分的阈值,默认为0.5| diff --git a/deploy/python/infer.py b/deploy/python/infer.py index 0b2d5bdff..9c9d934be 100644 --- a/deploy/python/infer.py +++ b/deploy/python/infer.py @@ -566,15 +566,19 @@ def predict_image(): output_dir=FLAGS.output_dir) -def predict_video(): +def predict_video(camera_id): detector = Detector( FLAGS.model_dir, use_gpu=FLAGS.use_gpu, run_mode=FLAGS.run_mode) - capture = cv2.VideoCapture(FLAGS.video_file) + if camera_id != -1: + capture = cv2.VideoCapture(camera_id) + video_name = 'output.mp4' + else: + capture = cv2.VideoCapture(FLAGS.video_file) + video_name = os.path.split(FLAGS.video_file)[-1] fps = 30 width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc(*'mp4v') - video_name = os.path.split(FLAGS.video_file)[-1] if not os.path.exists(FLAGS.output_dir): os.makedirs(FLAGS.output_dir) out_path = os.path.join(FLAGS.output_dir, video_name) @@ -594,6 +598,10 @@ def predict_video(): mask_resolution=detector.config.mask_resolution) im = np.array(im) writer.write(im) + if camera_id != -1: + cv2.imshow('Mask Detection', im) + if cv2.waitKey(1) & 0xFF == ord('q'): + break writer.release() @@ -617,6 +625,11 @@ if __name__ == '__main__': "--image_file", type=str, default='', help="Path of image file.") parser.add_argument( "--video_file", type=str, default='', help="Path of video file.") + parser.add_argument( + "--camera_id", + type=int, + default=-1, + help="device id of camera to predict.") parser.add_argument( "--run_mode", type=str, @@ -647,5 +660,5 @@ if __name__ == '__main__': assert "Cannot predict image and video at the same time" if FLAGS.image_file != '': predict_image() - if FLAGS.video_file != '': - predict_video() + if FLAGS.video_file != '' or FLAGS.camera_id != -1: + predict_video(FLAGS.camera_id) -- GitLab