mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-13 04:13:58 +08:00
[Model] Add Picodet RKNPU2 (#635)
* * 更新picodet cpp代码 * * 更新文档 * 更新picodet cpp example * * 删除无用的debug代码 * 新增python example * * 修改c++代码 * * 修改python代码 * * 修改postprocess代码 * 修复没有scale_factor导致的bug * 修复错误 * 更正代码格式 * 更正代码格式
This commit is contained in:
@@ -14,73 +14,53 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "fastdeploy/vision.h"
|
||||
#include <sys/time.h>
|
||||
double __get_us(struct timeval t) { return (t.tv_sec * 1000000 + t.tv_usec); }
|
||||
void InferPicodet(const std::string& model_dir, const std::string& image_file);
|
||||
|
||||
void InferPicodet(const std::string& device = "cpu");
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 3) {
|
||||
std::cout
|
||||
<< "Usage: infer_demo path/to/model_dir path/to/image run_option, "
|
||||
"e.g ./infer_model ./picodet_model_dir ./test.jpeg"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
InferPicodet(argv[1], argv[2]);
|
||||
|
||||
int main() {
|
||||
InferPicodet("npu");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fastdeploy::RuntimeOption GetOption(const std::string& device) {
|
||||
void InferPicodet(const std::string& model_dir, const std::string& image_file) {
|
||||
struct timeval start_time, stop_time;
|
||||
auto model_file = model_dir + "/picodet_s_416_coco_lcnet_rk3568.rknn";
|
||||
auto params_file = "";
|
||||
auto config_file = model_dir + "/infer_cfg.yml";
|
||||
|
||||
auto option = fastdeploy::RuntimeOption();
|
||||
if (device == "npu") {
|
||||
option.UseRKNPU2();
|
||||
} else {
|
||||
option.UseCpu();
|
||||
}
|
||||
return option;
|
||||
}
|
||||
option.UseRKNPU2();
|
||||
|
||||
fastdeploy::ModelFormat GetFormat(const std::string& device) {
|
||||
auto format = fastdeploy::ModelFormat::ONNX;
|
||||
if (device == "npu") {
|
||||
format = fastdeploy::ModelFormat::RKNN;
|
||||
} else {
|
||||
format = fastdeploy::ModelFormat::ONNX;
|
||||
}
|
||||
return format;
|
||||
}
|
||||
auto format = fastdeploy::ModelFormat::RKNN;
|
||||
|
||||
std::string GetModelPath(std::string& model_path, const std::string& device) {
|
||||
if (device == "npu") {
|
||||
model_path += "rknn";
|
||||
} else {
|
||||
model_path += "onnx";
|
||||
}
|
||||
return model_path;
|
||||
}
|
||||
|
||||
void InferPicodet(const std::string &device) {
|
||||
std::string model_file = "./model/picodet_s_416_coco_lcnet/picodet_s_416_coco_lcnet_rk3588.";
|
||||
std::string params_file;
|
||||
std::string config_file = "./model/picodet_s_416_coco_lcnet/infer_cfg.yml";
|
||||
|
||||
fastdeploy::RuntimeOption option = GetOption(device);
|
||||
fastdeploy::ModelFormat format = GetFormat(device);
|
||||
model_file = GetModelPath(model_file, device);
|
||||
auto model = fastdeploy::vision::detection::RKPicoDet(
|
||||
auto model = fastdeploy::vision::detection::PicoDet(
|
||||
model_file, params_file, config_file,option,format);
|
||||
|
||||
if (!model.Initialized()) {
|
||||
std::cerr << "Failed to initialize." << std::endl;
|
||||
return;
|
||||
}
|
||||
auto image_file = "./images/000000014439.jpg";
|
||||
model.GetPostprocessor().ApplyDecodeAndNMS();
|
||||
|
||||
auto im = cv::imread(image_file);
|
||||
|
||||
fastdeploy::vision::DetectionResult res;
|
||||
clock_t start = clock();
|
||||
gettimeofday(&start_time, NULL);
|
||||
if (!model.Predict(&im, &res)) {
|
||||
std::cerr << "Failed to predict." << std::endl;
|
||||
return;
|
||||
}
|
||||
clock_t end = clock();
|
||||
auto dur = static_cast<double>(end - start);
|
||||
printf("picodet_npu use time:%f\n", (dur / CLOCKS_PER_SEC));
|
||||
gettimeofday(&stop_time, NULL);
|
||||
printf("infer use %f ms\n", (__get_us(stop_time) - __get_us(start_time)) / 1000);
|
||||
|
||||
std::cout << res.Str() << std::endl;
|
||||
auto vis_im = fastdeploy::vision::VisDetection(im, res,0.5);
|
||||
cv::imwrite("picodet_npu_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./picodet_npu_result.jpg" << std::endl;
|
||||
cv::imwrite("picodet_result.jpg", vis_im);
|
||||
std::cout << "Visualized result saved in ./picodet_result.jpg" << std::endl;
|
||||
}
|
Reference in New Issue
Block a user