通过Tengine ssd 处理usb camera 实时图像

开发环境:

Rock960开发板

Ubuntu16.04

deltavision usb camera

 

一 Tengine安装

    下载Tengine代码: https://github.com/OAID/Tengine

     安装文档:doc/install.md    

   1. 依赖库

  • caffe依赖库
sudo apt install libprotobuf-dev protobuf-compiler libboost-all-dev libgoogle-glog-dev
  • opencv
sudo apt install libopencv-dev

  2.配置文件

cd ~/tengine
 
cp makefile.config.example makefile.config

   因为直接在rock960上开发,直接可以使用默认配置

  3. 编译    

cd ~/tengine
make

 4. 验证

./build/tests/bin/bench_mobilenet -r1

二.  运行自带的ssd example

 1. 编译example

cd ~/tengine/examples
vim linux_build.sh

修改cmake,“/home/usr/tengine”是tengine 所在目录,“/usr/lib/aarch64-linux-gnu” 是protobuf库的所在目录

修改 examples/mobilenet_ssd/CMakeLists.txt, 添加一句 set( TENGINE_DIR /home/rock/Tengine)

cmake -DPROTOBUF_DIR=/usr/lib/aarch64-linux-gnu -DTENGINE_DIR=/home/usr/tengine \
      ..

  编译

mkdir build
cd build
../linux_build.sh
make -j4 

2. 运行ssd 

  把官方提供的模型,放在 ${Tengine_ROOT}/models/

  • MobileNetSSD_deploy.caffemodel
  • MobileNetSSD_deploy.prototxt

 执行

cd example/build/mobilenet_ssd

./MSSD -p ../../../models/MobileNetSSD_deploy.prototxt -m ../../../models/MobileNetSSD_deploy.caffemodel -i img.jpg

三 . 从usb camera 获取图像并处理

   读取usb camera 的方法参考:https://mp.csdn.net/postedit/85252440

   1. 复制 mobilenet_ssd目录,命名为mobilenet_ssd_camera

      修改  examples/CMakeLists.txt文件, 添加一句

add_subdirectory(mobilenet_ssd_camera)

   2. 修改 mobilenet_ssd_camera/mssd.cpp

/*
 *  V4L2 video capture example
 *
 *  This program can be used and distributed without restrictions.
 *
 *      This program is provided with the V4L2 API
 * see https://linuxtv.org/docs.php for more information
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include              /* getopt_long() */

#include               /* low-level i/o */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include "opencv2/imgproc/imgproc.hpp"
#include 

#include "tengine_c_api.h"
#include "common.hpp"


#define DEF_PROTO "models/MobileNetSSD_deploy.prototxt"
#define DEF_MODEL "models/MobileNetSSD_deploy.caffemodel"
#define DEF_IMAGE "tests/images/ssd_dog.jpg"

#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define COLS (640)
#define ROWS (480)
#define SSD_IMG_H (300)
#define SSD_IMG_W (300)
using namespace cv;

enum io_method {
        IO_METHOD_READ,
        IO_METHOD_MMAP,
        IO_METHOD_USERPTR,
};

struct buffer {
        void   *start;
        size_t  length;
};

typedef struct buffer* PBUF;

static char            *dev_name;
static enum io_method   io = IO_METHOD_MMAP;
static int              fd = -1;
struct buffer          *buffers;
static unsigned int     n_buffers;
static int              out_buf;
static int              force_format;
static int              frame_count = 70;

static std::string proto_file;
static std::string model_file;
static std::string image_file;
static std::string save_name="save.jpg";
const char *model_name = "mssd_300";
    
cv::Mat yuvImg(ROWS , COLS, CV_8UC2);
cv::Mat rgbImg(ROWS, COLS,CV_8UC3);
cv::Mat resizeImg(SSD_IMG_W, SSD_IMG_H,CV_8UC3);
cv::Mat floatImg(SSD_IMG_W, SSD_IMG_H, CV_32FC3);

static int fpsTick();

struct Box
{
    float x0;
    float y0;
    float x1;
    float y1;
    int class_idx;
    float score;
};

void get_input_data_ssd(std::string& image_file, float* input_data, int img_h,  int img_w)
{
    cv::Mat img = cv::imread(image_file);

    if (img.empty())
    {
        std::cerr << "Failed to read image file " << image_file << ".\n";
        return;
    }
   
    cv::resize(img, img, cv::Size(img_h, img_w));
    img.convertTo(img, CV_32FC3);
    float *img_data = (float *)img.data;
    int hw = img_h * img_w;

    float mean[3]={127.5,127.5,127.5};
    for (int h = 0; h < img_h; h++)
    {
        for (int w = 0; w < img_w; w++)
        {
            for (int c = 0; c < 3; c++)
            {
                input_data[c * hw + h * img_w + w] = 0.007843* (*img_data - mean[c]);
                img_data++;
            }
        }
    }
}

void post_process_ssd(std::string& image_file,float threshold,float* outdata,int num,std::string& save_name)
{
    std::cout<<"post_process_ssd\n";
    const char* class_names[] = {"background",
                            "aeroplane", "bicycle", "bird", "boat",
                            "bottle", "bus", "car", "cat", "chair",
                            "cow", "diningtable", "dog", "horse",
                            "motorbike", "person", "pottedplant",
                            "sheep", "sofa", "train", "tvmonitor"};

    //cv::Mat img = cv::imread(image_file);
    int raw_h = rgbImg.size().height;
    int raw_w = rgbImg.size().width;
    std::vector boxes;
    int line_width=raw_w*0.005;
    printf("detect ruesult num: %d \n",num);
    for (int i=0;i=threshold)
        {
            Box box;
            box.class_idx=outdata[0];
            box.score=outdata[1];
            box.x0=outdata[2]*raw_w;
            box.y0=outdata[3]*raw_h;
            box.x1=outdata[4]*raw_w;
            box.y1=outdata[5]*raw_h;
            boxes.push_back(box);
            printf("%s\t:%.0f%%\n", class_names[box.class_idx], box.score * 100);
            printf("BOX:( %g , %g ),( %g , %g )\n",box.x0,box.y0,box.x1,box.y1);
        }
        outdata+=6;
    }
    for(int i=0;i<(int)boxes.size();i++)
    {
        Box box=boxes[i];
        cv::rectangle(rgbImg, cv::Rect(box.x0, box.y0,(box.x1-box.x0),(box.y1-box.y0)),cv::Scalar(255, 255, 0),line_width);
        std::ostringstream score_str;
        score_str<

     3. 重新make

      回到 examples 目录,执行make\

     正常的话, 会生成 examples/build/mobilenet_ssd_camera 目录

   4. 执行

./MSSD -p ../../../models/MobileNetSSD_deploy.prototxt -m ../../../models/MobileNetSSD_deploy.caffemodel    

你可能感兴趣的:(深度学习)