TA的每日心情 | 郁闷 2024-10-28 10:11 |
---|
签到天数: 1703 天 连续签到: 1 天 [LV.Master]伴坛终老
|
本帖最后由 nemon 于 2021-5-14 05:58 编辑
试验一下baidu的例子。
登录盒子后,下载paddle lite 1.5.1的例子:
- wget https://platform.bj.bcebos.com/edgeboard/1.5.1/PaddleLiteSample.zip
复制代码 解压
- unzip PaddleLiteSample.zip
复制代码 进去
- cd PaddleLiteSample/detection
复制代码
如果没有build目录,创建一个
清理build目录
调用cmake 创建 Makefile
控制台输出一大堆
- -- The C compiler identification is GNU 8.2.0
- -- The CXX compiler identification is GNU 8.2.0
- -- Check for working C compiler: /usr/bin/cc
- -- Check for working C compiler: /usr/bin/cc -- works
- -- Detecting C compiler ABI info
- -- Detecting C compiler ABI info - done
- -- Detecting C compile features
- -- Detecting C compile features - done
- -- Check for working CXX compiler: /usr/bin/c++
- -- Check for working CXX compiler: /usr/bin/c++ -- works
- -- Detecting CXX compiler ABI info
- -- Detecting CXX compiler ABI info - done
- -- Detecting CXX compile features
- -- Detecting CXX compile features - done
- -- OpenCV found (/usr/local/share/OpenCV),opencv_core;opencv_videoio;opencv_highgui;opencv_imgproc;opencv_imgcodecs;opencv_ml;opencv_video
- PADDLELITE_FOUND
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /home/root/workspace/PaddleLiteSample/detection/build
复制代码
编译工程
控制台又输出一堆
- Scanning dependencies of target image_detection
- [ 16%] Building CXX object CMakeFiles/image_detection.dir/src/camera.cpp.o
- [ 33%] Building CXX object CMakeFiles/image_detection.dir/src/image_detection.cpp.o
- [ 50%] Linking CXX executable image_detection
- [ 50%] Built target image_detection
- Scanning dependencies of target video_detection
- [ 66%] Building CXX object CMakeFiles/video_detection.dir/src/camera.cpp.o
- [ 83%] Building CXX object CMakeFiles/video_detection.dir/src/video_detection.cpp.o
- [100%] Linking CXX executable video_detection
- [100%] Built target video_detection
复制代码 调用一个执行试试看- ./image_detection ../configs/yolov3/screw.json
复制代码 控制台输出结果
- driver_version: 1.5.1
- paddle_lite_version: 1.5.1
- label:0,score:0.980071 loc:823,352,224,159
- label:0,score:0.978424 loc:1076,516,206,177
- label:0,score:0.975051 loc:932,135,217,136
- label:0,score:0.973695 loc:628,680,196,165
- label:0,score:0.880789 loc:996,568,123,188
- label:0,score:0.845378 loc:817,651,118,187
- label:1,score:0.95993 loc:651,225,139,128
- label:1,score:0.958707 loc:661,519,146,147
- label:1,score:0.958172 loc:657,363,150,144
- label:1,score:0.957937 loc:828,512,142,135
- label:1,score:0.957928 loc:780,146,138,130
- label:1,score:0.957118 loc:1106,260,153,146
- label:1,score:0.955453 loc:1142,611,152,160
- label:1,score:0.953481 loc:1082,419,145,133
- label:1,score:0.94732 loc:967,275,148,136
- label:1,score:0.932704 loc:918,699,141,129
复制代码 看看输出的图片
最后看一下src里面的主程序:- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <thread>
- #include <unistd.h>
- #include <mutex>
- #include <csignal>
- #include <stdio.h>
- #include <opencv2/opencv.hpp>
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "paddle_api.h"
- #include "json.hpp"
- using namespace std;
- using namespace cv;
- using namespace paddle::lite_api;
- using json = nlohmann::json;
- std::shared_ptr<paddle::lite_api::PaddlePredictor> g_predictor;
- static float THRESHOLD = 0.3;
- void init(json& j) {
- std::string model_dir = j["model"];
- std::vector<Place> valid_places({
- Place{TARGET(kFPGA), PRECISION(kFP16), DATALAYOUT(kNHWC)},
- Place{TARGET(kHost), PRECISION(kFloat)},
- Place{TARGET(kARM), PRECISION(kFloat)},
- });
- paddle::lite_api::CxxConfig config;
- bool combined = true;
- if (combined) {
- config.set_model_file(model_dir + "/model");
- config.set_param_file(model_dir + "/params");
- } else {
- config.set_model_dir(model_dir);
- }
- config.set_valid_places(valid_places);
- auto predictor = paddle::lite_api::CreatePaddlePredictor(config);
- g_predictor = predictor;
- THRESHOLD = j["threshold"];
- }
- Mat read_image(json& value, float* data) {
- auto image = value["image"];
- Mat img = imread(image);
- std::string format = value["format"];
- std::transform(format.begin(), format.end(),format.begin(), ::toupper);
- int width = value["input_width"];
- int height = value["input_height"];
- std::vector<float> mean = value["mean"];
- std::vector<float> scale = value["scale"];
- Mat img2;
- resize(img, img2, Size(width, height));
-
- Mat sample_float;
- img2.convertTo(sample_float, CV_32FC3);
- int index = 0;
- for (int row = 0; row < sample_float.rows; ++row) {
- float* ptr = (float*)sample_float.ptr(row);
- for (int col = 0; col < sample_float.cols; col++) {
- float* uc_pixel = ptr;
- float b = uc_pixel[0];
- float g = uc_pixel[1];
- float r = uc_pixel[2];
- if (format == "RGB") {
- data[index] = (r - mean[0]) * scale[0];
- data[index + 1] = (g - mean[1]) * scale[1];
- data[index + 2] = (b - mean[2]) * scale[2];
- } else {
- data[index] = (b - mean[0]) * scale[0];
- data[index + 1] = (g - mean[1]) * scale[1];
- data[index + 2] = (r - mean[2]) * scale[2];
- }
- ptr += 3;
- index += 3;
- }
- }
- return img;
- }
- void drawRect(const Mat &mat, float *data, int len, bool yolo) {
- for (int i = 0; i < len; i++) {
- float index = data[0];
- float score = data[1];
- if (score > THRESHOLD) {
- int x1 = 0;
- int y1 = 0;
- int x2 = 0;
- int y2 = 0;
- if (yolo) {
- x1 = static_cast<int>(data[2]);
- y1 = static_cast<int>(data[3]);
- x2 = static_cast<int>(data[4]);
- y2 = static_cast<int>(data[5]);
- } else {
- x1 = static_cast<int>(data[2] * mat.cols);
- y1 = static_cast<int>(data[3] * mat.rows);
- x2 = static_cast<int>(data[4] * mat.cols);
- y2 = static_cast<int>(data[5] * mat.rows);
- }
- int width = x2 - x1;
- int height = y2 - y1;
- cv::Point pt1(x1, y1);
- cv::Point pt2(x2, y2);
- cv::rectangle(mat, pt1, pt2, cv::Scalar(102, 0, 255), 3);
- std::cout << "label:" << index << ",score:" << score << " loc:";
- std::cout << x1 << "," << y1 << "," << width << "," << height
- << std::endl;
- }
- data += 6;
- }
- imwrite("result.jpg", mat);
- }
- void predict(json& value) {
- int width = value["input_width"];
- int height = value["input_height"];
- auto input = g_predictor->GetInput(0);
- input->Resize({1, 3, height, width});
- auto* in_data = input->mutable_data<float>();
- Mat img = read_image(value, in_data);
- bool is_yolo = false;
- auto network_type = value["network_type"];
- if (network_type != nullptr && network_type == "YOLOV3") {
- is_yolo = true;
- auto img_shape = g_predictor->GetInput(1);
- img_shape->Resize({1, 2});
- auto* img_shape_data = img_shape->mutable_data<int32_t>();
- img_shape_data[0] = img.rows;
- img_shape_data[1] = img.cols;
- }
- g_predictor->Run();
- auto output = g_predictor->GetOutput(0);
- float *data = output->mutable_data<float>();
- int size = output->shape()[0];
- auto image = value["image"];
- drawRect(img, data, size, is_yolo);
- }
- int main(int argc, char* argv[]){
- std::string path;
- if (argc > 1) {
- path = argv[1];
- } else {
- path = "../configs/config.json";
- }
-
- json j;
- std::ifstream is(path);
- is >> j;
- init(j);
- predict(j);
- return 0;
- }
- <font size="2">
- </font>
复制代码 代码中,43-45行创建预测器,第128行函数predict进行预测,drawRect函数负责打印结果输出图片。
|
|