提交 96e3a208 编写于 作者: D dongshuilong

fix ppshitu_lite bugs

上级 4639b6f1
...@@ -53,8 +53,8 @@ LOCAL_OBJS=$(patsubst %.cpp, %.o, $(patsubst %.cc, %.o, $(LOCAL_SRCS))) ...@@ -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 JSON_OBJS = json_reader.o json_value.o json_writer.o
main: $(LOCAL_OBJS) $(JSON_OBJS) fetch_opencv pp_shitu: $(LOCAL_OBJS) $(JSON_OBJS) fetch_opencv
$(CC) $(SYSROOT_LINK) $(CXXFLAGS_LINK) $(LOCAL_OBJS) $(JSON_OBJS) -o main $(CXX_LIBS) $(LDFLAGS) $(CC) $(SYSROOT_LINK) $(CXXFLAGS_LINK) $(LOCAL_OBJS) $(JSON_OBJS) -o pp_shitu $(CXX_LIBS) $(LDFLAGS)
fetch_opencv: fetch_opencv:
@ test -d ${THIRD_PARTY_DIR} || mkdir ${THIRD_PARTY_DIR} @ 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 ...@@ -87,4 +87,4 @@ $(JSON_OBJS): %.o: ${THIRD_PARTY_DIR}/jsoncpp_code/%.cpp fetch_json_code
.PHONY: clean fetch_opencv fetch_json_code .PHONY: clean fetch_opencv fetch_json_code
clean: clean:
rm -rf $(LOCAL_OBJS) $(JSON_OBJS) rm -rf $(LOCAL_OBJS) $(JSON_OBJS)
rm -f main rm -f pp_shitu
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <stdlib.h>
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
...@@ -58,7 +59,7 @@ class ObjectDetector { ...@@ -58,7 +59,7 @@ class ObjectDetector {
printf("before object detector\n"); printf("before object detector\n");
if(config["Global"]["det_model_path"].as<std::string>().empty()){ if(config["Global"]["det_model_path"].as<std::string>().empty()){
std::cout << "Please set [det_model_path] in config file" << std::endl; std::cout << "Please set [det_model_path] in config file" << std::endl;
return -1; exit(-1);
} }
LoadModel(config["Global"]["det_model_path"].as<std::string>(), cpu_threads); LoadModel(config["Global"]["det_model_path"].as<std::string>(), cpu_threads);
printf("create object detector\n"); } printf("create object detector\n"); }
...@@ -68,7 +69,6 @@ class ObjectDetector { ...@@ -68,7 +69,6 @@ class ObjectDetector {
// Run predictor // Run predictor
void Predict(const std::vector<cv::Mat>& imgs, void Predict(const std::vector<cv::Mat>& imgs,
const double threshold = 0.5,
const int warmup = 0, const int warmup = 0,
const int repeats = 1, const int repeats = 1,
std::vector<PPShiTu::ObjectResult>* result = nullptr, std::vector<PPShiTu::ObjectResult>* result = nullptr,
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <iostream> #include <iostream>
#include <math.h> #include <math.h>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
#include <vector> #include <vector>
...@@ -37,24 +38,24 @@ struct RESULT { ...@@ -37,24 +38,24 @@ struct RESULT {
class Recognition { class Recognition {
public: public:
void Recognition(const Json::Value &config_file) { explicit Recognition(const Json::Value &config_file) {
MobileConfig config; MobileConfig config;
if (config_file["Global"]["rec_model_path"].as<std::string>().empty()) { if (config_file["Global"]["rec_model_path"].as<std::string>().empty()) {
std::cout << "Please set [rec_model_path] in config file" << std::endl; std::cout << "Please set [rec_model_path] in config file" << std::endl;
return -1; exit(-1);
} }
config.set_model_from_file( config.set_model_from_file(
config_file["Global"]["rec_model_path"].as<std::string>()); config_file["Global"]["rec_model_path"].as<std::string>());
predictor = CreatePaddlePredictor<MobileConfig>(config); this->predictor = CreatePaddlePredictor<MobileConfig>(config);
if (config_file["Global"]["rec_label_path"].as<std::string>().empty()) { if (config_file["Global"]["rec_label_path"].as<std::string>().empty()) {
std::cout << "Please set [rec_label_path] in config file" << std::endl; std::cout << "Please set [rec_label_path] in config file" << std::endl;
return -1; exit(-1);
} }
LoadLabel(config_file["Global"]["rec_label_path"].as<std::string>()); LoadLabel(config_file["Global"]["rec_label_path"].as<std::string>());
SetPreProcessParam(config_file["RecPreProcess"]["transform_ops"]); SetPreProcessParam(config_file["RecPreProcess"]["transform_ops"]);
if (!config_file["Global"]["return_k"].as<int>().empty()) if (!config_file["Global"].isMember("return_k"))
topk = config_file["Global"]["return_k"].as<int>(); this->topk = config_file["Global"]["return_k"].as<int>();
} }
void LoadLabel(std::string path) { void LoadLabel(std::string path) {
...@@ -68,7 +69,7 @@ public: ...@@ -68,7 +69,7 @@ public:
if (pos != std::string::npos) { if (pos != std::string::npos) {
line = line.substr(pos); line = line.substr(pos);
} }
label_list.push_back(line); this->label_list.push_back(line);
} }
file.clear(); file.clear();
file.close(); file.close();
...@@ -78,33 +79,34 @@ public: ...@@ -78,33 +79,34 @@ public:
for (const auto &item : config_file) { for (const auto &item : config_file) {
auto op_name = item["type"].as<std::string>(); auto op_name = item["type"].as<std::string>();
if (op_name == "ResizeImage") { if (op_name == "ResizeImage") {
size = item["size"].as<int>(); this->size = item["size"].as<int>();
} else if (op_name == "NormalizeImage") { } else if (op_name == "NormalizeImage") {
mean.clear(); this->mean.clear();
std.clear(); this->std.clear();
for (auto tmp : item["mean"]) { for (auto tmp : item["mean"]) {
mean.emplace_back(tmp.as<float>()); this->mean.emplace_back(tmp.as<float>());
} }
for (auto tmp : item["std"]) { for (auto tmp : item["std"]) {
std.emplace_back(1 / tmp.as<float>()); this->std.emplace_back(1 / tmp.as<float>());
} }
scale = item["scale"].as<double>(); this->scale = item["scale"].as<double>();
} }
} }
}
std::vector<RESULT> RunRecModel(const cv::Mat &img, double &const_time); std::vector<RESULT> RunRecModel(const cv::Mat &img, double &cost_time);
std::vector<RESULT> PostProcess(const float *output_data, int output_size, std::vector<RESULT> PostProcess(const float *output_data, int output_size,
cv::Mat &output_image); cv::Mat &output_image);
cv::Mat ResizeImage(const cv::Mat &img); cv::Mat ResizeImage(const cv::Mat &img);
void NeonMeanScale(const float *din, float *dout, int size); void NeonMeanScale(const float *din, float *dout, int size);
private: private:
std::shared_ptr<PaddlePredictor> predictor; std::shared_ptr<PaddlePredictor> predictor;
std::vector<std::string> label_list; std::vector<std::string> label_list;
std::vector<float> mean = {0.485f, 0.456f, 0.406f}; std::vector<float> mean = {0.485f, 0.456f, 0.406f};
std::vector<float> std = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f}; std::vector<float> std = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f};
double scale = 0.00392157; double scale = 0.00392157;
float size = 224; float size = 224;
int topk = 5; int topk = 5;
} };
} } // namespace PPShiTu
...@@ -146,12 +146,12 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs, ...@@ -146,12 +146,12 @@ void DetPredictImage(const std::vector<cv::Mat> &batch_imgs,
void PrintResult(const std::string &image_path, void PrintResult(const std::string &image_path,
std::vector<PPShiTu::ObjectResult> &det_result, std::vector<PPShiTu::ObjectResult> &det_result,
std::vector<std::vector<PPShiTu::RESULT>> &rec_results) { std::vector<std::vector<PPShiTu::RESULT>> &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) { for (int i = 0; i < det_result.size(); ++i) {
printf("\tresult%d: bbox[%d, %d, %d, %d], score: %f, label: %s\n", 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[i].rect[0], det_result[i].rect[1], det_result[i].rect[2],
det_result[t].rect[3], rec_results[i].score, det_result[i].rect[3], rec_results[i][0].score,
rec_results[i].class_name.c_str()); rec_results[i][0].class_name.c_str());
} }
} }
...@@ -207,7 +207,7 @@ int main(int argc, char **argv) { ...@@ -207,7 +207,7 @@ int main(int argc, char **argv) {
if (!RT_Config["Global"]["infer_imgs"].as<std::string>().empty() || if (!RT_Config["Global"]["infer_imgs"].as<std::string>().empty() ||
!RT_Config["Global"]["infer_imgs_dir"].as<std::string>().empty()) { !RT_Config["Global"]["infer_imgs_dir"].as<std::string>().empty()) {
std::vector<std::string> all_img_paths; std::vector<std::string> all_img_paths;
std::vector<cv::string> cv_all_img_paths; std::vector<cv::String> cv_all_img_paths;
if (!RT_Config["Global"]["infer_imgs"].as<std::string>().empty()) { if (!RT_Config["Global"]["infer_imgs"].as<std::string>().empty()) {
all_img_paths.push_back( all_img_paths.push_back(
RT_Config["Global"]["infer_imgs"].as<std::string>()); RT_Config["Global"]["infer_imgs"].as<std::string>());
...@@ -224,7 +224,7 @@ int main(int argc, char **argv) { ...@@ -224,7 +224,7 @@ int main(int argc, char **argv) {
} }
} }
for (int i = 0; i < all_img_paths.size(); ++i) { 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); cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR);
if (!srcimg.data) { if (!srcimg.data) {
std::cerr << "[ERROR] image read failed! image path: " << img_path std::cerr << "[ERROR] image read failed! image path: " << img_path
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
// https://github.com/RangiLyu/nanodet/blob/main/demo_mnn/nanodet_mnn.cpp // https://github.com/RangiLyu/nanodet/blob/main/demo_mnn/nanodet_mnn.cpp
#include "include/picodet_postprocess.h" #include "include/picodet_postprocess.h"
#include <cmath>
namespace PPShiTu { namespace PPShiTu {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
namespace PPShiTu { namespace PPShiTu {
std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img, std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img,
double &const_time) { double &cost_time) {
// Read img // Read img
cv::Mat resize_image = ResizeImage(img); cv::Mat resize_image = ResizeImage(img);
...@@ -25,7 +25,7 @@ std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img, ...@@ -25,7 +25,7 @@ std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img,
resize_image.convertTo(img_fp, CV_32FC3, scale); resize_image.convertTo(img_fp, CV_32FC3, scale);
// Prepare input data from image // Prepare input data from image
std::unique_ptr<Tensor> input_tensor(std::move(predictor->GetInput(0))); std::unique_ptr<Tensor> input_tensor(std::move(this->predictor->GetInput(0)));
input_tensor->Resize({1, 3, img_fp.rows, img_fp.cols}); input_tensor->Resize({1, 3, img_fp.rows, img_fp.cols});
auto *data0 = input_tensor->mutable_data<float>(); auto *data0 = input_tensor->mutable_data<float>();
...@@ -34,11 +34,11 @@ std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img, ...@@ -34,11 +34,11 @@ std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img,
auto start = std::chrono::system_clock::now(); auto start = std::chrono::system_clock::now();
// Run predictor // Run predictor
predictor->Run(); this->predictor->Run();
// Get output and post process // Get output and post process
std::unique_ptr<const Tensor> output_tensor( std::unique_ptr<const Tensor> output_tensor(
std::move(predictor->GetOutput(0))); std::move(this->predictor->GetOutput(0)));
auto *output_data = output_tensor->data<float>(); auto *output_data = output_tensor->data<float>();
auto end = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now();
auto duration = auto duration =
...@@ -59,7 +59,7 @@ std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img, ...@@ -59,7 +59,7 @@ std::vector<RESULT> Recognition::RunRecModel(const cv::Mat &img,
void Recognition::NeonMeanScale(const float *din, float *dout, int size) { 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"; std::cerr << "[ERROR] mean or scale size must equal to 3\n";
exit(1); exit(1);
} }
...@@ -93,31 +93,31 @@ void Recognition::NeonMeanScale(const float *din, float *dout, int size) { ...@@ -93,31 +93,31 @@ void Recognition::NeonMeanScale(const float *din, float *dout, int size) {
dout_c2 += 4; dout_c2 += 4;
} }
for (; i < size; i++) { for (; i < size; i++) {
*(dout_c0++) = (*(din++) - mean[0]) * std[0]; *(dout_c0++) = (*(din++) - this->mean[0]) * this->std[0];
*(dout_c1++) = (*(din++) - mean[1]) * std[1]; *(dout_c1++) = (*(din++) - this->mean[1]) * this->std[1];
*(dout_c2++) = (*(din++) - mean[2]) * std[2]; *(dout_c2++) = (*(din++) - this->mean[2]) * this->std[2];
} }
} }
cv::Mat Recognition::ResizeImage(const cv::Mat &img) { cv::Mat Recognition::ResizeImage(const cv::Mat &img) {
cv::Mat resize_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; return resize_img;
} }
std::vector<RESULT> Recognition::PostProcess(const float *output_data, std::vector<RESULT> Recognition::PostProcess(const float *output_data,
int output_size, int output_size,
cv::Mat &output_image) { cv::Mat &output_image) {
int max_indices[topk]; int max_indices[this->topk];
double max_scores[topk]; double max_scores[this->topk];
for (int i = 0; i < topk; i++) { for (int i = 0; i < this->topk; i++) {
max_indices[i] = 0; max_indices[i] = 0;
max_scores[i] = 0; max_scores[i] = 0;
} }
for (int i = 0; i < output_size; i++) { for (int i = 0; i < output_size; i++) {
float score = output_data[i]; float score = output_data[i];
int index = i; int index = i;
for (int j = 0; j < topk; j++) { for (int j = 0; j < this->topk; j++) {
if (score > max_scores[j]) { if (score > max_scores[j]) {
index += max_indices[j]; index += max_indices[j];
max_indices[j] = index - max_indices[j]; max_indices[j] = index - max_indices[j];
...@@ -129,11 +129,11 @@ std::vector<RESULT> Recognition::PostProcess(const float *output_data, ...@@ -129,11 +129,11 @@ std::vector<RESULT> Recognition::PostProcess(const float *output_data,
} }
} }
std::vector<RESULT> results(topk); std::vector<RESULT> results(this->topk);
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
results[i].class_name = "Unknown"; results[i].class_name = "Unknown";
if (max_indices[i] >= 0 && max_indices[i] < label_list.size()) { if (max_indices[i] >= 0 && max_indices[i] < this->label_list.size()) {
results[i].class_name = label_list[max_indices[i]]; results[i].class_name = this->label_list[max_indices[i]];
} }
results[i].score = max_scores[i]; results[i].score = max_scores[i];
results[i].class_id = max_indices[i]; results[i].class_id = max_indices[i];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册