test.cpp 6.69 KB
#include <iostream>
#include <sstream>
#include <fstream>
#include <chrono>
#include <dirent.h>
#include "vehicle_plate_dr.h"
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include "opencv2/imgproc/types_c.h"
#include "time.h"
#include "sys/time.h"
#include "sy_errorinfo.h"
#include "utils.h"
#include "dvpp_process.h"

using namespace std;
using namespace cv;
#include <chrono>
#include <dirent.h>

double msecond() {
    struct timeval tv;
    gettimeofday(&tv, 0);
    return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
}


void getAllNm(std::string pthNm, std::vector<std::string>& fileList)
{
        DIR    *dir;
    struct    dirent    *ptr;
    dir = opendir(pthNm.c_str()); ///open the dir
    int filenum = 0;
    while((ptr = readdir(dir)) != NULL) ///read the list of this dir
    {
        // char* to string
        std::string curNm = ptr->d_name;
        if(curNm != "." && curNm != "..")
        {
                filenum++;
                fileList.push_back(curNm);
                //printf("file %d name: %s\n", filenum, curNm.c_str());
        }
   }
    closedir(dir);
}

int stain_vplate_process(void * handle, sy_img img_data, int& index, float& score);

int main() {
    cout << vpdr_get_version() << endl;
	 const char*  img_file_path = "../../../data/vp/";
	string saveimagepath= "../../../data/result/";
	
    vpd_param dparam;
    dparam.modelNames = "../vp/models/yolo_plate_det310p.om";
    // dparam.modelNames = "plate_det_310p.om";
    dparam.thresld = 0.2;
    dparam.devId = 0;

    vpr_param rparam;
    rparam.cls_modelNames = "../vp/models/plate3cls_310p.om";
    rparam.reg_modelNames = "../vp/models/plate_reg230316_310p.om";

    void* handle = nullptr;
    cout << "init start " << endl;
    ACL_CALL(aclInit(nullptr), ACL_ERROR_NONE, SY_FAILED);
    ACL_CALL(aclrtSetDevice(dparam.devId), ACL_ERROR_NONE, SY_FAILED);
    aclrtContext ctx;
    ACL_CALL(aclrtCreateContext(&ctx, dparam.devId), ACL_ERROR_NONE, SY_FAILED);
    aclrtStream stream = nullptr;
	ACL_CALL(aclrtCreateStream(&stream), ACL_ERROR_NONE, SY_FAILED);
	DvppProcess* dvpp = new DvppProcess();
	dvpp->InitResource(stream);
    int ret = vpdr_init(&handle, dparam, rparam);

    if (ret == 0) {
        cout << "init success " << endl;
  		std::vector<std::string> fileList;

        getAllNm(img_file_path,fileList);
        if (fileList.empty()) throw std::logic_error("No suitable images were found");

        for (auto & file : fileList) {
            string filename = img_file_path + file;
            cout << "img path: " << filename << endl;
            const int batchsize = 2;
            Mat cvImg = imread(filename.c_str());
            sy_img imgs[batchsize];
          
            ImageData src[batchsize], dvpp_data[batchsize];
            for (int b = 0; b < batchsize; b++) {
                Utils::ReadImageFile(src[b], filename); //将二进制图像读入内存,并读取宽高信息
                ACL_CALL(dvpp->CvtJpegToYuv420sp(dvpp_data[b], src[b]), SY_SUCCESS, SY_FAILED); //解码
                imgs[b].w_ = dvpp_data[b].width;
                imgs[b].h_ = dvpp_data[b].height;
                imgs[b].data_ = dvpp_data[b].data.get();
            }
        
            vplates_result * results = new vplates_result[batchsize];
            double t1, t2;
            t1 = msecond();
            int ret = vpdr_batch(handle, imgs, batchsize, results);
            t2 = msecond();
            printf("debug mean process time: %.2f\n", (t2 - t1)/batchsize);
            if (SY_SUCCESS != ret) {
                printf("vpr detection process failed!");
                return SY_FAILED;
            }

            // draw results
            for(int batchIdx = 0; batchIdx < batchsize; batchIdx ++){
                printf("debug det num:%d\n",results[batchIdx].count);
                for (int i = 0; i < results[batchIdx].count; i++) {
                    printf("plate number:");
                    for (int p = 0; p < PLATENUM; p++) {
                        printf("%s", results[batchIdx].vehicle_plate_infos[i].recg[p].character);
                    }
                    printf("\n");
                    Point lt(results[batchIdx].vehicle_plate_infos[i].rect.left_, results[batchIdx].vehicle_plate_infos[i].rect.top_);
                    Point rb((results[batchIdx].vehicle_plate_infos[i].rect.left_ + 
                    results[batchIdx].vehicle_plate_infos[i].rect.width_), (results[batchIdx].vehicle_plate_infos[i].rect.top_ + 
                    results[batchIdx].vehicle_plate_infos[i].rect.height_));
                    rectangle(cvImg, lt, rb, cv::Scalar(0, 0, 255), 6);

                    Point ldmk1(results[batchIdx].vehicle_plate_infos[i].rr_point[0].x_, results[batchIdx].vehicle_plate_infos[i].rr_point[0].y_);
                    Point ldmk2(results[batchIdx].vehicle_plate_infos[i].rr_point[1].x_, results[batchIdx].vehicle_plate_infos[i].rr_point[1].y_);
                    Point ldmk3(results[batchIdx].vehicle_plate_infos[i].rr_point[2].x_, results[batchIdx].vehicle_plate_infos[i].rr_point[2].y_);
                    Point ldmk4(results[batchIdx].vehicle_plate_infos[i].rr_point[3].x_ ,results[batchIdx].vehicle_plate_infos[i].rr_point[3].y_);
                    circle(cvImg,ldmk1,3,cv::Scalar(0, 255, 0), -1);
                    circle(cvImg,ldmk2,3,cv::Scalar(0, 255, 0), -1);
                    circle(cvImg,ldmk3,3,cv::Scalar(0, 255, 0), -1);
                    circle(cvImg,ldmk4,3,cv::Scalar(0, 255, 0), -1);


                    char buffer[50];
                    int fontface = cv::FONT_HERSHEY_SIMPLEX;
                    double scale = 1;
                    int thickness = 2;
                    int baseline = 0;
                    printf("type:%d\n", results[batchIdx].vehicle_plate_infos[i].type);
                    snprintf(buffer, sizeof(buffer), "type:%d state:%d", results[batchIdx].vehicle_plate_infos[i].type, results[batchIdx].vehicle_plate_infos[i].state);
                    cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness, &baseline);
                    cv::putText(cvImg, buffer, lt - cv::Point(0, baseline), fontface,
                            scale, cv::Scalar(0, 0, 255), thickness, 8); 
                    cout << "detect_score:" << results[batchIdx].vehicle_plate_infos[i].detect_score << " recg_score:" << results[batchIdx].vehicle_plate_infos[i].num_score << endl;

                }
            }
            string jpgSaveName = saveimagepath + file;
            cv::imwrite(jpgSaveName, cvImg);

        }
    }

    dvpp->DestroyResource();
    vpdr_release(&handle);
    aclFinalize();

    return 0;
}