` 今天我们测评米尔百度大脑EdgeBoard边缘AI计算盒 中的本地实例二——图片检测。和分类不同,物体检测除了能知道物体的类型,还能检测出物体所在的位置坐标。物体检测的示例,是在图片上检测物体,并绘制出坐标信息。
话不多说,直接进入正题:
我们进入文件夹PaddleLiteSampledetection中,其工程目录结构:
├── CMakeLists.txt // cmake 工程配置文件。
├── include
├── configs // 配置文件目录
│ ├── yolov3
│ └── ssd
├── lib
├── models // 模型文件目录
│ ├── yolov3
│ └── ssd
│── src
│ ├── json.hpp // json 解析库
│ └── image_detect.cpp // 图片推理示例
└── README.md
下面是配置文件示例。
.JSON
{
"model":"../models/yolov3",
"combined_model":true,
"video_device":"/dev/video0",
"input_width":608,
"input_height":608,
"image":"../models/yolov3/1.jpg",
"mean":[123.675, 116.28, 103.53],
"scale":[0.0171248, 0.017507, 0.0174292],
"format":"RGB",
"threshold":0.3,
"network_type":"YOLOV3"
}
| key | value |
| -------------- | ------------------------------------------ |
| model | 模型目录存放的位置 |
| combined_model | 是否为融合的模型,只有两个文件的是融合模型 |
| video_device | u***摄像头识别到的设备节点 |
| input_width | 输入网络的图片尺寸 输入图片会缩放到该大小 |
| input_height | 输入网络的图片尺寸 |
| image | 进行检测的图片输入 |
| mean | 平均值 |
| scale | 输入网络前预算处理为 ( x - mean ) * scale |
| format | 网络所需要的格式,OpenCV默认是BGR |
| threshold | 信心值阈值 |
| network_type | |
其它的目标检测网络也可以通过添加/修改 配置文件实现,无须修改代码。
我们检测的图片如下所示:
检测例程的使用步骤如下所示:
1.加载驱动,系统启动后加载一次即可(已启动的可忽略本)
insmod /home/root/workspace/driver/FPGAdrv.ko
2.编译示例,EdgeBoard边缘计算盒上具有编译能力,进入到PaddleLiteSample/detection示例的build目录下进行编译
cd /home/root/workspace/PaddleLiteSample/detection
// 如果没有build目录,创建一个
mkdir build
cd build
rm -rf *
// 调用cmake 创建 Makefile
cmake ..
// 编译工程。
make
编译结束后会在build 目录生成如下几个文件。
image_detection 读取本地图片推理示例。
3.执行示例
读取一张图片,做推理,将结果保存为图片输出result.jpg
./image_detection ../configs/yolov3/screw.json
最终的图片检测结果如下所示:
其配置代码如下图所示,可以通过修改配置文件对不同的图片进行检测。
后面照例是代码:
程序运行的主函数为:
- 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;
- }
复制代码
其中主要的函数为:
- 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();
- 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();
- 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();
- int size = output->shape()[0];
- auto image = value["image"];
- drawRect(img, data, size, is_yolo);
- }
复制代码
剩下的内容在解析库文件json.hpp中,大家可以自行分析。
`
|