#include #include #include #include "sy_common.h" #include "sy_errorinfo.h" #include "vehicle_analysis.h" #include "vehicle_result.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 "utils.h" #include "dvpp_process.h" using namespace std; using namespace cv; #include #include //static char colorLabel[13][8] = { "棕", "橙", "灰", "白", "粉", "紫", "红", "绿", "蓝", "银", "青", "黄", "黑" }; static std::vector CARCOLOR1 = { "BROWN","ORANGE","GRAY","WHITE","PINK","PURPLE","RED","GREEN","BLUE", "SILVER","CYAN","YELLOW","BLACK" }; static char vsLabel[11][40] = { "diao", "guan", "huo", "zha", "jiao","jiu", "la", "xiao", "qing", "sui","wei" }; 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& 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 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(i, j)[0]; int G = bgr.at(i, j)[1]; int R = bgr.at(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() { //ofstream oFile; //打开要输出的文件 //oFile.open("va_result.csv", ios::out | ios::trunc); //oFile << "image" << "," << "image_quality_score" << "," << "count" << "," << "vehicle_info" << endl; cout << va_get_version() << endl; //string imagepath= "./vpt/"; //string saveimagepath= "./result/"; //int gpuid = 0; //string imagepath= "/home/HwHiAiUser/WH/vehicle_analysis202105/data/vpt/"; //string imagepath= "/home/HwHiAiUser/WH/vehicle_analysis202105/data/bug/"; string img_file_path= "/data/wanghong/Atlas310pro/vehicle_analysis202303/data/bug/"; //string imagepath= "/home/HwHiAiUser/WH/vehicle_analysis202105/data/img/"; //string imagepath= "/home/HwHiAiUser/WH/vehicle_analysis202105/data/carset/"; string saveimagepath= "/data/wanghong/Atlas310pro/vehicle_analysis202303/data/result/"; int gpuid = 0; va_param param; param.vehicle_detect_config= SY_CONFIG_OPEN; //1.开启车检测 SY_CONFIG_CLOSE SY_CONFIG_OPEN param.vehicle_color_config= SY_CONFIG_OPEN; //3.开启车颜色识别 param.vehicle_recg_config= SY_CONFIG_OPEN; //4.开启车型识别 param.vehicle_recg_supplement_config= SY_CONFIG_OPEN; //4.开启车型识别补充识别 param.vehicle_plate_det_recg_config= SY_CONFIG_OPEN; //5.开启车牌检测识别 param.vehicle_pendant_det_config= SY_CONFIG_OPEN; //6.开启车属性检测识别 param.vehicle_illegal_config= SY_CONFIG_OPEN; //7.开启车违规 param.vehicle_feature_config= SY_CONFIG_OPEN; //8.开启车辆特征提取 param.vehicle_motor_tricycle_analysis_config= SY_CONFIG_OPEN; //8.摩托车分析 param.vehicle_manned_config= SY_CONFIG_OPEN; //8.开启载人分析 param.gpuid=gpuid; //车检测参数 //param.vehicle_det_param.process_min_l = 720;// 720; //param.vehicle_det_param.process_max_l = 1280;//1280; //param.vehicle_det_param.thresld=0.3; //param.min_obj_size=200; //最小目标 param.vehicle_det_thresld=0.4; //车牌检测参数 //param.vehicle_plate_det_param.process_min_l=320; //param.vehicle_plate_det_param.process_max_l=320; //param.vehicle_plate_det_param.thresld=0.4; param.vehicle_plate_det_thresld=0.5; //车属性检测参数 //param.vehicle_attribute_det_param.process_min_l=360; //param.vehicle_attribute_det_param.process_max_l=640; //param.vehicle_attribute_det_param.thresld=0.3; param.vehicle_attribute_det_thresld=0.5; //车logo检测参数 //param.vehicle_logo_det_param.process_min_l=512; //param.vehicle_logo_det_param.process_max_l=512; //param.vehicle_logo_det_param.thresld=0.1; param.vehicle_logo_det_thresld=0.1; //车颜色阈值 param.vc_thresld = 0.5; //车型参数 param.dbPath="/data/wanghong/Atlas310pro/vehicle_analysis202303/vehicle_analysis/db/vr_h0725x210605_r191230.db"; //param.dbPath="../db/vr_h0725x210605_r191230.db"; param.models_Path="/data/wanghong/Atlas310pro/vehicle_analysis202303/vehicle_analysis/all_models/"; //所有模型的地址 //param.models_Path="../modelsPath/"; //所有模型的地址 int ret0= va_acl_init(); void* handle = nullptr; cout << "init start " << endl; int ret = va_init(&handle, param); if (ret == 0) { cout << "init success " << endl; } else return ret; // void* handle111 = nullptr; // cout << "init2 start " << endl; // int ret2 = va_init(&handle111, param); // if (ret2 == 0) { // cout << "init2 success " << endl; // } // else // { // cout << "init2 failed " << endl; // return ret2; // } double t3=0; int count =0; //temp aclrtContext ctx; ACL_CALL(aclrtCreateContext(&ctx, gpuid), ACL_ERROR_NONE, SY_FAILED); ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, SY_FAILED); aclrtStream stream = nullptr; ACL_CALL(aclrtCreateStream(&stream), ACL_ERROR_NONE, SY_FAILED); DvppProcess* dvpp = new DvppProcess(); dvpp->InitResource(stream); for(int iter =0;iter<100;iter++) { //ifstream infile("list.txt"); //ifstream infile("list_bug.txt"); //ifstream infile("list_700W.txt"); //ifstream infile("list_img.txt"); //string file; std::vector 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 batch_size = 1; printf("batch_size=%d \n",batch_size); Mat cvImg = imread(filename.c_str()); sy_img imgs[batch_size]; ACL_CALL(aclrtSetCurrentContext(ctx), ACL_ERROR_NONE, SY_FAILED); ImageData src[batch_size], dvpp_data[batch_size]; if(1) { for (int b = 0; b < batch_size; 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;//dvpp_data[b].alignWidth; imgs[b].h_ = dvpp_data[b].height;//dvpp_data[b].alignHeight; imgs[b].data_ = dvpp_data[b].data.get();//(uint8_t*)Utils::CopyDataDeviceToLocal(dvpp_data[b].data.get(), dvpp_data[b].size); } } ImageData img_data_dvpp[batch_size]; sy_img imgs_device[batch_size]; if(0) { uint32_t alignWidth = (cvImg.cols + 127) / 128 * 128; uint32_t alignHeight = (cvImg.rows + 15) / 16 * 16; uint32_t size = alignWidth * alignHeight * 1.5; std::shared_ptr data = shared_ptr((new uint8_t[size]), [](uint8_t* p) {delete [] p;}); CvBGR2NV21(cvImg, data.get()); for (int i = 0; i < batch_size; i++) { imgs[i].w_ = cvImg.cols; imgs[i].h_ = cvImg.rows; imgs[i].data_ = data.get(); //转到device输入 Utils::CopysyImageDataToDvpp(img_data_dvpp[i], imgs[i]); imgs_device[i].w_ = img_data_dvpp[i].width;//dvpp_data[b].alignWidth; imgs_device[i].h_ = img_data_dvpp[i].height;//dvpp_data[b].alignHeight; imgs_device[i].data_ = img_data_dvpp[i].data.get(); } } if(0) { uint32_t alignWidth = (cvImg.cols + 127) / 128 * 128; uint32_t alignHeight = (cvImg.rows + 15) / 16 * 16; uint32_t size = alignWidth * alignHeight * 1.5; std::shared_ptr data = shared_ptr((new uint8_t[size]), [](uint8_t* p) {delete [] p;}); CvBGR2NV21(cvImg, data.get()); for (int i = 0; i < batch_size; i++) { imgs[i].w_ = cvImg.cols; imgs[i].h_ = cvImg.rows; imgs[i].data_ = data.get(); } } count++; va_result *result=new va_result[batch_size]; for(int b=0;b0) { float vehicle_body_detect_res_score = result[b].info[c].vehicle_body_detect_res.score; int x1=result[b].info[c].vehicle_body_detect_res.rect.left_; int y1=result[b].info[c].vehicle_body_detect_res.rect.top_; int x2=result[b].info[c].vehicle_body_detect_res.rect.left_+result[b].info[c].vehicle_body_detect_res.rect.width_; int y2=result[b].info[c].vehicle_body_detect_res.rect.top_+result[b].info[c].vehicle_body_detect_res.rect.height_; std::cout << " vehicle_body_detect_res_score:" <0) { float vehicle_detect_res_score = result[b].info[c].vehicle_detect_res.score; int x1=result[b].info[c].vehicle_detect_res.rect.left_; int y1=result[b].info[c].vehicle_detect_res.rect.top_; int x2=result[b].info[c].vehicle_detect_res.rect.left_+result[b].info[c].vehicle_detect_res.rect.width_; int y2=result[b].info[c].vehicle_detect_res.rect.top_+result[b].info[c].vehicle_detect_res.rect.height_; std::cout << " vehicle_detect_res_score:" <0) { float vehicle_win_detect_res_score = result[b].info[c].vehicle_win_detect_res.score; int x1=result[b].info[c].vehicle_win_detect_res.rect.left_; int y1=result[b].info[c].vehicle_win_detect_res.rect.top_; int x2=result[b].info[c].vehicle_win_detect_res.rect.left_+result[b].info[c].vehicle_win_detect_res.rect.width_; int y2=result[b].info[c].vehicle_win_detect_res.rect.top_+result[b].info[c].vehicle_win_detect_res.rect.height_; std::cout << " vehicle_win_detect_res_score:" <0.5) { std::cout << " car color info:"<< endl; std::cout << " index:"<< index<<" score:"<< score<< std::endl; std::string str_i = CARCOLOR1[index]; cv::putText(cvImg, str_i, cv::Point(result[b].info[c].vehicle_body_detect_res.rect.left_+5, result[b].info[c].vehicle_body_detect_res.rect.top_+50), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.3, cv::Scalar(0, 0, 255), 2); } } //4.VR车型识别 if(param.vehicle_recg_config==SY_CONFIG_OPEN && (vehicle_type==0 || vehicle_type==1 || vehicle_type==4 )) { //std::cout << "vr output----"<< std::endl; char *vehicle_brand=result[b].info[c].vehicle_recg_res.vehicle_brand;//车辆品牌 char* vehicle_subbrand=result[b].info[c].vehicle_recg_res.vehicle_subbrand; //车辆子品牌 char* vehicle_issue_year=result[b].info[c].vehicle_recg_res.vehicle_issue_year; //车辆年款 char* vehicle_type_=result[b].info[c].vehicle_recg_res.vehicle_type; //车辆类型 char* freight_ton=result[b].info[c].vehicle_recg_res.freight_ton; //货车吨级 float name_score=result[b].info[c].vehicle_recg_res.name_score; //识别置信度 float name_score_thre = 0; //if(vehicle_type==0)name_score_thre=0.7;//车头车型识别建议阈值0.7 //if(vehicle_type==1)name_score_thre=0.8;//车尾车型识别建议阈值0.8 if(name_score > name_score_thre) { cout << " vehicle_recg info: " << endl; std::cout << " name_score: " << name_score << std::endl; std::cout << " vehicle_brand: " << vehicle_brand << std::endl; std::cout << " vehicle_subbrand: " << vehicle_subbrand << std::endl; std::cout << " vehicle_issue_year: " << vehicle_issue_year << std::endl; std::cout << " vehicle_type_: " << vehicle_type_ << std::endl; std::cout << " freight_ton: " << freight_ton << std::endl; //printf("name_score:%f\n", name_score); //printf("vehicle_brand:%s\n", vehicle_brand); //printf("vehicle_subbrand:%s\n", vehicle_subbrand); //printf("vehicle_issue_year:%s\n", vehicle_issue_year); //printf("vehicle_type_:%s\n", vehicle_type_); //printf("freight_ton:%s\n", freight_ton); } } //5.VP车牌检测识别 if(param.vehicle_plate_det_recg_config==SY_CONFIG_OPEN && (vehicle_type==0 || vehicle_type==1)) { std::cout << " car plate info:"<< endl; int special_type=result[b].info[c].vehicle_plate_det_recg_res.special_type;//常规车牌、临时车牌、低速车牌。0-common,1-temporary_license_plate,2-low_speed_license_plate. std::cout << " special_type:" << special_type<< std::endl; float detect_score=result[b].info[c].vehicle_plate_det_recg_res.detect_score; //std::cout << "detect_score:" << detect_score<< std::endl; if(detect_score>0.3) { int type=result[b].info[c].vehicle_plate_det_recg_res.type; sy_rect rect=result[b].info[c].vehicle_plate_det_recg_res.rect; int x1=rect.left_; int y1=rect.top_; int x2=rect.left_+rect.width_; int y2=rect.top_+rect.height_; std::cout << " vp_type:"<0.99)//车牌识别建议置信度阈值0.99 { //printf("plate: "); for (int m = 0; m < PLATENUM; m++) { //printf("%s", result[b].info[c].vehicle_plate_det_recg_res.recg[m].character); plate_recg=plate_recg + result[b].info[c].vehicle_plate_det_recg_res.recg[m].character; } //printf("\n"); //printf("maxprob: "); //for (int m = 0; m < PLATENUM; m++) //{ // printf("%f ", result[b].info[c].vehicle_plate_det_recg_res.vehicle_plate_infos[0].recg[m].maxprob); //} //printf("\n"); //std::cout << "type:" << type<<" num_score:" <