查看: 4318|回复: 1

[评测分享] 【EdgeBoard FZ5 边缘AI计算盒】paddle lite 的c++

[复制链接]
  • TA的每日心情
    郁闷
    2024-10-28 10:11
  • 签到天数: 1703 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2021-5-14 05:33:56 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 nemon 于 2021-5-14 05:58 编辑

    试验一下baidu的例子。
    登录盒子后,下载paddle lite 1.5.1的例子:
    1. wget https://platform.bj.bcebos.com/edgeboard/1.5.1/PaddleLiteSample.zip
    复制代码
    解压
    1. unzip PaddleLiteSample.zip
    复制代码
    进去
    1. cd PaddleLiteSample/detection
    复制代码

    如果没有build目录,创建一个
    1. mkdir build
    复制代码
    清理build目录
    1. cd build
    2. rm -rf *
    复制代码

    调用cmake 创建 Makefile
    1. cmake ..
    复制代码

    控制台输出一大堆
    1. -- The C compiler identification is GNU 8.2.0
    2. -- The CXX compiler identification is GNU 8.2.0
    3. -- Check for working C compiler: /usr/bin/cc
    4. -- Check for working C compiler: /usr/bin/cc -- works
    5. -- Detecting C compiler ABI info
    6. -- Detecting C compiler ABI info - done
    7. -- Detecting C compile features
    8. -- Detecting C compile features - done
    9. -- Check for working CXX compiler: /usr/bin/c++
    10. -- Check for working CXX compiler: /usr/bin/c++ -- works
    11. -- Detecting CXX compiler ABI info
    12. -- Detecting CXX compiler ABI info - done
    13. -- Detecting CXX compile features
    14. -- Detecting CXX compile features - done
    15. -- OpenCV found (/usr/local/share/OpenCV),opencv_core;opencv_videoio;opencv_highgui;opencv_imgproc;opencv_imgcodecs;opencv_ml;opencv_video
    16. PADDLELITE_FOUND
    17. -- Configuring done
    18. -- Generating done
    19. -- Build files have been written to: /home/root/workspace/PaddleLiteSample/detection/build
    复制代码

    编译工程
    1. make
    复制代码
    控制台又输出一堆
    1. Scanning dependencies of target image_detection
    2. [ 16%] Building CXX object CMakeFiles/image_detection.dir/src/camera.cpp.o
    3. [ 33%] Building CXX object CMakeFiles/image_detection.dir/src/image_detection.cpp.o
    4. [ 50%] Linking CXX executable image_detection
    5. [ 50%] Built target image_detection
    6. Scanning dependencies of target video_detection
    7. [ 66%] Building CXX object CMakeFiles/video_detection.dir/src/camera.cpp.o
    8. [ 83%] Building CXX object CMakeFiles/video_detection.dir/src/video_detection.cpp.o
    9. [100%] Linking CXX executable video_detection
    10. [100%] Built target video_detection
    复制代码
    调用一个执行试试看
    1. ./image_detection ../configs/yolov3/screw.json
    复制代码
    控制台输出结果

    1. driver_version: 1.5.1
    2. paddle_lite_version: 1.5.1
    3. label:0,score:0.980071 loc:823,352,224,159
    4. label:0,score:0.978424 loc:1076,516,206,177
    5. label:0,score:0.975051 loc:932,135,217,136
    6. label:0,score:0.973695 loc:628,680,196,165
    7. label:0,score:0.880789 loc:996,568,123,188
    8. label:0,score:0.845378 loc:817,651,118,187
    9. label:1,score:0.95993 loc:651,225,139,128
    10. label:1,score:0.958707 loc:661,519,146,147
    11. label:1,score:0.958172 loc:657,363,150,144
    12. label:1,score:0.957937 loc:828,512,142,135
    13. label:1,score:0.957928 loc:780,146,138,130
    14. label:1,score:0.957118 loc:1106,260,153,146
    15. label:1,score:0.955453 loc:1142,611,152,160
    16. label:1,score:0.953481 loc:1082,419,145,133
    17. label:1,score:0.94732 loc:967,275,148,136
    18. label:1,score:0.932704 loc:918,699,141,129
    复制代码
    看看输出的图片
    result.jpg
    最后看一下src里面的主程序:
    1. #include <iostream>
    2. #include <fstream>
    3. #include <vector>
    4. #include <thread>
    5. #include <unistd.h>
    6. #include <mutex>
    7. #include <csignal>
    8. #include <stdio.h>

    9. #include <opencv2/opencv.hpp>
    10. #include "opencv2/core/core.hpp"
    11. #include "opencv2/highgui/highgui.hpp"

    12. #include "paddle_api.h"
    13. #include "json.hpp"

    14. using namespace std;
    15. using namespace cv;
    16. using namespace paddle::lite_api;
    17. using json = nlohmann::json;

    18. std::shared_ptr<paddle::lite_api::PaddlePredictor> g_predictor;
    19. static float THRESHOLD = 0.3;

    20. void init(json& j) {
    21.     std::string model_dir = j["model"];
    22.     std::vector<Place> valid_places({
    23.         Place{TARGET(kFPGA), PRECISION(kFP16), DATALAYOUT(kNHWC)},
    24.         Place{TARGET(kHost), PRECISION(kFloat)},
    25.         Place{TARGET(kARM), PRECISION(kFloat)},
    26.     });

    27.     paddle::lite_api::CxxConfig config;
    28.     bool combined = true;

    29.     if (combined) {
    30.         config.set_model_file(model_dir + "/model");
    31.         config.set_param_file(model_dir + "/params");
    32.     } else {
    33.         config.set_model_dir(model_dir);
    34.     }

    35.     config.set_valid_places(valid_places);
    36.     auto predictor = paddle::lite_api::CreatePaddlePredictor(config);
    37.     g_predictor = predictor;

    38.     THRESHOLD = j["threshold"];
    39. }

    40. Mat read_image(json& value, float* data) {

    41.     auto image = value["image"];
    42.     Mat img = imread(image);
    43.     std::string format = value["format"];
    44.     std::transform(format.begin(), format.end(),format.begin(), ::toupper);

    45.     int width = value["input_width"];
    46.     int height = value["input_height"];
    47.     std::vector<float> mean = value["mean"];
    48.     std::vector<float> scale = value["scale"];

    49.     Mat img2;
    50.     resize(img, img2, Size(width, height));
    51.    
    52.     Mat sample_float;
    53.     img2.convertTo(sample_float, CV_32FC3);

    54.     int index = 0;
    55.     for (int row = 0; row < sample_float.rows; ++row) {
    56.         float* ptr = (float*)sample_float.ptr(row);
    57.         for (int col = 0; col < sample_float.cols; col++) {
    58.             float* uc_pixel = ptr;
    59.             float b = uc_pixel[0];
    60.             float g = uc_pixel[1];
    61.             float r = uc_pixel[2];

    62.             if (format == "RGB") {
    63.                 data[index] = (r - mean[0]) * scale[0];
    64.                 data[index + 1] = (g - mean[1]) * scale[1];
    65.                 data[index + 2] = (b - mean[2]) * scale[2];
    66.             } else {
    67.                 data[index] = (b - mean[0]) * scale[0];
    68.                 data[index + 1] = (g - mean[1]) * scale[1];
    69.                 data[index + 2] = (r - mean[2]) * scale[2];
    70.             }
    71.             ptr += 3;
    72.             index += 3;
    73.         }
    74.     }
    75.     return img;
    76. }

    77. void drawRect(const Mat &mat, float *data, int len, bool yolo) {
    78.   for (int i = 0; i < len; i++) {
    79.     float index = data[0];
    80.     float score = data[1];
    81.     if (score > THRESHOLD) {
    82.         int x1 = 0;
    83.         int y1 = 0;
    84.         int x2 = 0;
    85.         int y2 = 0;
    86.         if (yolo) {
    87.             x1 = static_cast<int>(data[2]);
    88.             y1 = static_cast<int>(data[3]);
    89.             x2 = static_cast<int>(data[4]);
    90.             y2 = static_cast<int>(data[5]);
    91.         } else {
    92.             x1 = static_cast<int>(data[2] * mat.cols);
    93.             y1 = static_cast<int>(data[3] * mat.rows);
    94.             x2 = static_cast<int>(data[4] * mat.cols);
    95.             y2 = static_cast<int>(data[5] * mat.rows);
    96.         }
    97.         int width = x2 - x1;
    98.         int height = y2 - y1;

    99.         cv::Point pt1(x1, y1);
    100.         cv::Point pt2(x2, y2);
    101.         cv::rectangle(mat, pt1, pt2, cv::Scalar(102, 0, 255), 3);
    102.         std::cout << "label:" << index << ",score:" << score << " loc:";
    103.         std::cout << x1 << "," << y1 << "," << width << "," << height
    104.                 << std::endl;
    105.     }
    106.     data += 6;
    107.   }
    108.   imwrite("result.jpg", mat);
    109. }

    110. void predict(json& value) {
    111.     int width = value["input_width"];
    112.     int height = value["input_height"];

    113.     auto input = g_predictor->GetInput(0);
    114.     input->Resize({1, 3, height, width});
    115.     auto* in_data = input->mutable_data<float>();

    116.     Mat img = read_image(value, in_data);
    117.     bool is_yolo = false;
    118.     auto network_type = value["network_type"];
    119.     if (network_type != nullptr && network_type == "YOLOV3") {
    120.         is_yolo = true;
    121.         auto img_shape = g_predictor->GetInput(1);
    122.         img_shape->Resize({1, 2});
    123.         auto* img_shape_data = img_shape->mutable_data<int32_t>();
    124.         img_shape_data[0] = img.rows;
    125.         img_shape_data[1] = img.cols;
    126.     }
    127.     g_predictor->Run();

    128.     auto output = g_predictor->GetOutput(0);
    129.     float *data = output->mutable_data<float>();
    130.     int size = output->shape()[0];

    131.     auto image = value["image"];
    132.     drawRect(img, data, size, is_yolo);
    133. }

    134. int main(int argc, char* argv[]){
    135.     std::string path;
    136.     if (argc > 1) {
    137.         path = argv[1];
    138.     } else {
    139.         path = "../configs/config.json";
    140.     }
    141.    
    142.     json j;
    143.     std::ifstream is(path);
    144.     is >> j;
    145.     init(j);
    146.     predict(j);
    147.     return 0;
    148. }
    149. <font size="2">
    150. </font>
    复制代码
    代码中,43-45行创建预测器,第128行函数predict进行预测,drawRect函数负责打印结果输出图片。




    回复

    使用道具 举报

    该用户从未签到

    发表于 2022-1-10 18:07:04 | 显示全部楼层
    船长,你那个AVR XMEGA-A3BU Xplained开发板还在吗,能否福利在下啊
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-11-26 08:53 , Processed in 0.139879 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.