#include #include "ErrorInfo.h" #include "VPT.h" #include "ObjCls.h" #include "utools.h" //9 #include "ga_vpt_predict_net.h" #include "ga_vpt_init_net.h" //13 //#include "ga_model.h" //#include "ga_model_init.h" int checkTime() { struct tm* info; int nYear, nMonth, nDay; time_t raw; time(&raw); info = localtime(&raw); nYear = info->tm_year + 1900; nMonth = info->tm_mon + 1; nDay = info->tm_mday; if ((nYear == 2019 && nMonth < 10)) return 1; else { printf("版本过期,请联系开发商!中科院自动化所模式识别实验室图像视频组\n"); return -1; } } int VPT_Init(void *&handle, VPT_PARAM vparam, char* resDir, VIDEO_OBJECT_SNAPSHOT_CALLBACK VideoObjSnapshotCallBack/* = NULL*/) { if (checkTime() != 1) return FAILED; int res = SUCCESS; handle = new objDetector; //申请对象 memcpy((void *)&((objDetector *)handle)->param, (void *)&vparam, sizeof(VPT_PARAM)); int flag = 0; //返回值 默认为SUCCEEDED:0 //----------------- 13类结构化 ---------------------// /* param.initnetArray = (unsigned char*)ga_model_init; param.initnetArrayLen = ga_model_init_len; param.predictnetArray = (unsigned char*)ga_model; param.predictnetArrayLen = ga_model_len; param.mode = GPU_MODE; param.gpuid = vparam.gpuid; param.flag_glog = 1; param.test_size = 360; param.test_max_size = 640; param.fpn_coarsest_stride = 32; param.submean = "3 0 0 0"; param.variance = "3 255.0 255.0 255.0"; */ //----------------- 13类结构化 ---------------------// //----------------- 猫和狗结构化 ---------------------// /* param.initnetArray = (unsigned char*)ga_model_init; param.initnetArrayLen = ga_model_init_len; param.predictnetArray = (unsigned char*)ga_model; param.predictnetArrayLen = ga_model_len; param.mode = GPU_MODE; param.gpuid = 0; param.flag_glog = 1; param.test_size = 800; param.test_max_size = 1333; param.fpn_coarsest_stride = 32; param.submean = "3 102.9801 115.9465 122.7717"; param.variance = "3 1.0 1.0 1.0"; */ //----------------- 猫和狗结构化 ---------------------// //----------------- 9类结构化(自用单路人车物) ---------------------// ctools_init_params vpt_param; vpt_param.log_level_ = 0; vpt_param.device_type_ = DEVICE_GPU; vpt_param.device_id_ = vparam.gpuid; vpt_param.engine_type_ = ENGINE_MCAFFE2; vpt_param.model_type_ = MODEL_FPN; #ifdef _MSC_VER //vpt_param.weight_file_ ="../../../model/vptModel/vpt_init_net"; //vpt_param.net_file_ = "../../../model/vptModel/vpt_predict_net"; #else vpt_param.weight_file_ = NULL; vpt_param.net_file_ = NULL; //vpt_param.weight_file_ = "vpt_model/init_net"; //vpt_param.net_file_ = "vpt_model/predict_net"; #endif /* vpt_param.weight_array_ = NULL; vpt_param.weight_array_len_ = 0; vpt_param.net_array_ = NULL; vpt_param.net_array_len_ = 0; */ vpt_param.weight_array_ = (unsigned char*)ga_vpt_init_net; vpt_param.weight_array_len_ = ga_vpt_init_net_len; vpt_param.net_array_ = (unsigned char*)ga_vpt_predict_net; vpt_param.net_array_len_ = ga_vpt_predict_net_len; vpt_param.need_im_info_ = 1; // true //"ResizePad_F32_F32,test_size,720,test_max_size,1280,fpn_coarsest_stride,32," //"NV12ToRGB_U8_U8,channel,3;" //"ResizePad_U8_F32,test_size,540,test_max_size,1280,fpn_coarsest_stride,32," //"NV12ToRGB_U8_F32,channel,3;" //"ResizePad_F32_F32,test_size,540,test_max_size,1280,fpn_coarsest_stride,32," vpt_param.data_process_str_ = "CopyData_CPU2GPU_U8;" "TypeConvert_U8_F32;" "ResizePad_F32_F32,test_size,720,test_max_size,1280,fpn_coarsest_stride,32," "submean_b,103.94,submean_g,116.78,submean_r,123.68," "variance_rev_b,0.017,variance_rev_g,0.017,variance_rev_r,0.017;" "NHWC2NCHW_F32" ; res = ctools_init(&(((objDetector *)handle)->detector), &vpt_param); /* cv::Mat img = cv::imread("test.jpg"); sy_img cur_img; cur_img.set_data(img.cols, img.rows, img.channels(), img.data); ctools_result *detresult; int count = 1000; vector< vector > detectResult; //转化为跟踪所需要的输入 while(count--) int res_status = ctools_process((((objDetector *)handle)->detector), &cur_img, 1, &detresult); cout << "finish Ctools_Detector" << endl; ctools_result &cur_result = detresult[0]; //(*result)[b].obj_count_ = cur_result.obj_count_ > MAX_DET_COUNT ? MAX_DET_COUNT : cur_result.obj_count_; for (int c = 0; c < cur_result.obj_count_ && c < 100; c++) { float score = cur_result.obj_results_[c].data_[1]; if (score >= THRESHOLD) //更改为left top right bottom score index { vector obj; obj.push_back(cur_result.obj_results_[c].data_[2]); obj.push_back(cur_result.obj_results_[c].data_[3]); obj.push_back(cur_result.obj_results_[c].data_[4]); obj.push_back(cur_result.obj_results_[c].data_[5]); obj.push_back(cur_result.obj_results_[c].data_[1]); obj.push_back(cur_result.obj_results_[c].data_[0] - 1); detectResult.push_back(obj); } } printf("============ finish det\n"); */ //----------------- 9类结构化(自用单路人车物) ---------------------// //拌线初始化 ((objDetector *)handle)->tracker = new Sort(/*lineArray, linecount, DETECTTYPE*/); ((objDetector *)handle)->snapshotHelper = new SnapShot(resDir, VideoObjSnapshotCallBack, vparam.SnaoshotParameter); ((objDetector *)handle)->frameCounter = FusionInterval; ((objDetector *)handle)->isInitFrame = true; return res; } void VPT_ResetTracker(void * handle/*, int maxResultCountVPT_Line * lineArray = NULL, int linecount = 0*/) { objDetector* tools = (objDetector*)handle; /*if (tools->ssdResult) { delete[] tools->ssdResult; } tools->param.maxResultCount = maxResultCount; tools->ssdResult = new CDTOOLS_Result[maxResultCount];*/ tools->tracker->Release(); delete tools->tracker; tools->tracker = NULL; tools->tracker = new Sort(/*lineArray, linecount, DETECTTYPE*/); //新接口 无需流量统计和快照 } //辅助函数:数据预处理 float VPTmeanSub[3] = { 103.52, 116.28, 123.675 }; float VPTVariance[3] = { 57.375, 57.12, 58.395 }; void datapreprocess(float * blob, int width, int height, int channels, int batchsize) { int datasize_ = width * height; for (int c = 0; c < 3; c++) { for (int i = 0; i < width * height; i++) { blob[datasize_ * c + i] = ((blob[datasize_ * c + i]) - VPTmeanSub[c]) / VPTVariance[c]; } } } int VPT_ProcessLastFrame(void * handle/*, unsigned char * rgb, int width, int height, FT_Result * result*/) { objDetector* tools = (objDetector*)handle; //CDTOOLS_Result ssdResult[MAX_OBJ_COUNT_TEST]; vector< vector > detectResult; //转化为跟踪所需要的输入 int index = 0; bool isUseDet = false; vector deleteTrackers; int ObjCount = tools->tracker->updateLastFrame(deleteTrackers); //void SaveSnapshot(vector deleteTrackers); tools->snapshotHelper->SaveSnapshot(deleteTrackers); vector>().swap(detectResult); detectResult.clear(); vector().swap(deleteTrackers); deleteTrackers.clear(); return 0; } //int VPT_Process(void * handle, unsigned char *bgr, int width, int height, int channels, int frameCount, VPT_Result *result, vector &deleteTrackers, VPT_TrafficResult* traffic, bool showTrack) int VPT_Process(void * handle, unsigned char *bgr, int width, int height, int channels, int frameCount, VPT_Result *result,/* int maxResultCount,*/ bool showTrack) { objDetector* tools = (objDetector*)handle; //CDTOOLS_Result ssdResult[MAX_OBJ_COUNT_TEST]; Mat img(height, width, CV_8UC3, bgr); cv::Mat tmp_img; img.copyTo(tmp_img); vector< vector > detectResult; //转化为跟踪所需要的输入 int index = 0; bool isUseDet = false; vector deleteTrackers; if (tools->frameCounter == FusionInterval || tools->isInitFrame) { // cout << "begin Ctools_Detector" << endl; sy_img cur_img; cur_img.set_data(img.cols, img.rows, img.channels(), img.data); ctools_result *detresult; int res_status = ctools_process(tools->detector, &cur_img, 1, &detresult); //cout << "finish Ctools_Detector" << endl; ctools_result &cur_result = detresult[0]; //(*result)[b].obj_count_ = cur_result.obj_count_ > MAX_DET_COUNT ? MAX_DET_COUNT : cur_result.obj_count_; for (int c = 0; c < cur_result.obj_count_ && c < tools->param.maxResultCount; c++) { float score = cur_result.obj_results_[c].data_[1]; if (score >= THRESHOLD) //更改为left top right bottom score index(未-1的原始数据 sort内部进行-1操作) { vector obj; obj.push_back(cur_result.obj_results_[c].data_[2]); obj.push_back(cur_result.obj_results_[c].data_[3]); obj.push_back(cur_result.obj_results_[c].data_[4]); obj.push_back(cur_result.obj_results_[c].data_[5]); obj.push_back(cur_result.obj_results_[c].data_[1]); obj.push_back(cur_result.obj_results_[c].data_[0]); detectResult.push_back(obj); } } //dResult->objCount = index; result->objCount = detectResult.size(); //VPT_Result temp; tools->isInitFrame = false; tools->frameCounter = 0; isUseDet = true; } tools->frameCounter++; //for (int j = 0; j < FusionInterval; j++) //跳帧:检测一帧,跟踪 (FusionInterval - 1)帧 //{ int objCount = tools->tracker->update(width, height, isUseDet, detectResult, result->obj, deleteTrackers); result->objCount = objCount; tools->snapshotHelper->SnapshotProcess(result, tmp_img, deleteTrackers, frameCount); vector>().swap(detectResult); detectResult.clear(); vector().swap(deleteTrackers); deleteTrackers.clear(); //} //} if (showTrack) tools->tracker->addTracker(&tmp_img); return 0; } //int VPT_ProcessImage(void * handle, unsigned char * bgr, int width, int height, int channels, VPT_Result * result) //{ // objDetector* tools = (objDetector*)handle; // // if (bgr == NULL || width == 0 || height == 0) //图像数据错误 // return -1; // // clock_t begin = clock(); // // CDTOOLS_Result ssdResult[50]; // // int count = Ctools_Detector(tools->detector, bgr, width, height, 3, ssdResult, datapreprocess); // // clock_t end = clock(); // // for (int i = 0; i < count; i++) // { // const CDTOOLS_Result& d = ssdResult[i];// Detection format: [image_id, label, score, xmin, ymin, xmax, ymax) // const float score = d.boxinfo[5]; // // result->obj[i].id = i; // result->obj[i].left = d.boxinfo[0]; // bbout[i][0]; // result->obj[i].top = d.boxinfo[1]; //bbout[i][1]; // result->obj[i].right = d.boxinfo[2]; //bbout[i][2]; // result->obj[i].bottom = d.boxinfo[3]; //bbout[i][3]; // result->obj[i].confidence = d.boxinfo[4]; // result->obj[i].index = d.boxinfo[5] - 1; // bbout[i][4]; // } // // result->objCount = count; // return count; //} void VPT_Release(void *&handle) { objDetector* tools = (objDetector*)handle; VPT_ProcessLastFrame(tools); if (tools && tools->detector) { ctools_release(&tools->detector); tools->detector = NULL; } if (tools) { delete tools; tools = NULL; } }