diff --git a/deploy/lite_shitu/Makefile b/deploy/lite_shitu/Makefile index 9c439382a..64ee6aed8 100644 --- a/deploy/lite_shitu/Makefile +++ b/deploy/lite_shitu/Makefile @@ -53,8 +53,8 @@ LOCAL_OBJS=$(patsubst %.cpp, %.o, $(patsubst %.cc, %.o, $(LOCAL_SRCS))) JSON_OBJS = json_reader.o json_value.o json_writer.o -main: $(LOCAL_OBJS) $(JSON_OBJS) fetch_opencv - $(CC) $(SYSROOT_LINK) $(CXXFLAGS_LINK) $(LOCAL_OBJS) $(JSON_OBJS) -o main $(CXX_LIBS) $(LDFLAGS) +pp_shitu: $(LOCAL_OBJS) $(JSON_OBJS) fetch_opencv + $(CC) $(SYSROOT_LINK) $(CXXFLAGS_LINK) $(LOCAL_OBJS) $(JSON_OBJS) -o pp_shitu $(CXX_LIBS) $(LDFLAGS) fetch_opencv: @ test -d ${THIRD_PARTY_DIR} || mkdir ${THIRD_PARTY_DIR} @@ -87,4 +87,4 @@ $(JSON_OBJS): %.o: ${THIRD_PARTY_DIR}/jsoncpp_code/%.cpp fetch_json_code .PHONY: clean fetch_opencv fetch_json_code clean: rm -rf $(LOCAL_OBJS) $(JSON_OBJS) - rm -f main + rm -f pp_shitu diff --git a/deploy/lite_shitu/include/object_detector.h b/deploy/lite_shitu/include/object_detector.h index 6e4fc4ef4..779cc89d1 100644 --- a/deploy/lite_shitu/include/object_detector.h +++ b/deploy/lite_shitu/include/object_detector.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -58,7 +59,7 @@ class ObjectDetector { printf("before object detector\n"); if(config["Global"]["det_model_path"].as().empty()){ std::cout << "Please set [det_model_path] in config file" << std::endl; - return -1; + exit(-1); } LoadModel(config["Global"]["det_model_path"].as(), cpu_threads); printf("create object detector\n"); } @@ -68,7 +69,6 @@ class ObjectDetector { // Run predictor void Predict(const std::vector& imgs, - const double threshold = 0.5, const int warmup = 0, const int repeats = 1, std::vector* result = nullptr, diff --git a/deploy/lite_shitu/include/recognition.h b/deploy/lite_shitu/include/recognition.h index da945f7ea..b7d2a48c2 100644 --- a/deploy/lite_shitu/include/recognition.h +++ b/deploy/lite_shitu/include/recognition.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -37,24 +38,24 @@ struct RESULT { class Recognition { public: - void Recognition(const Json::Value &config_file) { + explicit Recognition(const Json::Value &config_file) { MobileConfig config; if (config_file["Global"]["rec_model_path"].as().empty()) { std::cout << "Please set [rec_model_path] in config file" << std::endl; - return -1; + exit(-1); } config.set_model_from_file( config_file["Global"]["rec_model_path"].as()); - predictor = CreatePaddlePredictor(config); + this->predictor = CreatePaddlePredictor(config); if (config_file["Global"]["rec_label_path"].as().empty()) { std::cout << "Please set [rec_label_path] in config file" << std::endl; - return -1; + exit(-1); } LoadLabel(config_file["Global"]["rec_label_path"].as()); SetPreProcessParam(config_file["RecPreProcess"]["transform_ops"]); - if (!config_file["Global"]["return_k"].as().empty()) - topk = config_file["Global"]["return_k"].as(); + if (!config_file["Global"].isMember("return_k")) + this->topk = config_file["Global"]["return_k"].as(); } void LoadLabel(std::string path) { @@ -68,7 +69,7 @@ public: if (pos != std::string::npos) { line = line.substr(pos); } - label_list.push_back(line); + this->label_list.push_back(line); } file.clear(); file.close(); @@ -78,33 +79,34 @@ public: for (const auto &item : config_file) { auto op_name = item["type"].as(); if (op_name == "ResizeImage") { - size = item["size"].as(); + this->size = item["size"].as(); } else if (op_name == "NormalizeImage") { - mean.clear(); - std.clear(); + this->mean.clear(); + this->std.clear(); for (auto tmp : item["mean"]) { - mean.emplace_back(tmp.as()); + this->mean.emplace_back(tmp.as()); } for (auto tmp : item["std"]) { - std.emplace_back(1 / tmp.as()); + this->std.emplace_back(1 / tmp.as()); } - scale = item["scale"].as(); + this->scale = item["scale"].as(); } } - - std::vector RunRecModel(const cv::Mat &img, double &const_time); - std::vector PostProcess(const float *output_data, int output_size, - cv::Mat &output_image); - cv::Mat ResizeImage(const cv::Mat &img); - void NeonMeanScale(const float *din, float *dout, int size); - - private: - std::shared_ptr predictor; - std::vector label_list; - std::vector mean = {0.485f, 0.456f, 0.406f}; - std::vector std = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f}; - double scale = 0.00392157; - float size = 224; - int topk = 5; } -} + + std::vector RunRecModel(const cv::Mat &img, double &cost_time); + std::vector PostProcess(const float *output_data, int output_size, + cv::Mat &output_image); + cv::Mat ResizeImage(const cv::Mat &img); + void NeonMeanScale(const float *din, float *dout, int size); + +private: + std::shared_ptr predictor; + std::vector label_list; + std::vector mean = {0.485f, 0.456f, 0.406f}; + std::vector std = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f}; + double scale = 0.00392157; + float size = 224; + int topk = 5; +}; +} // namespace PPShiTu diff --git a/deploy/lite_shitu/src/main.cc b/deploy/lite_shitu/src/main.cc index 3b8dee693..ae23e7c55 100644 --- a/deploy/lite_shitu/src/main.cc +++ b/deploy/lite_shitu/src/main.cc @@ -146,12 +146,12 @@ void DetPredictImage(const std::vector &batch_imgs, void PrintResult(const std::string &image_path, std::vector &det_result, std::vector> &rec_results) { - printf("%s:\n", img_path.c_str()); + printf("%s:\n", image_path.c_str()); for (int i = 0; i < det_result.size(); ++i) { printf("\tresult%d: bbox[%d, %d, %d, %d], score: %f, label: %s\n", i, det_result[i].rect[0], det_result[i].rect[1], det_result[i].rect[2], - det_result[t].rect[3], rec_results[i].score, - rec_results[i].class_name.c_str()); + det_result[i].rect[3], rec_results[i][0].score, + rec_results[i][0].class_name.c_str()); } } @@ -207,7 +207,7 @@ int main(int argc, char **argv) { if (!RT_Config["Global"]["infer_imgs"].as().empty() || !RT_Config["Global"]["infer_imgs_dir"].as().empty()) { std::vector all_img_paths; - std::vector cv_all_img_paths; + std::vector cv_all_img_paths; if (!RT_Config["Global"]["infer_imgs"].as().empty()) { all_img_paths.push_back( RT_Config["Global"]["infer_imgs"].as()); @@ -224,7 +224,7 @@ int main(int argc, char **argv) { } } for (int i = 0; i < all_img_paths.size(); ++i) { - std::string img_path = img_files_list[idx]; + std::string img_path = all_img_paths[i]; cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR); if (!srcimg.data) { std::cerr << "[ERROR] image read failed! image path: " << img_path diff --git a/deploy/lite_shitu/src/picodet_postprocess.cc b/deploy/lite_shitu/src/picodet_postprocess.cc index a77e0ee3f..cde914c26 100644 --- a/deploy/lite_shitu/src/picodet_postprocess.cc +++ b/deploy/lite_shitu/src/picodet_postprocess.cc @@ -16,6 +16,7 @@ // https://github.com/RangiLyu/nanodet/blob/main/demo_mnn/nanodet_mnn.cpp #include "include/picodet_postprocess.h" +#include namespace PPShiTu { diff --git a/deploy/lite_shitu/src/recognition.cc b/deploy/lite_shitu/src/recognition.cc index 1411fa0fc..8d3a99476 100644 --- a/deploy/lite_shitu/src/recognition.cc +++ b/deploy/lite_shitu/src/recognition.cc @@ -16,7 +16,7 @@ namespace PPShiTu { std::vector Recognition::RunRecModel(const cv::Mat &img, - double &const_time) { + double &cost_time) { // Read img cv::Mat resize_image = ResizeImage(img); @@ -25,7 +25,7 @@ std::vector Recognition::RunRecModel(const cv::Mat &img, resize_image.convertTo(img_fp, CV_32FC3, scale); // Prepare input data from image - std::unique_ptr input_tensor(std::move(predictor->GetInput(0))); + std::unique_ptr input_tensor(std::move(this->predictor->GetInput(0))); input_tensor->Resize({1, 3, img_fp.rows, img_fp.cols}); auto *data0 = input_tensor->mutable_data(); @@ -34,11 +34,11 @@ std::vector Recognition::RunRecModel(const cv::Mat &img, auto start = std::chrono::system_clock::now(); // Run predictor - predictor->Run(); + this->predictor->Run(); // Get output and post process std::unique_ptr output_tensor( - std::move(predictor->GetOutput(0))); + std::move(this->predictor->GetOutput(0))); auto *output_data = output_tensor->data(); auto end = std::chrono::system_clock::now(); auto duration = @@ -59,7 +59,7 @@ std::vector Recognition::RunRecModel(const cv::Mat &img, void Recognition::NeonMeanScale(const float *din, float *dout, int size) { - if (mean.size() != 3 || scale.size() != 3) { + if (this->mean.size() != 3 || this->std.size() != 3) { std::cerr << "[ERROR] mean or scale size must equal to 3\n"; exit(1); } @@ -93,31 +93,31 @@ void Recognition::NeonMeanScale(const float *din, float *dout, int size) { dout_c2 += 4; } for (; i < size; i++) { - *(dout_c0++) = (*(din++) - mean[0]) * std[0]; - *(dout_c1++) = (*(din++) - mean[1]) * std[1]; - *(dout_c2++) = (*(din++) - mean[2]) * std[2]; + *(dout_c0++) = (*(din++) - this->mean[0]) * this->std[0]; + *(dout_c1++) = (*(din++) - this->mean[1]) * this->std[1]; + *(dout_c2++) = (*(din++) - this->mean[2]) * this->std[2]; } } cv::Mat Recognition::ResizeImage(const cv::Mat &img) { cv::Mat resize_img; - cv::resize(img, resize_img, cv::Size(size, size)); + cv::resize(img, resize_img, cv::Size(this->size, this->size)); return resize_img; } std::vector Recognition::PostProcess(const float *output_data, int output_size, cv::Mat &output_image) { - int max_indices[topk]; - double max_scores[topk]; - for (int i = 0; i < topk; i++) { + int max_indices[this->topk]; + double max_scores[this->topk]; + for (int i = 0; i < this->topk; i++) { max_indices[i] = 0; max_scores[i] = 0; } for (int i = 0; i < output_size; i++) { float score = output_data[i]; int index = i; - for (int j = 0; j < topk; j++) { + for (int j = 0; j < this->topk; j++) { if (score > max_scores[j]) { index += max_indices[j]; max_indices[j] = index - max_indices[j]; @@ -129,11 +129,11 @@ std::vector Recognition::PostProcess(const float *output_data, } } - std::vector results(topk); + std::vector results(this->topk); for (int i = 0; i < results.size(); i++) { results[i].class_name = "Unknown"; - if (max_indices[i] >= 0 && max_indices[i] < label_list.size()) { - results[i].class_name = label_list[max_indices[i]]; + if (max_indices[i] >= 0 && max_indices[i] < this->label_list.size()) { + results[i].class_name = this->label_list[max_indices[i]]; } results[i].score = max_scores[i]; results[i].class_id = max_indices[i];