test.cpp 9.42 KB
#include <iostream>
#include <sstream>
#include <fstream>
#include "vpd.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"

#include <chrono>
#include <dirent.h>

using namespace std;
using namespace cv;

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);
}


void CvBGR2NV21(Mat& bgr, unsigned char* yuv) {
    // int stride = (bgr.cols + 127) / 128 * 128;
    int stride = (bgr.cols + 63) / 64 * 64;
    int strideH = (bgr.rows + 15) / 16 * 16;
    for (int i = 0; i < bgr.rows; i++) {
        for (int j = 0; j < bgr.cols; j++) {
            int B = bgr.at<cv::Vec3b>(i, j)[0];
            int G = bgr.at<cv::Vec3b>(i, j)[1];
            int R = bgr.at<cv::Vec3b>(i, j)[2];

            int Y = (77 * R + 150 * G + 29 * B) >> 8;
            yuv[i * stride + j] = (Y < 0) ? 0 : ((Y > 255) ? 255 : Y);
            if (i % 2 == 0 && j % 2 == 0) {
                int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128;
                int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128;
                yuv[strideH * stride + i / 2 * stride + j] = (V < 0) ? 0 : ((V > 255) ? 255 : V);
                yuv[strideH * stride + i / 2 * stride + j + 1] = (U < 0) ? 0 : ((U > 255) ? 255 : U);
            }
        }
    }
}

