test.cpp 3.75 KB
#include <iostream>
#include <sstream>
#include <fstream>
#include "human_parsing.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 main() {
    cout << hp_get_version() << endl;
	const char*  img_file_path = "../../../data/hp/";
	string saveimagepath= "../../../data/result/";
	
    hp_param param;
    param.modelNames = "../hp/models/hp220908_310p.om";
    param.devId = 0;

    void* handle = nullptr;
    cout << "init start " << endl;
    ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED);
    ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED);
    aclrtContext ctx;
    ACL_CALL(aclrtCreateContext(&ctx, param.devId), ACL_SUCCESS, SY_FAILED);
    aclrtStream stream = nullptr;
	ACL_CALL(aclrtCreateStream(&stream), ACL_SUCCESS, SY_FAILED);
    DvppProcess* dvpp = new DvppProcess();
	dvpp->InitResource(stream);
    int ret = hp_init(&handle, param);
    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 = 1;

            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();
            }
           
            hp_analysis_res * results = new hp_analysis_res[batchsize];
            int ret = hp_batch(handle, imgs, batchsize, results);
            if (SY_SUCCESS != ret) {
                printf("HumanCarParse process failed!");
                return SY_FAILED;
            }
            
            for(int batchIdx = 0;batchIdx<batchsize;batchIdx++) { 
                for (int i = 0; i < HP_ATTRI_INDEX_SIZE; i++) {   
                    printf("hp index:%d,confidence:%f\n",results[batchIdx].res_objs[i].res_index,results[batchIdx].res_objs[i].res_prob);
                }
            }

            if (results) {
                delete[] results;
                results = NULL;
            }

        }
    }

    hp_release(&handle);
    aclrtDestroyContext(ctx);
    aclrtResetDevice(param.devId);
    aclFinalize();

    return 0;
}