diff --git a/examples/vision/sr/basicvsr/README.md b/examples/vision/sr/basicvsr/README.md index a077a3cbf2..fc92b84225 100644 --- a/examples/vision/sr/basicvsr/README.md +++ b/examples/vision/sr/basicvsr/README.md @@ -18,7 +18,7 @@ | 模型 | 参数大小 | 精度 | 备注 | |:----------------------------------------------------------------------------|:-------|:----- | :------ | -| [BasicVSR](https://bj.bcebos.com/paddlehub/fastdeploy/BasicVSR_reds_x4.tgz) | 30.1MB | - | +| [BasicVSR](https://bj.bcebos.com/paddlehub/fastdeploy/BasicVSR_reds_x4.tar) | 30.1MB | - | **注意**:非常不建议在没有独立显卡的设备上运行该模型 diff --git a/examples/vision/sr/basicvsr/cpp/infer.cc b/examples/vision/sr/basicvsr/cpp/infer.cc index 603179bec5..d752e143a2 100644 --- a/examples/vision/sr/basicvsr/cpp/infer.cc +++ b/examples/vision/sr/basicvsr/cpp/infer.cc @@ -20,8 +20,8 @@ const char sep = '\\'; const char sep = '/'; #endif -void CpuInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void CpuInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto model = fastdeploy::vision::sr::BasicVSR(model_file, params_file); @@ -32,34 +32,36 @@ void CpuInfer(const std::string& model_dir, } // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -67,48 +69,44 @@ void CpuInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; bool reach_end = false; - while (capture.isOpened()) - { + while (capture.isOpened()) { std::vector imgs; - for (int i = 0; i < frame_num; i++) - { + for (int i = 0; i < frame_num; i++) { capture.read(frame); - if (!frame.empty()) - { + if (!frame.empty()) { imgs.push_back(frame); - }else{ + } else { reach_end = true; } } - if (reach_end) - { + if (reach_end) { break; } std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; frame_id += 1; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } -void GpuInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void GpuInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto option = fastdeploy::RuntimeOption(); option.UseGpu(); - auto model = fastdeploy::vision::sr::BasicVSR( - model_file, params_file, option); + auto model = + fastdeploy::vision::sr::BasicVSR(model_file, params_file, option); if (!model.Initialized()) { std::cerr << "Failed to initialize." << std::endl; @@ -116,32 +114,34 @@ void GpuInfer(const std::string& model_dir, } // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -149,50 +149,48 @@ void GpuInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; bool reach_end = false; - while (capture.isOpened()) - { + while (capture.isOpened()) { std::vector imgs; - for (int i = 0; i < frame_num; i++) - { + for (int i = 0; i < frame_num; i++) { capture.read(frame); - if (!frame.empty()) - { + if (!frame.empty()) { imgs.push_back(frame); - }else{ + } else { reach_end = true; } } - if (reach_end) - { + if (reach_end) { break; } std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; frame_id += 1; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } -void TrtInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void TrtInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto option = fastdeploy::RuntimeOption(); option.UseGpu(); - option.UseTrtBackend(); // use paddle-TRT + option.UseTrtBackend(); + option.EnablePaddleTrtCollectShape(); + option.SetTrtInputShape("lrs", {1, 2, 3, 180, 320}); option.EnablePaddleToTrt(); - auto model = fastdeploy::vision::sr::BasicVSR( - model_file, params_file, option); + auto model = + fastdeploy::vision::sr::BasicVSR(model_file, params_file, option); if (!model.Initialized()) { std::cerr << "Failed to initialize." << std::endl; @@ -201,35 +199,38 @@ void TrtInfer(const std::string& model_dir, // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model - //Note that the resolution between the size and the original input is consistent when the model is exported, + // Note that the resolution between the size and the original input is + // consistent when the model is exported, // for example: [1,2,3,180,320], after 4x super separation [1,2,3,720,1080]. - //Therefore, it is very important to derive the model + // Therefore, it is very important to derive the model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -237,45 +238,41 @@ void TrtInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; bool reach_end = false; - while (capture.isOpened()) - { + while (capture.isOpened()) { std::vector imgs; - for (int i = 0; i < frame_num; i++) - { + for (int i = 0; i < frame_num; i++) { capture.read(frame); - if (!frame.empty()) - { + if (!frame.empty()) { imgs.push_back(frame); - }else{ + } else { reach_end = true; } } - if (reach_end) - { + if (reach_end) { break; } std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; frame_id += 1; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } int main(int argc, char* argv[]) { if (argc < 4) { - std::cout - << "Usage: infer_demo path/to/model_dir path/to/video frame number run_option, " - "e.g ./infer_model ./vsr_model_dir ./person.mp4 0 2" - << std::endl; + std::cout << "Usage: infer_demo path/to/model_dir path/to/video frame " + "number run_option, " + "e.g ./infer_model ./vsr_model_dir ./vsr_src.mp4 0 2" + << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." << std::endl; diff --git a/examples/vision/sr/basicvsr/python/README.md b/examples/vision/sr/basicvsr/python/README.md index d5594b70ad..ac5dd97c64 100644 --- a/examples/vision/sr/basicvsr/python/README.md +++ b/examples/vision/sr/basicvsr/python/README.md @@ -17,11 +17,11 @@ wget https://bj.bcebos.com/paddlehub/fastdeploy/BasicVSR_reds_x4.tar tar -xvf BasicVSR_reds_x4.tar wget https://bj.bcebos.com/paddlehub/fastdeploy/vsr_src.mp4 # CPU推理 -python infer.py --model BasicVSR_reds_x4 --video person.mp4 --frame_num 2 --device cpu +python infer.py --model BasicVSR_reds_x4 --video vsr_src.mp4 --frame_num 2 --device cpu # GPU推理 -python infer.py --model BasicVSR_reds_x4 --video person.mp4 --frame_num 2 --device gpu +python infer.py --model BasicVSR_reds_x4 --video vsr_src.mp4 --frame_num 2 --device gpu # GPU上使用TensorRT推理 (注意:TensorRT推理第一次运行,有序列化模型的操作,有一定耗时,需要耐心等待) -python infer.py --model BasicVSR_reds_x4 --video person.mp4 --frame_num 2 --device gpu --use_trt True +python infer.py --model BasicVSR_reds_x4 --video vsr_src.mp4 --frame_num 2 --device gpu --use_trt True ``` ## BasicVSR Python接口 diff --git a/examples/vision/sr/basicvsr/python/infer.py b/examples/vision/sr/basicvsr/python/infer.py index 8b1a9dbb73..468d3dd7bf 100644 --- a/examples/vision/sr/basicvsr/python/infer.py +++ b/examples/vision/sr/basicvsr/python/infer.py @@ -30,6 +30,8 @@ def build_option(args): option.use_gpu() if args.use_trt: option.use_trt_backend() + option.enable_paddle_trt_collect_shape() + option.set_trt_input_shape("lrs", [1, 2, 3, 180, 320]) option.enable_paddle_to_trt() return option @@ -56,7 +58,7 @@ def build_option(args): # Create VideoWriter for output video_out_dir = "./" video_out_path = os.path.join(video_out_dir, video_out_name) -fucc = cv2.VideoWriter_fourcc(*"mp4v") +fucc = cv2.VideoWriter_fourcc(* "mp4v") video_out = cv2.VideoWriter(video_out_path, fucc, video_fps, (out_width, out_height), True) if not video_out.isOpened(): diff --git a/examples/vision/sr/edvr/README.md b/examples/vision/sr/edvr/README.md index 398715cd5e..670bad8a14 100644 --- a/examples/vision/sr/edvr/README.md +++ b/examples/vision/sr/edvr/README.md @@ -18,7 +18,7 @@ | 模型 | 参数大小 | 精度 | 备注 | |:--------------------------------------------------------------------------------|:-------|:----- | :------ | -| [EDVR](https://bj.bcebos.com/paddlehub/fastdeploy/EDVR_M_wo_tsa_SRx4.tgz) | 14.9MB | - | +| [EDVR](https://bj.bcebos.com/paddlehub/fastdeploy/EDVR_M_wo_tsa_SRx4.tar) | 14.9MB | - | **注意**:非常不建议在没有独立显卡的设备上运行该模型 diff --git a/examples/vision/sr/edvr/cpp/README.md b/examples/vision/sr/edvr/cpp/README.md index ec917b824f..54ad66ee11 100644 --- a/examples/vision/sr/edvr/cpp/README.md +++ b/examples/vision/sr/edvr/cpp/README.md @@ -25,11 +25,11 @@ wget https://bj.bcebos.com/paddlehub/fastdeploy/vsr_src.mp4 # CPU推理 -./infer_demo EDVR_M_wo_tsa_SRx4 vsr_src.mp4 0 2 +./infer_demo EDVR_M_wo_tsa_SRx4 vsr_src.mp4 0 5 # GPU推理 -./infer_demo EDVR_M_wo_tsa_SRx4 vsr_src.mp4 1 2 +./infer_demo EDVR_M_wo_tsa_SRx4 vsr_src.mp4 1 5 # GPU上TensorRT推理 -./infer_demo EDVR_M_wo_tsa_SRx4 vsr_src.mp4 2 2 +./infer_demo EDVR_M_wo_tsa_SRx4 vsr_src.mp4 2 5 ``` 以上命令只适用于Linux或MacOS, Windows下SDK的使用方式请参考: diff --git a/examples/vision/sr/edvr/cpp/infer.cc b/examples/vision/sr/edvr/cpp/infer.cc index 3e9e81ca94..9cdabc6f09 100644 --- a/examples/vision/sr/edvr/cpp/infer.cc +++ b/examples/vision/sr/edvr/cpp/infer.cc @@ -20,8 +20,8 @@ const char sep = '\\'; const char sep = '/'; #endif -void CpuInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void CpuInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto model = fastdeploy::vision::sr::EDVR(model_file, params_file); @@ -32,34 +32,36 @@ void CpuInfer(const std::string& model_dir, } // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -67,42 +69,40 @@ void CpuInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; std::vector imgs; - while (capture.read(frame)){ - if (!frame.empty()) - { - if(frame_id < frame_num){ + while (capture.read(frame)) { + if (!frame.empty()) { + if (frame_id < frame_num) { imgs.push_back(frame); - frame_id ++; + frame_id++; continue; } imgs.erase(imgs.begin()); imgs.push_back(frame); } - frame_id ++; + frame_id++; std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } -void GpuInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void GpuInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto option = fastdeploy::RuntimeOption(); option.UseGpu(); - auto model = fastdeploy::vision::sr::EDVR( - model_file, params_file, option); + auto model = fastdeploy::vision::sr::EDVR(model_file, params_file, option); if (!model.Initialized()) { std::cerr << "Failed to initialize." << std::endl; @@ -110,32 +110,34 @@ void GpuInfer(const std::string& model_dir, } // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -143,44 +145,44 @@ void GpuInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; std::vector imgs; - while (capture.read(frame)){ - if (!frame.empty()) - { - if(frame_id < frame_num){ + while (capture.read(frame)) { + if (!frame.empty()) { + if (frame_id < frame_num) { imgs.push_back(frame); - frame_id ++; + frame_id++; continue; } imgs.erase(imgs.begin()); imgs.push_back(frame); } - frame_id ++; + frame_id++; std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } -void TrtInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void TrtInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto option = fastdeploy::RuntimeOption(); option.UseGpu(); - option.UseTrtBackend(); // use paddle-TRT + option.UseTrtBackend(); + option.EnablePaddleTrtCollectShape(); + option.SetTrtInputShape("x", {1, 5, 3, 180, 320}); option.EnablePaddleToTrt(); - auto model = fastdeploy::vision::sr::EDVR( - model_file, params_file, option); + auto model = fastdeploy::vision::sr::EDVR(model_file, params_file, option); if (!model.Initialized()) { std::cerr << "Failed to initialize." << std::endl; @@ -189,75 +191,77 @@ void TrtInfer(const std::string& model_dir, // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model - //Note that the resolution between the size and the original input is consistent when the model is exported, + // Note that the resolution between the size and the original input is + // consistent when the model is exported, // for example: [1,2,3,180,320], after 4x super separation [1,2,3,720,1080]. - //Therefore, it is very important to derive the model + // Therefore, it is very important to derive the model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } // Capture all frames and do inference cv::Mat frame; int frame_id = 0; - std::vector imgs; - while (capture.read(frame)){ - if (!frame.empty()) - { - if(frame_id < frame_num){ - imgs.push_back(frame); - frame_id ++; - continue; - } - imgs.erase(imgs.begin()); - imgs.push_back(frame); - } - frame_id ++; - std::vector results; - model.Predict(imgs, results); - for (auto &item : results) - { - // cv::imshow("13",item); - // cv::waitKey(30); - video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; - } + std::vector imgs; + while (capture.read(frame)) { + if (!frame.empty()) { + if (frame_id < frame_num) { + imgs.push_back(frame); + frame_id++; + continue; + } + imgs.erase(imgs.begin()); + imgs.push_back(frame); } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + frame_id++; + std::vector results; + model.Predict(imgs, results); + for (auto& item : results) { + // cv::imshow("13",item); + // cv::waitKey(30); + video_out.write(item); + std::cout << "Processing frame: " << frame_id << std::endl; + } + } + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } int main(int argc, char* argv[]) { if (argc < 4) { - std::cout - << "Usage: infer_demo path/to/model_dir path/to/video frame number run_option, " - "e.g ./infer_model ./vsr_model_dir ./person.mp4 0 2" - << std::endl; + std::cout << "Usage: infer_demo path/to/model_dir path/to/video frame " + "number run_option, " + "e.g ./infer_model ./vsr_model_dir ./vsr_src.mp4 0 5" + << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." << std::endl; diff --git a/examples/vision/sr/edvr/python/README.md b/examples/vision/sr/edvr/python/README.md index 83f0a01e3a..8875045df1 100644 --- a/examples/vision/sr/edvr/python/README.md +++ b/examples/vision/sr/edvr/python/README.md @@ -17,11 +17,11 @@ wget https://bj.bcebos.com/paddlehub/fastdeploy/EDVR_M_wo_tsa_SRx4.tar tar -xvf EDVR_M_wo_tsa_SRx4.tar wget https://bj.bcebos.com/paddlehub/fastdeploy/vsr_src.mp4 # CPU推理 -python infer.py --model EDVR_M_wo_tsa_SRx4 --video person.mp4 --frame_num 2 --device cpu +python infer.py --model EDVR_M_wo_tsa_SRx4 --video vsr_src.mp4 --frame_num 5 --device cpu # GPU推理 -python infer.py --model EDVR_M_wo_tsa_SRx4 --video person.mp4 --frame_num 2 --device gpu +python infer.py --model EDVR_M_wo_tsa_SRx4 --video vsr_src.mp4 --frame_num 5 --device gpu # GPU上使用TensorRT推理 (注意:TensorRT推理第一次运行,有序列化模型的操作,有一定耗时,需要耐心等待) -python infer.py --model EDVR_M_wo_tsa_SRx4 --video person.mp4 --frame_num 2 --device gpu --use_trt True +python infer.py --model EDVR_M_wo_tsa_SRx4 --video vsr_src.mp4 --frame_num 5 --device gpu --use_trt True ``` ## EDVR Python接口 diff --git a/examples/vision/sr/edvr/python/infer.py b/examples/vision/sr/edvr/python/infer.py index fe30bcf4f0..606e617461 100644 --- a/examples/vision/sr/edvr/python/infer.py +++ b/examples/vision/sr/edvr/python/infer.py @@ -30,6 +30,8 @@ def build_option(args): option.use_gpu() if args.use_trt: option.use_trt_backend() + option.enable_paddle_trt_collect_shape() + option.set_trt_input_shape("x", [1, 5, 3, 180, 320]) option.enable_paddle_to_trt() return option @@ -56,7 +58,7 @@ def build_option(args): # Create VideoWriter for output video_out_dir = "./" video_out_path = os.path.join(video_out_dir, video_out_name) -fucc = cv2.VideoWriter_fourcc(*"mp4v") +fucc = cv2.VideoWriter_fourcc(* "mp4v") video_out = cv2.VideoWriter(video_out_path, fucc, video_fps, (out_width, out_height), True) if not video_out.isOpened(): diff --git a/examples/vision/sr/ppmsvsr/README.md b/examples/vision/sr/ppmsvsr/README.md index 944e614462..a11e8101ed 100644 --- a/examples/vision/sr/ppmsvsr/README.md +++ b/examples/vision/sr/ppmsvsr/README.md @@ -18,7 +18,7 @@ | 模型 | 参数大小 | 精度 | 备注 | |:----------------------------------------------------------------------------|:------|:----- | :------ | -| [PP-MSVSR](https://bj.bcebos.com/paddlehub/fastdeploy/PP-MSVSR_reds_x4.tgz) | 8.8MB | - | +| [PP-MSVSR](https://bj.bcebos.com/paddlehub/fastdeploy/PP-MSVSR_reds_x4.tar) | 8.8MB | - | ## 详细部署文档 diff --git a/examples/vision/sr/ppmsvsr/cpp/infer.cc b/examples/vision/sr/ppmsvsr/cpp/infer.cc index 2267b83a82..90831c6dab 100644 --- a/examples/vision/sr/ppmsvsr/cpp/infer.cc +++ b/examples/vision/sr/ppmsvsr/cpp/infer.cc @@ -20,8 +20,8 @@ const char sep = '\\'; const char sep = '/'; #endif -void CpuInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void CpuInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto model = fastdeploy::vision::sr::PPMSVSR(model_file, params_file); @@ -32,34 +32,36 @@ void CpuInfer(const std::string& model_dir, } // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -67,51 +69,44 @@ void CpuInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; bool reach_end = false; - while (capture.isOpened()) - { + while (capture.isOpened()) { std::vector imgs; - for (int i = 0; i < frame_num; i++) - { + for (int i = 0; i < frame_num; i++) { capture.read(frame); - if (!frame.empty()) - { + if (!frame.empty()) { imgs.push_back(frame); - }else{ + } else { reach_end = true; } } - if (reach_end) - { + if (reach_end) { break; } std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; frame_id += 1; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } -void GpuInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void GpuInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto option = fastdeploy::RuntimeOption(); // use paddle-TRT option.UseGpu(); - option.UseTrtBackend(); - option.EnablePaddleToTrt(); - auto model = fastdeploy::vision::sr::PPMSVSR( - model_file, params_file, option); + auto model = fastdeploy::vision::sr::PPMSVSR(model_file, params_file, option); if (!model.Initialized()) { std::cerr << "Failed to initialize." << std::endl; @@ -119,32 +114,34 @@ void GpuInfer(const std::string& model_dir, } // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -152,48 +149,46 @@ void GpuInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; bool reach_end = false; - while (capture.isOpened()) - { + while (capture.isOpened()) { std::vector imgs; - for (int i = 0; i < frame_num; i++) - { + for (int i = 0; i < frame_num; i++) { capture.read(frame); - if (!frame.empty()) - { + if (!frame.empty()) { imgs.push_back(frame); - }else{ + } else { reach_end = true; } } - if (reach_end) - { + if (reach_end) { break; } std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; frame_id += 1; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } -void TrtInfer(const std::string& model_dir, - const std::string& video_file, int frame_num) { +void TrtInfer(const std::string& model_dir, const std::string& video_file, + int frame_num) { auto model_file = model_dir + sep + "model.pdmodel"; auto params_file = model_dir + sep + "model.pdiparams"; auto option = fastdeploy::RuntimeOption(); option.UseGpu(); option.UseTrtBackend(); - auto model = fastdeploy::vision::sr::PPMSVSR( - model_file, params_file, option); + option.EnablePaddleTrtCollectShape(); + option.SetTrtInputShape("lqs", {1, 2, 3, 180, 320}); + option.EnablePaddleToTrt(); + auto model = fastdeploy::vision::sr::PPMSVSR(model_file, params_file, option); if (!model.Initialized()) { std::cerr << "Failed to initialize." << std::endl; @@ -202,35 +197,38 @@ void TrtInfer(const std::string& model_dir, // note: input/output shape is [b, n, c, h, w] (n = frame_nums; b=1(default)) // b and n is dependent on export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md cv::VideoCapture capture; // change your save video path std::string video_out_name = "output.mp4"; capture.open(video_file); - if (!capture.isOpened()) - { - std::cout<<"can not open video "<(capture.get(cv::CAP_PROP_FPS)); - int video_frame_count = static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); + int video_frame_count = + static_cast(capture.get(cv::CAP_PROP_FRAME_COUNT)); // Set fixed size for output frame, only for msvsr model - //Note that the resolution between the size and the original input is consistent when the model is exported, + // Note that the resolution between the size and the original input is + // consistent when the model is exported, // for example: [1,2,3,180,320], after 4x super separation [1,2,3,720,1080]. - //Therefore, it is very important to derive the model + // Therefore, it is very important to derive the model int out_width = 1280; int out_height = 720; - std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count << std::endl; + std::cout << "fps: " << video_fps << "\tframe_count: " << video_frame_count + << std::endl; // Create VideoWriter for output cv::VideoWriter video_out; std::string video_out_path("./"); video_out_path += video_out_name; int fcc = cv::VideoWriter::fourcc('m', 'p', '4', 'v'); - video_out.open(video_out_path, fcc, video_fps, cv::Size(out_width, out_height), true); - if (!video_out.isOpened()) - { + video_out.open(video_out_path, fcc, video_fps, + cv::Size(out_width, out_height), true); + if (!video_out.isOpened()) { std::cout << "create video writer failed!" << std::endl; return; } @@ -238,45 +236,41 @@ void TrtInfer(const std::string& model_dir, cv::Mat frame; int frame_id = 0; bool reach_end = false; - while (capture.isOpened()) - { + while (capture.isOpened()) { std::vector imgs; - for (int i = 0; i < frame_num; i++) - { + for (int i = 0; i < frame_num; i++) { capture.read(frame); - if (!frame.empty()) - { + if (!frame.empty()) { imgs.push_back(frame); - }else{ + } else { reach_end = true; } } - if (reach_end) - { + if (reach_end) { break; } std::vector results; model.Predict(imgs, results); - for (auto &item : results) - { + for (auto& item : results) { // cv::imshow("13",item); // cv::waitKey(30); video_out.write(item); - std::cout << "Processing frame: "<< frame_id << std::endl; + std::cout << "Processing frame: " << frame_id << std::endl; frame_id += 1; } } - std::cout << "inference finished, output video saved at " << video_out_path << std::endl; + std::cout << "inference finished, output video saved at " << video_out_path + << std::endl; capture.release(); video_out.release(); } int main(int argc, char* argv[]) { if (argc < 4) { - std::cout - << "Usage: infer_demo path/to/model_dir path/to/video frame number run_option, " - "e.g ./infer_model ./vsr_model_dir ./person.mp4 0 2" - << std::endl; + std::cout << "Usage: infer_demo path/to/model_dir path/to/video frame " + "number run_option, " + "e.g ./infer_model ./vsr_model_dir ./vsr_src.mp4 0 2" + << std::endl; std::cout << "The data type of run_option is int, 0: run with cpu; 1: run " "with gpu; 2: run with gpu and use tensorrt backend." << std::endl; diff --git a/examples/vision/sr/ppmsvsr/python/README.md b/examples/vision/sr/ppmsvsr/python/README.md index 8e4c736ae2..66eea35f7d 100644 --- a/examples/vision/sr/ppmsvsr/python/README.md +++ b/examples/vision/sr/ppmsvsr/python/README.md @@ -17,11 +17,11 @@ wget https://bj.bcebos.com/paddlehub/fastdeploy/PP-MSVSR_reds_x4.tar tar -xvf PP-MSVSR_reds_x4.tar wget https://bj.bcebos.com/paddlehub/fastdeploy/vsr_src.mp4 # CPU推理 -python infer.py --model PP-MSVSR_reds_x4 --video person.mp4 --frame_num 2 --device cpu +python infer.py --model PP-MSVSR_reds_x4 --video vsr_src.mp4 --frame_num 2 --device cpu # GPU推理 -python infer.py --model PP-MSVSR_reds_x4 --video person.mp4 --frame_num 2 --device gpu +python infer.py --model PP-MSVSR_reds_x4 --video vsr_src.mp4 --frame_num 2 --device gpu # GPU上使用TensorRT推理 (注意:TensorRT推理第一次运行,有序列化模型的操作,有一定耗时,需要耐心等待) -python infer.py --model PP-MSVSR_reds_x4 --video person.mp4 --frame_num 2 --device gpu --use_trt True +python infer.py --model PP-MSVSR_reds_x4 --video vsr_src.mp4 --frame_num 2 --device gpu --use_trt True ``` ## VSR Python接口 diff --git a/examples/vision/sr/ppmsvsr/python/infer.py b/examples/vision/sr/ppmsvsr/python/infer.py index de7ab851ec..e8c7cbab95 100644 --- a/examples/vision/sr/ppmsvsr/python/infer.py +++ b/examples/vision/sr/ppmsvsr/python/infer.py @@ -30,6 +30,8 @@ def build_option(args): option.use_gpu() if args.use_trt: option.use_trt_backend() + option.enable_paddle_trt_collect_shape() + option.set_trt_input_shape("lqs", [1, 2, 3, 180, 320]) option.enable_paddle_to_trt() return option @@ -56,7 +58,7 @@ def build_option(args): # Create VideoWriter for output video_out_dir = "./" video_out_path = os.path.join(video_out_dir, video_out_name) -fucc = cv2.VideoWriter_fourcc(*"mp4v") +fucc = cv2.VideoWriter_fourcc(* "mp4v") video_out = cv2.VideoWriter(video_out_path, fucc, video_fps, (out_width, out_height), True) if not video_out.isOpened(): diff --git a/fastdeploy/vision.h b/fastdeploy/vision.h old mode 100755 new mode 100644 index 00a51cefb5..9bea1550e4 --- a/fastdeploy/vision.h +++ b/fastdeploy/vision.h @@ -15,9 +15,9 @@ #include "fastdeploy/core/config.h" #ifdef ENABLE_VISION +#include "fastdeploy/vision/classification/contrib/resnet.h" #include "fastdeploy/vision/classification/contrib/yolov5cls.h" #include "fastdeploy/vision/classification/ppcls/model.h" -#include "fastdeploy/vision/classification/contrib/resnet.h" #include "fastdeploy/vision/detection/contrib/nanodet_plus.h" #include "fastdeploy/vision/detection/contrib/scaledyolov4.h" #include "fastdeploy/vision/detection/contrib/yolor.h" @@ -29,33 +29,34 @@ #include "fastdeploy/vision/detection/contrib/yolov7end2end_trt.h" #include "fastdeploy/vision/detection/contrib/yolox.h" #include "fastdeploy/vision/detection/ppdet/model.h" +#include "fastdeploy/vision/facealign/contrib/face_landmark_1000.h" +#include "fastdeploy/vision/facealign/contrib/pfld.h" +#include "fastdeploy/vision/facealign/contrib/pipnet.h" #include "fastdeploy/vision/facedet/contrib/retinaface.h" #include "fastdeploy/vision/facedet/contrib/scrfd.h" #include "fastdeploy/vision/facedet/contrib/ultraface.h" #include "fastdeploy/vision/facedet/contrib/yolov5face.h" -#include "fastdeploy/vision/facealign/contrib/pfld.h" -#include "fastdeploy/vision/facealign/contrib/face_landmark_1000.h" -#include "fastdeploy/vision/facealign/contrib/pipnet.h" #include "fastdeploy/vision/faceid/contrib/adaface.h" #include "fastdeploy/vision/faceid/contrib/arcface.h" #include "fastdeploy/vision/faceid/contrib/cosface.h" #include "fastdeploy/vision/faceid/contrib/insightface_rec.h" #include "fastdeploy/vision/faceid/contrib/partial_fc.h" #include "fastdeploy/vision/faceid/contrib/vpl.h" +#include "fastdeploy/vision/headpose/contrib/fsanet.h" #include "fastdeploy/vision/keypointdet/pptinypose/pptinypose.h" #include "fastdeploy/vision/matting/contrib/modnet.h" #include "fastdeploy/vision/matting/contrib/rvm.h" #include "fastdeploy/vision/matting/ppmatting/ppmatting.h" #include "fastdeploy/vision/ocr/ppocr/classifier.h" #include "fastdeploy/vision/ocr/ppocr/dbdetector.h" -#include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h" #include "fastdeploy/vision/ocr/ppocr/ppocr_v2.h" #include "fastdeploy/vision/ocr/ppocr/ppocr_v3.h" #include "fastdeploy/vision/ocr/ppocr/recognizer.h" +#include "fastdeploy/vision/ocr/ppocr/utils/ocr_utils.h" #include "fastdeploy/vision/segmentation/ppseg/model.h" -#include "fastdeploy/vision/tracking/pptracking/model.h" -#include "fastdeploy/vision/headpose/contrib/fsanet.h" #include "fastdeploy/vision/sr/ppsr/model.h" +#include "fastdeploy/vision/tracking/pptracking/model.h" + #endif #include "fastdeploy/vision/visualize/visualize.h" diff --git a/fastdeploy/vision/sr/ppsr/basicvsr.cc b/fastdeploy/vision/sr/ppsr/basicvsr.cc index 6f1691cc19..02f1508244 100644 --- a/fastdeploy/vision/sr/ppsr/basicvsr.cc +++ b/fastdeploy/vision/sr/ppsr/basicvsr.cc @@ -19,12 +19,12 @@ namespace vision { namespace sr { BasicVSR::BasicVSR(const std::string& model_file, - const std::string& params_file, - const RuntimeOption& custom_option, - const ModelFormat& model_format){ + const std::string& params_file, + const RuntimeOption& custom_option, + const ModelFormat& model_format) { // unsupported ORT backend - valid_cpu_backends = {Backend::PDINFER}; - valid_gpu_backends = {Backend::PDINFER}; + valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO}; + valid_gpu_backends = {Backend::PDINFER, Backend::TRT, Backend::ORT}; runtime_option = custom_option; runtime_option.model_format = model_format; @@ -33,6 +33,6 @@ BasicVSR::BasicVSR(const std::string& model_file, initialized = Initialize(); } -} // namespace sr -} // namespace vision -} // namespace fastdeploy \ No newline at end of file +} // namespace sr +} // namespace vision +} // namespace fastdeploy \ No newline at end of file diff --git a/fastdeploy/vision/sr/ppsr/basicvsr.h b/fastdeploy/vision/sr/ppsr/basicvsr.h index 976372121d..e6523b47d2 100644 --- a/fastdeploy/vision/sr/ppsr/basicvsr.h +++ b/fastdeploy/vision/sr/ppsr/basicvsr.h @@ -19,7 +19,7 @@ namespace fastdeploy { namespace vision { namespace sr { -class FASTDEPLOY_DECL BasicVSR : public PPMSVSR{ +class FASTDEPLOY_DECL BasicVSR : public PPMSVSR { public: /** * Set path of model file and configuration file, and the configuration of runtime @@ -28,8 +28,7 @@ class FASTDEPLOY_DECL BasicVSR : public PPMSVSR{ * @param[in] custom_option RuntimeOption for inference, the default will use cpu, and choose the backend defined in `valid_cpu_backends` * @param[in] model_format Model format of the loaded model, default is Paddle format */ - BasicVSR(const std::string& model_file, - const std::string& params_file, + BasicVSR(const std::string& model_file, const std::string& params_file, const RuntimeOption& custom_option = RuntimeOption(), const ModelFormat& model_format = ModelFormat::PADDLE); /// model name contained BasicVSR diff --git a/fastdeploy/vision/sr/ppsr/edvr.cc b/fastdeploy/vision/sr/ppsr/edvr.cc index f9bb4e0cc1..6c73f6183b 100644 --- a/fastdeploy/vision/sr/ppsr/edvr.cc +++ b/fastdeploy/vision/sr/ppsr/edvr.cc @@ -18,13 +18,12 @@ namespace fastdeploy { namespace vision { namespace sr { -EDVR::EDVR(const std::string& model_file, - const std::string& params_file, +EDVR::EDVR(const std::string& model_file, const std::string& params_file, const RuntimeOption& custom_option, - const ModelFormat& model_format){ + const ModelFormat& model_format) { // unsupported ORT backend - valid_cpu_backends = {Backend::PDINFER}; - valid_gpu_backends = {Backend::PDINFER}; + valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO}; + valid_gpu_backends = {Backend::PDINFER, Backend::TRT, Backend::ORT}; runtime_option = custom_option; runtime_option.model_format = model_format; @@ -34,28 +33,31 @@ EDVR::EDVR(const std::string& model_file, initialized = Initialize(); } -bool EDVR::Postprocess(std::vector& infer_results, std::vector& results){ +bool EDVR::Postprocess(std::vector& infer_results, + std::vector& results) { // group to image // output_shape is [b, n, c, h, w] n = frame_nums b=1(default) // b and n is dependence export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md auto output_shape = infer_results[0].shape; // EDVR int h_ = output_shape[2]; int w_ = output_shape[3]; int c_ = output_shape[1]; int frame_num = 1; - float *out_data = static_cast(infer_results[0].Data()); - cv::Mat temp = cv::Mat::zeros(h_, w_, CV_32FC3); // RGB image + float* out_data = static_cast(infer_results[0].Data()); + cv::Mat temp = cv::Mat::zeros(h_, w_, CV_32FC3); // RGB image int pix_num = h_ * w_; int frame_pix_num = pix_num * c_; for (int frame = 0; frame < frame_num; frame++) { int index = 0; for (int h = 0; h < h_; ++h) { for (int w = 0; w < w_; ++w) { - temp.at(h, w) = {out_data[2 * pix_num + index + frame_pix_num * frame], - out_data[pix_num + index + frame_pix_num * frame], - out_data[index + frame_pix_num * frame]}; + temp.at(h, w) = { + out_data[2 * pix_num + index + frame_pix_num * frame], + out_data[pix_num + index + frame_pix_num * frame], + out_data[index + frame_pix_num * frame]}; index += 1; } } @@ -66,6 +68,6 @@ bool EDVR::Postprocess(std::vector& infer_results, std::vector& output) { - BGR2RGB::Run(mat); Normalize::Run(mat, mean_, scale_, true); HWC2CHW::Run(mat); // Csat float - float* ptr = static_cast(mat->Data()); + float* ptr = static_cast(mat->Data()); size_t size = mat->Width() * mat->Height() * mat->Channels(); output = std::vector(ptr, ptr + size); return true; } -bool PPMSVSR::Predict(std::vector& imgs, std::vector& results) { - - // Theoretically, the more frame nums there are, the better the result will be, - // but it will lead to a significant increase in memory +bool PPMSVSR::Predict(std::vector& imgs, + std::vector& results) { + // Theoretically, the more frame nums there are, the better the result will + // be, but it will lead to a significant increase in memory int frame_num = imgs.size(); int rows = imgs[0].rows; int cols = imgs[0].cols; @@ -71,11 +69,12 @@ bool PPMSVSR::Predict(std::vector& imgs, std::vector& results) Mat mat(imgs[i]); std::vector data_temp; Preprocess(&mat, data_temp); - all_data_temp.insert(all_data_temp.end(), data_temp.begin(), data_temp.end()); + all_data_temp.insert(all_data_temp.end(), data_temp.begin(), + data_temp.end()); } // share memory in order to avoid memory copy, data type must be float32 - input_tensors[0].SetExternalData({1 ,frame_num , channels, rows, cols}, FDDataType::FP32, - all_data_temp.data()); + input_tensors[0].SetExternalData({1, frame_num, channels, rows, cols}, + FDDataType::FP32, all_data_temp.data()); input_tensors[0].shape = {1, frame_num, channels, rows, cols}; input_tensors[0].name = InputInfoOfRuntime(0).name; std::vector output_tensors; @@ -90,11 +89,13 @@ bool PPMSVSR::Predict(std::vector& imgs, std::vector& results) return true; } -bool PPMSVSR::Postprocess(std::vector& infer_results, std::vector& results){ +bool PPMSVSR::Postprocess(std::vector& infer_results, + std::vector& results) { // group to image // output_shape is [b, n, c, h, w] n = frame_nums b=1(default) // b and n is dependence export model shape - // see https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md + // see + // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md auto output_shape = infer_results[0].shape; // PP-MSVSR int h_ = output_shape[3]; @@ -102,17 +103,18 @@ bool PPMSVSR::Postprocess(std::vector& infer_results, std::vector(infer_results[0].Data()); - cv::Mat temp = cv::Mat::zeros(h_, w_, CV_32FC3); // RGB image + float* out_data = static_cast(infer_results[0].Data()); + cv::Mat temp = cv::Mat::zeros(h_, w_, CV_32FC3); // RGB image int pix_num = h_ * w_; int frame_pix_num = pix_num * c_; for (int frame = 0; frame < frame_num; frame++) { int index = 0; for (int h = 0; h < h_; ++h) { for (int w = 0; w < w_; ++w) { - temp.at(h, w) = {out_data[2 * pix_num + index + frame_pix_num * frame], - out_data[pix_num + index + frame_pix_num * frame], - out_data[index + frame_pix_num * frame]}; + temp.at(h, w) = { + out_data[2 * pix_num + index + frame_pix_num * frame], + out_data[pix_num + index + frame_pix_num * frame], + out_data[index + frame_pix_num * frame]}; index += 1; } } @@ -123,6 +125,6 @@ bool PPMSVSR::Postprocess(std::vector& infer_results, std::vector(m, "PPMSVSR") - .def(pybind11::init()) - .def("predict", [](vision::sr::PPMSVSR& self, std::vector& datas){ - std::vector inputs; - for (auto& data: datas){ - auto mat = PyArrayToCvMat(data); - inputs.push_back(mat); - } - std::vector res; - std::vector res_pyarray; - self.Predict(inputs, res); - for (auto& img: res){ - auto ret = pybind11::array_t({img.rows, img.cols, img.channels()}, img.data); - res_pyarray.push_back(ret); - } - return res_pyarray; - }); - pybind11::class_(m, "EDVR") - .def(pybind11::init()) - .def("predict", [](vision::sr::EDVR& self, std::vector& datas){ - std::vector inputs; - for (auto& data: datas){ - auto mat = PyArrayToCvMat(data); - inputs.push_back(mat); - } - std::vector res; - std::vector res_pyarray; - self.Predict(inputs, res); - for (auto& img: res){ - auto ret = pybind11::array_t({img.rows, img.cols, img.channels()}, img.data); - res_pyarray.push_back(ret); - } - return res_pyarray; - }); - pybind11::class_(m, "BasicVSR") - .def(pybind11::init()) - .def("predict", [](vision::sr::BasicVSR& self, std::vector& datas){ - std::vector inputs; - for (auto& data: datas){ - auto mat = PyArrayToCvMat(data); - inputs.push_back(mat); - } - std::vector res; - std::vector res_pyarray; - self.Predict(inputs, res); - for (auto& img: res){ - auto ret = pybind11::array_t({img.rows, img.cols, img.channels()}, img.data); - res_pyarray.push_back(ret); - } - return res_pyarray; - }); +namespace fastdeploy { +void BindPPSR(pybind11::module& m) { + pybind11::class_(m, "PPMSVSR") + .def(pybind11::init()) + .def("predict", + [](vision::sr::PPMSVSR& self, std::vector& datas) { + std::vector inputs; + for (auto& data : datas) { + auto mat = PyArrayToCvMat(data); + inputs.push_back(mat); + } + std::vector res; + std::vector res_pyarray; + self.Predict(inputs, res); + for (auto& img : res) { + auto ret = pybind11::array_t( + {img.rows, img.cols, img.channels()}, img.data); + res_pyarray.push_back(ret); + } + return res_pyarray; + }); + pybind11::class_(m, "EDVR") + .def(pybind11::init()) + .def("predict", + [](vision::sr::EDVR& self, std::vector& datas) { + std::vector inputs; + for (auto& data : datas) { + auto mat = PyArrayToCvMat(data); + inputs.push_back(mat); + } + std::vector res; + std::vector res_pyarray; + self.Predict(inputs, res); + for (auto& img : res) { + auto ret = pybind11::array_t( + {img.rows, img.cols, img.channels()}, img.data); + res_pyarray.push_back(ret); + } + return res_pyarray; + }); + pybind11::class_(m, "BasicVSR") + .def(pybind11::init()) + .def("predict", + [](vision::sr::BasicVSR& self, std::vector& datas) { + std::vector inputs; + for (auto& data : datas) { + auto mat = PyArrayToCvMat(data); + inputs.push_back(mat); + } + std::vector res; + std::vector res_pyarray; + self.Predict(inputs, res); + for (auto& img : res) { + auto ret = pybind11::array_t( + {img.rows, img.cols, img.channels()}, img.data); + res_pyarray.push_back(ret); + } + return res_pyarray; + }); } } // namespace fastdeploy diff --git a/fastdeploy/vision/sr/sr_pybind.cc b/fastdeploy/vision/sr/sr_pybind.cc index 03be14df37..5f421d7ddb 100644 --- a/fastdeploy/vision/sr/sr_pybind.cc +++ b/fastdeploy/vision/sr/sr_pybind.cc @@ -16,10 +16,10 @@ namespace fastdeploy { - void BindPPSR(pybind11::module& m); +void BindPPSR(pybind11::module& m); - void BindSR(pybind11::module& m) { - auto sr_module = m.def_submodule("sr", "sr(super resolution) submodule"); - BindPPSR(sr_module); - } +void BindSR(pybind11::module& m) { + auto sr_module = m.def_submodule("sr", "sr(super resolution) submodule"); + BindPPSR(sr_module); +} } // namespace fastdeploy diff --git a/fastdeploy/vision/vision_pybind.cc b/fastdeploy/vision/vision_pybind.cc old mode 100755 new mode 100644 index c2cacae5c7..f08b77b69f --- a/fastdeploy/vision/vision_pybind.cc +++ b/fastdeploy/vision/vision_pybind.cc @@ -115,7 +115,8 @@ void BindVision(pybind11::module& m) { .def("__repr__", &vision::MattingResult::Str) .def("__str__", &vision::MattingResult::Str); - pybind11::class_(m, "KeyPointDetectionResult") + pybind11::class_(m, + "KeyPointDetectionResult") .def(pybind11::init()) .def_readwrite("keypoints", &vision::KeyPointDetectionResult::keypoints) .def_readwrite("scores", &vision::KeyPointDetectionResult::scores) @@ -129,8 +130,10 @@ void BindVision(pybind11::module& m) { .def("__repr__", &vision::HeadPoseResult::Str) .def("__str__", &vision::HeadPoseResult::Str); - m.def("enable_flycv", &vision::EnableFlyCV, "Enable image preprocessing by FlyCV."); - m.def("disable_flycv", &vision::DisableFlyCV, "Disable image preprocessing by FlyCV, change to use OpenCV."); + m.def("enable_flycv", &vision::EnableFlyCV, + "Enable image preprocessing by FlyCV."); + m.def("disable_flycv", &vision::DisableFlyCV, + "Disable image preprocessing by FlyCV, change to use OpenCV."); BindDetection(m); BindClassification(m);