int main() {
    cout << vpd_get_version() << endl;
	 const char*  img_file_path = "../../../data/vpd/";
	string saveimagepath= "../../../data/result/";
   
    vehicle_pendant_det_param param;
    param.dmodelNames = "../vpd/models/vpd0707_310p.om";
    param.dthresld = 0.3;
    param.dlogo_thresld = 0.5;
    param.devId = 0;

    void* handle = nullptr;
    cout << "init start " << endl;
    ACL_CALL(aclInit(nullptr), ACL_ERROR_NONE, SY_FAILED);
    ACL_CALL(aclrtSetDevice(param.devId), ACL_ERROR_NONE, SY_FAILED);
    aclrtContext ctx;
    ACL_CALL(aclrtCreateContext(&ctx, param.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 = vpd_init(&handle, param);
    if (ret == 0) {
        cout << "init success " << endl;
        
        //ifstream infile("list.txt");
        string file;
        if(1) {
			
        std::vector<std::string> fileList;

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

        double t3 = 0;
        for (auto & file : fileList) {
            string filename = img_file_path + file;
			
            cout << "img path: " << filename << endl;

            Mat cvImg = imread(filename.c_str());
            const int batchsize = 2;
            sy_img imgs[batchsize];
            //debug====================================================================
            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();
            }
            //debug end================================================================

            // uint32_t alignWidth = (cvImg.cols + 63) / 64 * 64; //310p宽64对齐
            // uint32_t alignHeight = (cvImg.rows + 15) / 16 * 16;
            // uint32_t size = alignWidth * alignHeight * 1.5;
            // std::shared_ptr<uint8_t> data = shared_ptr<uint8_t>((new uint8_t[size]), 
            //     [](uint8_t* p) {delete [] p;});
            // CvBGR2NV21(cvImg, data.get());
         
            // for (int i = 0; i < batchsize; i++) {
            //     imgs[i].w_ = cvImg.cols;
            //     imgs[i].h_ = cvImg.rows;
            //     imgs[i].data_ = data.get();
            // }
           
           
            vpd_result * results = new vpd_result[batchsize];
            for (int b = 0; b < batchsize; b++) {
                results[b].vpd_res = new vpd_info[OBJ_MAX_COUNT];
            }
            
            double t1,t2;
            t1 = msecond();
            int ret = vpd_process(handle, imgs, batchsize, results);
            t2 = msecond();
            printf("debug mean process time: %.2f\n", (t2 - t1)/batchsize);
            if (SY_SUCCESS != ret) {
                printf("vpd detection process failed!");
                return SY_FAILED;
            }
            

            //debug==========================================================
            // vpd_result * results_b10 = new vpd_result[batchsize];
            // for (int b = 0; b < batchsize; b++) {
            //     results_b10[b].vpd_res = new vpd_info[OBJ_MAX_COUNT];
            // }
            // t1 = msecond();
            // ret = vpd_process10(handle, imgs, batchsize, results_b10);
            // t2 = msecond();
            // printf("debug mean batch process time: %.2f\n", (t2 - t1)/batchsize);
            //debug end======================================================

            //draw results
            for(int b = 0; b < batchsize; b ++){    
                //vpd
                printf("debug vpd det num:%d\n",results[b].count);
                for (int i = 0; i < results[b].count; i++) {
                    if(results[b].vpd_res[i].confidence < 0.0) continue;
                    int index = results[b].vpd_res[i].index;
                    Point lt(results[b].vpd_res[i].rect.left_, results[b].vpd_res[i].rect.top_);
                    Point rb((results[b].vpd_res[i].rect.left_ + results[b].vpd_res[i].rect.width_), 
                    (results[b].vpd_res[i].rect.top_ + results[b].vpd_res[i].rect.height_));
                    rectangle(cvImg, lt, rb, cv::Scalar(index, index, 255), 2);

                    int fontface = cv::FONT_HERSHEY_SIMPLEX;
                    double scale = 0.5;
                    int thickness = 1;
                    int baseline = 0;
                    char buffer[50];

                    snprintf(buffer, sizeof(buffer), "%d:%.2f", index, results[b].vpd_res[i].confidence);
                    cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness, &baseline);
                    cv::putText(cvImg, buffer, lt - cv::Point(0, baseline), fontface, scale, cv::Scalar(255, 255, 255), thickness, 4);

                }

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

            // for(int b = 0; b < batchsize; b ++){    
            //     //vpd
            //     printf("debug vpd_b10 det num:%d\n",results_b10[b].count);
            //     // for (int i = 0; i < results_b10[b].count; i++) {
            //     //     if(results_b10[b].vpd_res[i].confidence < 0.0) continue;
            //     //     int index = results_b10[b].vpd_res[i].index;
            //     //     Point lt(results_b10[b].vpd_res[i].rect.left_, results_b10[b].vpd_res[i].rect.top_);
            //     //     Point rb((results_b10[b].vpd_res[i].rect.left_ + results_b10[b].vpd_res[i].rect.width_), 
            //     //     (results_b10[b].vpd_res[i].rect.top_ + results_b10[b].vpd_res[i].rect.height_));
            //     //     rectangle(cvImg, lt, rb, cv::Scalar(index, index, 255), 2);

            //     //     int fontface = cv::FONT_HERSHEY_SIMPLEX;
            //     //     double scale = 0.5;
            //     //     int thickness = 1;
            //     //     int baseline = 0;
            //     //     char buffer[50];

            //     //     snprintf(buffer, sizeof(buffer), "%d:%.2f", index, results_b10[b].vpd_res[i].confidence);
            //     //     cv::Size text = cv::getTextSize(buffer, fontface, scale, thickness, &baseline);
            //     //     cv::putText(cvImg, buffer, lt - cv::Point(0, baseline), fontface, scale, cv::Scalar(255, 255, 255), thickness, 4);

            //     // }

            //     // string jpgSaveName = "result/" + file;
            //     // cv::imwrite(jpgSaveName, cvImg);
            // }

            for (int i = 0; i < batchsize; i++) {
                delete [] results[i].vpd_res;
                // delete [] results_b10[i].vpd_res;
            }
            delete [] results;
            // delete [] results_b10;

        } }


    }

    vpd_release(&handle);
    aclrtDestroyStream(stream);
    aclrtDestroyContext(ctx);
    aclrtResetDevice(param.devId);
    aclFinalize();

    return 0;
}