#include "opencv2/opencv.hpp" #include "opencv2/highgui/highgui.hpp" #include "snapshot_helper.h" #include #include "HumanParsing.h" #include "HumanCarParsing.h" #include "VehiclePlate.h" #include "VehicleRecognition.h" #include "VehicleColor.h" //#include "HumanFeatures.h" #include "vehicle_features.h" #include "VehicleRearRecg.h" #include "CropImg.h" using namespace std; string ObjTypes[9] = { "行人", "自行车", "摩托车", "三轮车", "小型车", "大车", "卡车", "拖拉机", "中巴" }; string ObjTypesEnglish[9] = { "person", "bike", "motor", "tricycle", "car", "bigbus", "lorry", "tractor", "midibus" }; DWORD SnapshotThreadProcess(LPVOID param); void snapshot_helper::snapshot_helper_init(int gpuid, double gpu_total_memory, char* dbpath, sy_command hp_analysis_config, \ sy_command hcp_analysis_config, sy_command vehicle_analysis_config, sy_command vehicle_recg_config, sy_command vehicle_plate_det_recg_config, sy_command hf_recg_config, sy_command hcf_recg_config, sy_command vcf_recg_config) { hp_analysis_cf = hp_analysis_config; hcp_analysis_cf = hcp_analysis_config; vehicle_analysis_cf = vehicle_analysis_config; vehicle_recg_cf = vehicle_recg_config; vehicle_plate_det_recg_cf = vehicle_plate_det_recg_config; hf_recg_cf = hf_recg_config; hcf_recg_cf = hcf_recg_config; vcf_recg_cf = vcf_recg_config; if (hp_analysis_config == SY_CONFIG_OPEN || hf_recg_config == SY_CONFIG_OPEN) HumanParsing_Init(hp_handle, gpuid); if (hcp_analysis_config == SY_CONFIG_OPEN || hcf_recg_config == SY_CONFIG_OPEN) HumanCarParsing_Init(hcp_handle, gpuid); if (vehicle_analysis_config == SY_CONFIG_OPEN) { VehicleColor_Init(vc_handle, gpuid); VehiclePlateDetectRecog_Init(vp_handle, gpuid); VehicleRecognition_Init(vr_handle, dbpath, gpuid); VehicleRearRecg_Init(vrr_handle, dbpath, gpuid); } if (vehicle_analysis_config == SY_CONFIG_OPEN || vcf_recg_config == SY_CONFIG_OPEN) { vhd_features_init(vhd_handle, dbpath, gpuid); } //if (vehicle_plate_det_recg_config == SY_CONFIG_OPEN) // ; //if (vehicle_recg_config == SY_CONFIG_OPEN) // ; //if (hf_recg_config == SY_CONFIG_OPEN) // human_features_init(hf_handle, dbpath, gpuid); //if (hcf_recg_config == SY_CONFIG_OPEN) //{ // if (hf_handle != nullptr) // { // hcf_handle = hf_handle; // } // else // { // human_features_init(hcf_handle, dbpath, gpuid); // } //} if (vcf_recg_config == SY_CONFIG_OPEN) { vf_features_init(vf_handle, dbpath, gpuid); //cerr << "vcf_recg_config == SY_CONFIG_OPEN !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" < lock(threadMutex); if (snapshotImageQueue.empty()) break; } } ProcessSnapshotThread.interrupt(); //interrupt thread ProcessSnapshotThread.join(); //waiting thread finish } void snapshot_helper::add_task_info(int new_task_id, TASK_INFO new_task_info) { m_task_info[new_task_id] = new_task_info; } void snapshot_helper::delete_task_info(int new_task_id, TASK_INFO new_task_info) { m_task_info[new_task_id].image_folder = NULL; m_task_info[new_task_id].snapshot_folder = NULL; m_task_info[new_task_id].obj_callback = NULL; } int snapshot_helper::save_snapshot(bool is_image, OBJ_KEY obj_key, char* filename, char* mode, float* imgData, int width, int height, int taskID, int objID, int recFlag, int left = 0, int top = 0, int right = 0, int bottom = 0) { if (recFlag) { cv::Mat newVideoImg(height, width, CV_32FC3, imgData); cv::rectangle(newVideoImg, cv::Rect(left, top, right - left, bottom - top), cv::Scalar(0, 255, 0), 1, 1, 0); SNAPSHOT_IMAGE_UNIT cur_image = {}; cur_image.file_name = filename; cur_image.original_size.original_width = width; cur_image.original_size.original_height = height; cur_image.obj_key = obj_key; cur_image.is_image = is_image; newVideoImg.copyTo(cur_image.image); { std::lock_guard lock(threadMutex); snapshotImageQueue.push(cur_image); } newVideoImg.release(); } else { cv::Mat newSnapshot(height, width, CV_32FC3, imgData); SNAPSHOT_IMAGE_UNIT cur_image = {}; cur_image.file_name = filename; cur_image.original_size.original_width = (right - left); cur_image.original_size.original_height = (bottom - top); cur_image.obj_key = obj_key; cur_image.is_image = is_image; newSnapshot.copyTo(cur_image.image); { std::lock_guard lock(threadMutex); snapshotImageQueue.push(cur_image); } newSnapshot.release(); } return 1; } int snapshot_helper::save_snapshot(bool is_image, OBJ_KEY obj_key, char* filename, char* mode, unsigned char* imgData, int width, int height, int taskID, int objID, int recFlag, int left = 0, int top = 0, int right = 0, int bottom = 0) { if (recFlag) { cv::Mat newVideoImg(height, width, CV_8UC3, imgData); cv::rectangle(newVideoImg, cv::Rect(left, top, right - left, bottom - top), cv::Scalar(0, 255, 0), 1, 1, 0); SNAPSHOT_IMAGE_UNIT cur_image = {}; cur_image.file_name = filename; cur_image.original_size.original_width = width; cur_image.original_size.original_height = height; cur_image.obj_key = obj_key; cur_image.is_image = is_image; newVideoImg.copyTo(cur_image.image); { std::lock_guard lock(threadMutex); snapshotImageQueue.push(cur_image); } newVideoImg.release(); } else { cv::Mat newSnapshot(height, width, CV_8UC3, imgData); SNAPSHOT_IMAGE_UNIT cur_image = {}; cur_image.file_name = filename; cur_image.original_size.original_width = (right - left); cur_image.original_size.original_height = (bottom - top); cur_image.obj_key = obj_key; cur_image.is_image = is_image; newSnapshot.copyTo(cur_image.image); { std::lock_guard lock(threadMutex); snapshotImageQueue.push(cur_image); } newSnapshot.release(); } return 1; } int snapshot_helper::save_snapshot(OBJ_KEY obj_key) { OBJ_VALUE & obj_value = snapShotInfo[obj_key]; if (finished_save_ss_info.find(obj_key) != finished_save_ss_info.end()) { return 0; } if (m_task_info[obj_key.videoID].image_folder) //保存视频原图 { //CPU save video screenshot int data_size = obj_value.snapShot.width * obj_value.snapShot.height * IMG_CHANNELS; float *snapshotDataHost = new float[data_size]{}; cudaMemcpy(snapshotDataHost, obj_value.snapShot.frame, data_size * sizeof(float), cudaMemcpyDeviceToHost); //cudaError_t cudaStatus = cudaGetLastError(); //if (cudaStatus != cudaSuccess) { // printf("cudaMemcpy launch failed: %s\n", cudaGetErrorString(cudaStatus)); //} char snapShotName[260]; sprintf(snapShotName, "%s/%d.jpg", m_task_info[obj_key.videoID].image_folder, obj_key.objID); save_snapshot(true,obj_key, snapShotName, "cpu", snapshotDataHost, obj_value.snapShot.width, obj_value.snapShot.height, obj_key.videoID, obj_key.objID, 1, obj_value.box.left, obj_value.box.top, obj_value.box.right, obj_value.box.bottom); if (obj_value.snapShot.frame) { //free(obj_value.snapShot.frame); cudaFree(obj_value.snapShot.frame); obj_value.snapShot.frame = NULL; } if (snapshotDataHost) { delete[] snapshotDataHost; snapshotDataHost = NULL; } } if (m_task_info[obj_key.videoID].snapshot_folder) { //CPU save obj snapshot unsigned char* tempSnapShotHost = (unsigned char*)malloc(3 * obj_value.snapShotLittle.height * obj_value.snapShotLittle.width * sizeof(unsigned char)); cudaMemcpy(tempSnapShotHost, obj_value.snapShotLittle.frame, 3 * obj_value.snapShotLittle.height * obj_value.snapShotLittle.width * sizeof(unsigned char), cudaMemcpyDeviceToHost); char snapShotName[260]; sprintf(snapShotName, "%s/%d.jpg", m_task_info[obj_key.videoID].snapshot_folder, obj_key.objID); save_snapshot(false,obj_key, snapShotName, "cpu", tempSnapShotHost, obj_value.snapShotLittle.width, obj_value.snapShotLittle.height, obj_key.videoID, obj_key.objID, 0, obj_value.box.left, obj_value.box.top, obj_value.box.right, obj_value.box.bottom); if (tempSnapShotHost) { free(tempSnapShotHost); tempSnapShotHost = NULL; } } return 0; } void snapshot_helper::save_without_analysis(OBJ_KEY obj_key) { auto iter = snapShotInfo.find(obj_key); if (iter == snapShotInfo.end()) cout << "cant find" << endl; else { save_snapshot(obj_key); snapshot_res_callback(obj_key); /*if (iter->second.snapShot.frame) cudaFree(iter->second.snapShot.frame);*/ //delete by lm 20180803 CUresult result = cuCtxPushCurrent(pThreadParam->cuCtx); if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; snapShotInfo[obj_key].snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq } cudaError_t cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("save_without_analysis last error: %s\n", cudaGetErrorString(cudaStatus)); } } void snapshot_helper::finish_task_ss_analysis(int task_id, sy_command hp_analysis_config, sy_command hcp_analysis_config, sy_command vehicle_analysis_config, sy_command hf_recg_config, sy_command hcf_recg_config, sy_command vcf_recg_config) //是否开启车辆特征识别); //某一路任务结束了,立马对该路任务进行二次属性分析 { vector hp_keys; vector hcp_keys; vector vehicle_keys; cudaError_t cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("begin_task_ss_analysis last error: %s\n", cudaGetErrorString(cudaStatus)); } printf("snapShotInfo size: %d\n", snapShotInfo.size()); int index_test = 0; for (auto iter = snapShotInfo.begin(); iter != snapShotInfo.end(); iter++) { printf("index: %d %d %d \n", index_test++, iter->first.videoID, task_id); if (iter->first.videoID == task_id) { save_snapshot(iter->first); if (iter->second.index.index == 0 && (hp_analysis_config == SY_CONFIG_OPEN || hf_recg_config == SY_CONFIG_OPEN) && m_task_info[task_id].snapshot_folder != NULL && iter->second.snapShotLittle.frame != NULL && iter->second.snapShotLittle.height == HP_HEIGHT && iter->second.snapShotLittle.width == HP_WIDTH) hp_keys.push_back(iter->first); else if ((iter->second.index.index == 1 || iter->second.index.index == 2) && (hcp_analysis_config == SY_CONFIG_OPEN || hcf_recg_config == SY_CONFIG_OPEN) && m_task_info[task_id].snapshot_folder != NULL && iter->second.snapShotLittle.frame != NULL && iter->second.snapShotLittle.height == HCP_HEIGHT && iter->second.snapShotLittle.width == HCP_WIDTH) hcp_keys.push_back(iter->first); else if ((iter->second.index.index == 8 || (iter->second.index.index >= 4 && iter->second.index.index <= 6)) && (vehicle_analysis_config == SY_CONFIG_OPEN || vcf_recg_config == SY_CONFIG_OPEN) && m_task_info[task_id].snapshot_folder != NULL && iter->second.snapShotLittle.frame != NULL && iter->second.snapShotLittle.height == VEHICLE_HEIGHT && iter->second.snapShotLittle.width == VEHICLE_WIDTH) vehicle_keys.push_back(iter->first); else ; //save_without_analysis(iter->first); cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("index last error: %s\n", cudaGetErrorString(cudaStatus)); } } } cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("finish_task_ss_analysis last error: %s\n", cudaGetErrorString(cudaStatus)); } if (!hp_keys.empty()) { const int hp_batch_size = hp_keys.size(); int hp_batch_count = OBJ_BATCH_COUNT; for (int i = 0; i <= (hp_batch_size / OBJ_BATCH_COUNT); ++i) { if (i == (hp_batch_size / OBJ_BATCH_COUNT)) { hp_batch_count = hp_batch_size % OBJ_BATCH_COUNT; } sy_img* finish_hp_img = new sy_img[hp_batch_count]{}; for (int j = 0; j < hp_batch_count; j++) { OBJ_KEY cur_obj_key = hp_keys[j]; finish_hp_img[j].set_data(HP_WIDTH, HP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } hp_res *result = new hp_res[hp_batch_count]{}; if (hp_analysis_config == SY_CONFIG_OPEN || hf_recg_config == SY_CONFIG_OPEN) { HumanParsing_Process(hp_handle, finish_hp_img, hp_batch_count, result); } int resIndex = 0; for (int k = 0; k < hp_batch_count; k++) { auto iter = snapShotInfo.find(hp_keys[k]); if (iter == snapShotInfo.end()) resIndex++; //cout << iter_key.videoID << " " << iter_key.objID << " cant find" << endl; else { hp_result curRes = {}; if (hp_analysis_config == SY_CONFIG_OPEN) { memcpy(curRes.res_objs.res_objs, result[resIndex].res_objs, sizeof(classify_obj_res)* HP_FIR_INDEX_SIZE); } if (hf_recg_config == SY_CONFIG_OPEN) { memcpy(curRes.feature, result[resIndex].feature, sizeof(float)* HF_FEA_SIZE); } //printf("finish memcpy. "); //CPUSaveImage(iter_key, snapShotInfo[iter_key]); //printf("finish callback. "); if (hp_analysis_config == SY_CONFIG_OPEN || hf_recg_config == SY_CONFIG_OPEN) { snapshot_res_callback(hp_keys[k], &curRes); } //此处未进行资源释放,图片数据还保留在数组中,之后还会再进行一次二次属性分析,但是因为回调已经置为NULL,因此不返回 resIndex++; if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq int count_person_size = count_person.size(); for (int c = 0; c < count_person_size; c++) { OBJ_KEY iter = count_person.front(); count_person.pop(); if (iter.videoID == hp_keys[k].videoID && iter.objID == hp_keys[k].objID) continue; count_person.push(iter); } } } hp_keys.erase(hp_keys.begin(), hp_keys.begin() + hp_batch_count); if (result != NULL) { delete[] result; result = NULL; } //if (result_f != NULL) //{ // delete[] result_f; // result_f = NULL; //} if (finish_hp_img != NULL) { delete[] finish_hp_img; finish_hp_img = NULL; } } } cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("hp last error: %s\n", cudaGetErrorString(cudaStatus)); } if (!hcp_keys.empty()) { const int hcp_batch_size = hcp_keys.size(); int hcp_batch_count = OBJ_BATCH_COUNT; for (int i = 0; i <= (hcp_batch_size / OBJ_BATCH_COUNT); ++i) { if (i == (hcp_batch_size / OBJ_BATCH_COUNT)) { hcp_batch_count = hcp_batch_size % OBJ_BATCH_COUNT; } sy_img* finish_hcp_img = new sy_img[hcp_batch_count]{}; for (int j = 0; j < hcp_batch_count; j++) { OBJ_KEY cur_obj_key = hcp_keys[j]; finish_hcp_img[j].set_data(HCP_WIDTH, HCP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } hcp_res *result = new hcp_res[hcp_batch_count]{}; if (hcp_analysis_config == SY_CONFIG_OPEN || hcf_recg_config == SY_CONFIG_OPEN) { HumanCarParsing_Process(hcp_handle, finish_hcp_img, hcp_batch_count, result); } /* hf_result * result_f = new hf_result[hcp_batch_size]{}; if(hcf_recg_config == SY_CONFIG_OPEN) { human_features_process(hcf_handle, finish_hcp_img, hcp_batch_size, result_f); }*/ int resIndex = 0; for (int k = 0; k < hcp_batch_count; k++) { auto iter = snapShotInfo.find(hcp_keys[k]); if (iter == snapShotInfo.end()) resIndex++; else { hcp_result curRes = {}; if (hcp_analysis_config == SY_CONFIG_OPEN) { memcpy(curRes.res_objs.res_objs, result[resIndex].res_objs, sizeof(classify_obj_res)* HCP_FIR_INDEX_SIZE); } if (hcf_recg_config == SY_CONFIG_OPEN) { memcpy(curRes.feature, result[resIndex].feature, sizeof(float)* HCF_FEA_SIZE); } if (hcp_analysis_config == SY_CONFIG_OPEN || hcf_recg_config == SY_CONFIG_OPEN) { snapshot_res_callback(hcp_keys[k], &curRes); } resIndex++; if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq int count_bike_size = count_bike.size(); for (int c = 0; c < count_bike_size; c++) { OBJ_KEY iter = count_bike.front(); count_bike.pop(); if (iter.videoID == hcp_keys[k].videoID && iter.objID == hcp_keys[k].objID) continue; count_bike.push(iter); } } } hcp_keys.erase(hcp_keys.begin(), hcp_keys.begin() + hcp_batch_count); if (result != NULL) { delete[] result; result = NULL; } /* if (result_f != NULL) { delete[] result; result_f = NULL; }*/ if (finish_hcp_img != NULL) { delete[] finish_hcp_img; finish_hcp_img = NULL; } } } cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("hcp last error: %s\n", cudaGetErrorString(cudaStatus)); } if (!vehicle_keys.empty()) { const int vehicle_batch_size = vehicle_keys.size(); int vehicle_batch_count = OBJ_BATCH_COUNT_VEHICLE; for (int i = 0; i <= (vehicle_batch_size / OBJ_BATCH_COUNT_VEHICLE); ++i) { if (i == (vehicle_batch_size / OBJ_BATCH_COUNT_VEHICLE)) { vehicle_batch_count = vehicle_batch_size % OBJ_BATCH_COUNT_VEHICLE; } sy_img* finish_vehicle_img = new sy_img[vehicle_batch_count]{}; for (int j = 0; j < vehicle_batch_count; j++) { OBJ_KEY cur_obj_key = vehicle_keys[j]; finish_vehicle_img[j].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vc_result *vcresult = new vc_result[vehicle_batch_count]{}; vplate_result *vp_result = new vplate_result[vehicle_batch_count]{}; vr_result *vrresult = new vr_result[vehicle_batch_count]{}; if (vehicle_analysis_config == SY_CONFIG_OPEN) { VehicleColor_Process(vc_handle, finish_vehicle_img, vehicle_batch_count, vcresult); VehiclePlateDetectRecog_Process(vp_handle, finish_vehicle_img, vehicle_batch_count, vp_result); VehicleRecog_Process(finish_vehicle_img, vehicle_batch_count, vrresult); } vhd_result * vhd_res = new vhd_result[vehicle_batch_count]{}; map mp_head; //车头 float ** fea = nullptr; int w_index = 0; if (vcf_recg_config == SY_CONFIG_OPEN) { sy_img * batch_vehicle_f = new sy_img[vehicle_batch_count]{};//batch_vehicle_vf; vhd_features_process(vhd_handle, finish_vehicle_img, vehicle_batch_count, vhd_res); for (int vc_idx = 0; vc_idx < vehicle_batch_count; vc_idx++) { OBJ_KEY cur_obj_key = vehicle_keys[vc_idx]; vhd_result & result = vhd_res[vc_idx]; for (int k = 0; k < result.obj_count_; ++k) { if (result.obj_results_[k].obj_index == 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[k].obj_rect.width_ * result.obj_results_[k].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_f[w_index].data_ == 0) { cudaMalloc(&batch_vehicle_f[w_index].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_f[w_index].data_, result.obj_results_[k].obj_rect.left_, result.obj_results_[k].obj_rect.top_, result.obj_results_[k].obj_rect.width_, result.obj_results_[k].obj_rect.height_); batch_vehicle_f[w_index].w_ = result.obj_results_[k].obj_rect.width_; batch_vehicle_f[w_index].h_ = result.obj_results_[k].obj_rect.height_; batch_vehicle_f[w_index].c_ = IMG_CHANNELS; ++w_index; mp_head.insert({ vc_idx, cur_obj_key }); break; } } } cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 if (w_index != 0) { fea = new float*[w_index] {}; for (int ii = 0; ii < w_index; ++ii) { fea[ii] = new float[FEATURESIZE] {}; } vf_features_process(vf_handle, batch_vehicle_f, w_index, fea); } for (size_t ii = 0; ii < vehicle_batch_count/*vehicle_keys.size()*/; ii++) { if (batch_vehicle_f[ii].data_ != NULL) cudaFree(batch_vehicle_f[ii].data_); } delete[] batch_vehicle_f; } int resIndex = 0; int feaIndex = 0; for (int vc_idx = 0; vc_idx < vehicle_batch_count; vc_idx++) { auto iter = snapShotInfo.find(vehicle_keys[vc_idx]); if (iter == snapShotInfo.end()) resIndex++; //cout << iter_key.videoID << " " <second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq for (vector::iterator iter = count_vehicle_v.begin(); iter != count_vehicle_v.end(); ) { if (iter->videoID == vehicle_keys[vc_idx].videoID && iter->objID == vehicle_keys[vc_idx].objID) iter = count_vehicle_v.erase(iter); else ++iter; } //注释掉是之后释放 因为快照可能还在异步的保存 /*if (curRes.vr_res.vehicle_brand != NULL) { delete[] curRes.vr_res.vehicle_brand; curRes.vr_res.vehicle_brand = NULL; } if (curRes.vr_res.vehicle_subbrand != NULL) { delete[] curRes.vr_res.vehicle_subbrand; curRes.vr_res.vehicle_subbrand = NULL; } if (curRes.vr_res.vehicle_issue_year != NULL) { delete[] curRes.vr_res.vehicle_issue_year; curRes.vr_res.vehicle_issue_year = NULL; } if (curRes.vr_res.vehicle_type != NULL) { delete[] curRes.vr_res.vehicle_type; curRes.vr_res.vehicle_type = NULL; } if (curRes.vr_res.freight_ton != NULL) { delete[] curRes.vr_res.freight_ton; curRes.vr_res.freight_ton = NULL; }*/ } } count_vehivle_finishanalysis = 0; vehicle_result_v.clear(); vector().swap(vehicle_result_v); if (vhd_res != NULL) { if (w_index != 0) { delete[] vhd_res; vhd_res = NULL; } } if (fea != nullptr) { for (int ii = 0; ii < w_index; ++ii) { delete[] fea[ii]; } delete[] fea; fea = nullptr; } if (vcresult != NULL) { delete[] vcresult; vcresult = NULL; } if (vp_result != NULL) { delete[] vp_result; vp_result = NULL; } if (vrresult != NULL) { delete[] vrresult; vrresult = NULL; } if (finish_vehicle_img != NULL) { delete[] finish_vehicle_img; finish_vehicle_img = NULL; } vehicle_keys.erase(vehicle_keys.begin(), vehicle_keys.begin() + vehicle_batch_count); } cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { printf("vehicle last error: %s\n", cudaGetErrorString(cudaStatus)); } } } void snapshot_helper::hp_analysis() { if (count_person.size() >= OBJ_BATCH_COUNT ) { #ifdef LOG_INFO std::cout << "------ begin HumanParsing_Process -----" << std::endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif vector erase_obj_key; for (int i = 0; i < OBJ_BATCH_COUNT; i++) { OBJ_KEY cur_obj_key = count_person.front(); erase_obj_key.push_back(cur_obj_key); if ((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame == NULL) { cout << "error data! task id: " << cur_obj_key.videoID << " obj id: " << cur_obj_key.objID << endl; } if (snapShotInfo[cur_obj_key].index.index != 0) printf("=============== index error ==================\n"); if (snapShotInfo[cur_obj_key].snapShotLittle.width != HP_WIDTH || snapShotInfo[cur_obj_key].snapShotLittle.height != HP_HEIGHT) printf("=================== width height error ======================\n"); batch_hp[i].set_data(HP_WIDTH, HP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); count_person.pop(); } hp_res *result = nullptr; if (hp_analysis_cf == SY_CONFIG_OPEN || hf_recg_cf == SY_CONFIG_OPEN) { result = new hp_res[OBJ_BATCH_COUNT]{}; HumanParsing_Process(hp_handle, batch_hp, OBJ_BATCH_COUNT, result); } /* hf_result * result_f = nullptr; if (hf_handle != nullptr) { result_f = new hf_result[OBJ_BATCH_COUNT]{}; human_features_process(hf_handle, batch_hp, OBJ_BATCH_COUNT, result_f); }*/ #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; std::printf("HumanParsing_Process: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("HumanParsing_Process = %lf ms\n", time_val); */ #endif #endif //删除已经进行保存和二次属性分析的目标 int resIndex = 0; for (auto iter_key : erase_obj_key) { //printf("begin find. "); auto iter = snapShotInfo.find(iter_key); //printf("end find. "); if (iter == snapShotInfo.end()) { resIndex++; cout << iter_key.videoID << " " << iter_key.objID << " cant find" << endl; } else { hp_result curRes{}; if (hp_analysis_cf == SY_CONFIG_OPEN) { memcpy(&curRes.res_objs, result[resIndex].res_objs, sizeof(classify_obj_res)* HP_FIR_INDEX_SIZE); } if (hf_recg_cf == SY_CONFIG_OPEN) { memcpy(curRes.feature, result[resIndex].feature, sizeof(float)* HF_FEA_SIZE); } //printf("finish memcpy. "); //CPUSaveImage(iter_key, snapShotInfo[iter_key]); //printf("finish callback. "); if (hf_recg_cf == SY_CONFIG_OPEN || hp_analysis_cf == SY_CONFIG_OPEN) { resIndex++; snapshot_res_callback(iter_key, &curRes); if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq } //printf("finish release. \n"); } } //printf("finish callback\n"); erase_obj_key.clear(); vector().swap(erase_obj_key); if (hp_analysis_cf == SY_CONFIG_OPEN || hf_recg_cf == SY_CONFIG_OPEN) { if (result != nullptr) { delete[] result; result = nullptr; } } /* if (hf_handle != nullptr) { if (result_f != nullptr) { delete[] result_f; } }*/ #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; std::printf("total: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("HumanParsing_Process = %lf ms\n", time_val); */ #endif std::cout << "----- end HumanParsing_Process -----" << std::endl; #endif //printf("real end hp\n"); } } //整体的三种二次属性分析 void snapshot_helper::hp_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag) { if (!flag) { count_person.push(obj_key); if (count_person.size() >= OBJ_BATCH_COUNT && !AttributionAnalysis) { #ifdef LOG_INFO std::cout << "------ begin HumanParsing_Process -----" << std::endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif AttributionAnalysis = true; //进行了二次属性分析 vector erase_obj_key; for (int i = 0; i < OBJ_BATCH_COUNT; i++) { OBJ_KEY cur_obj_key = count_person.front(); erase_obj_key.push_back(cur_obj_key); if ((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame == NULL) { cout << "error data! task id: " << cur_obj_key.videoID << " obj id: " << cur_obj_key.objID << endl; } if (snapShotInfo[cur_obj_key].index.index != 0) printf("=============== index error ==================\n"); if (snapShotInfo[cur_obj_key].snapShotLittle.width != HP_WIDTH || snapShotInfo[cur_obj_key].snapShotLittle.height != HP_HEIGHT) printf("=================== width height error ======================\n"); batch_hp[i].set_data(HP_WIDTH, HP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); count_person.pop(); } hp_res *result = nullptr; if (hp_analysis_cf == SY_CONFIG_OPEN || hf_recg_cf == SY_CONFIG_OPEN) { result = new hp_res[OBJ_BATCH_COUNT]{}; HumanParsing_Process(hp_handle, batch_hp, OBJ_BATCH_COUNT, result); } /* hf_result * result_f = nullptr; if (hf_handle != nullptr) { result_f = new hf_result[OBJ_BATCH_COUNT]{}; human_features_process(hf_handle, batch_hp, OBJ_BATCH_COUNT, result_f); }*/ #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; std::printf("HumanParsing_Process: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("HumanParsing_Process = %lf ms\n", time_val); */ #endif #endif //删除已经进行保存和二次属性分析的目标 int resIndex = 0; for (auto iter_key : erase_obj_key) { //printf("begin find. "); auto iter = snapShotInfo.find(iter_key); //printf("end find. "); if (iter == snapShotInfo.end()) { resIndex++; cout << iter_key.videoID << " " << iter_key.objID << " cant find" << endl; } else { hp_result curRes{}; if (hp_analysis_cf == SY_CONFIG_OPEN) { memcpy(&curRes.res_objs, result[resIndex].res_objs, sizeof(classify_obj_res)* HP_FIR_INDEX_SIZE); } if (hf_recg_cf == SY_CONFIG_OPEN) { memcpy(curRes.feature, result[resIndex].feature, sizeof(float)* HF_FEA_SIZE); } //printf("finish memcpy. "); //CPUSaveImage(iter_key, snapShotInfo[iter_key]); //printf("finish callback. "); if (hf_recg_cf == SY_CONFIG_OPEN || hp_analysis_cf == SY_CONFIG_OPEN) { resIndex++; snapshot_res_callback(iter_key, &curRes); if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq } //printf("finish release. \n"); } } //printf("finish callback\n"); erase_obj_key.clear(); vector().swap(erase_obj_key); if (hp_analysis_cf == SY_CONFIG_OPEN || hf_recg_cf == SY_CONFIG_OPEN) { if (result != nullptr) { delete[] result; result = nullptr; } } /* if (hf_handle != nullptr) { if (result_f != nullptr) { delete[] result_f; } }*/ #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; std::printf("total: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("HumanParsing_Process = %lf ms\n", time_val); */ #endif std::cout << "----- end HumanParsing_Process -----" << std::endl; #endif //printf("real end hp\n"); } } } //void snapshot_helper::hf_features(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag) //{ // if (!flag) // { // bool is_only_hf = false; // vector erase_obj_key; // if (hp_handle == nullptr) // { // count_person.push(obj_key); // // if (count_person.size() >= OBJ_BATCH_COUNT && !AttributionAnalysis) // { // for (int i = 0; i < OBJ_BATCH_COUNT; i++) // { // OBJ_KEY cur_obj_key = count_person.front(); // erase_obj_key.push_back(cur_obj_key); // // if ((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame == NULL) // { // cout << "error data! task id: " << cur_obj_key.videoID << " obj id: " << cur_obj_key.objID << endl; // } // // if (snapShotInfo[cur_obj_key].index.index != 0) // printf("=============== index error ==================\n"); // // if (snapShotInfo[cur_obj_key].snapShotLittle.width != HP_WIDTH || snapShotInfo[cur_obj_key].snapShotLittle.height != HP_HEIGHT) // printf("=================== width height error ======================\n"); // // batch_hp[i].set_data(HP_WIDTH, HP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); // count_person.pop(); // } // // } // } // // if (count_person.size() >= OBJ_BATCH_COUNT && && !AttributionAnalysis) // { // AttributionAnalysis = true; //进行了二次属性分析 // if (hp_handle != nullptr) // { // for (int i = 0; i < OBJ_BATCH_COUNT; i++) // { // OBJ_KEY cur_obj_key = count_person.front(); // erase_obj_key.push_back(cur_obj_key); // count_person.pop(); // } // } // // // std::unique_ptr ptr(new hf_result[OBJ_BATCH_COUNT]{}); // hf_result * result = ptr.get(); // human_features_process(hf_handle, batch_hp, OBJ_BATCH_COUNT, result); // //删除已经进行保存和二次属性分析的目标 // int resIndex = 0; // for (auto iter_key : erase_obj_key) // { // //printf("begin find. "); // auto iter = snapShotInfo.find(iter_key); // // //printf("end find. "); // if (iter == snapShotInfo.end()) // { // resIndex++; // cout << iter_key.videoID << " " << iter_key.objID << " cant find" << endl; // } // else // { // hp_result curRes{}; // if (hp_handle != nullptr) // { // curRes = snapShotInfo_hpf[iter_key]; // } // memcpy(curRes.feature, result[resIndex++].res_objs, Hf_FIR_INDEX_SIZE); // // //printf("finish memcpy. "); // //CPUSaveImage(iter_key, snapShotInfo[iter_key]); // snapshot_res_callback(iter_key, &curRes); // //printf("finish callback. "); // if (iter->second.snapShotLittle.frame) // { // cudaFree(iter->second.snapShotLittle.frame); // iter->second.snapShotLittle.frame = NULL; // } // snapShotInfo.erase(iter);//modified by dyq // //printf("finish release. \n"); // } // } // // //printf("finish callback\n"); // // erase_obj_key.clear(); // vector().swap(erase_obj_key); // // //#ifdef LOG_INFO //#ifdef _MSC_VER // QueryPerformanceCounter(&nEndTime); // time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; // std::printf("total: %.2f ms \n", time_val); //#else // /* // gettimeofday(&t2, NULL); // time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; // printf("HumanParsing_Process = %lf ms\n", time_val); // */ //#endif // std::cout << "----- end HumanParsing_Process -----" << std::endl; //#endif // //printf("real end hp\n"); // } // } // // //} void snapshot_helper::hp_analysis(OBJ_KEY obj_key) { count_person.push(obj_key); } void snapshot_helper::hcp_analysis(OBJ_KEY obj_key) { count_bike.push(obj_key); } void snapshot_helper::vehicle_analysis(OBJ_KEY obj_key) { count_vehicle_v.push_back(obj_key); } void snapshot_helper::object_attri_analysis() { //count_person.size()的类型是size_t不为0,因为这个地方直接用三元运算符的话,会强制类型转换到size_t,没有办法和0比较 int analysis_person_size = count_person.size() - OBJ_BATCH_COUNT; analysis_person_size = max(0, analysis_person_size); int analysis_bike_size = count_bike.size() - OBJ_BATCH_COUNT; analysis_bike_size = max(0, analysis_bike_size); int analysis_vehicle_size = count_vehicle_v.size() - OBJ_BATCH_COUNT_VEHICLE; analysis_vehicle_size = max(0, analysis_vehicle_size); int tmp_size = max(analysis_person_size, max(analysis_bike_size, analysis_vehicle_size)); int analysis_type = -1; if (tmp_size == 0) return; else if (tmp_size == analysis_person_size) analysis_type = HP_ANALYSIS; else if (tmp_size == analysis_bike_size) analysis_type = HCP_ANALYSIS; else { analysis_type = VC_ANALYSIS; } switch (analysis_type) { case HP_ANALYSIS: //printf("============ begin hp_analysis. ana size: %d. \n", count_person.size()); hp_analysis(); break; case HCP_ANALYSIS: //printf("============ begin hcp_analysis. ana size: %d. \n", count_bike.size()); hcp_analysis(); break; case VC_ANALYSIS: if (vc_analysis == VC_ANALYSIS_TYPE::VC_ANALYSIS) { vehicle_color_analysis(); vc_analysis = VC_ANALYSIS_TYPE::VR_VF_ANALYSIS; } else if (vc_analysis == VC_ANALYSIS_TYPE::VR_VF_ANALYSIS) { vehicle_recg_analysis(); vc_analysis = VC_ANALYSIS_TYPE::VPDR_ANALYSIS; } else if (vc_analysis == VC_ANALYSIS_TYPE::VPDR_ANALYSIS) { vehicle_plate_dr_analysis(); vc_analysis = VC_ANALYSIS_TYPE::VC_ANALYSIS; } //printf("============ begin vc_vr_analysis. ana size: %d. \n", count_vehicle_v.size()); //vf_feature(); break; default: break; } } void snapshot_helper::hcp_analysis() { if (count_bike.size() >= OBJ_BATCH_COUNT ) { vector erase_obj_key; #ifdef LOG_INFO std::cout << "----- begin HumanCarParsing_Process -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = 0; i < OBJ_BATCH_COUNT; i++) { OBJ_KEY cur_obj_key = count_bike.front(); erase_obj_key.push_back(cur_obj_key); if (snapShotInfo[cur_obj_key].index.index != 1 && snapShotInfo[cur_obj_key].index.index != 2) printf("=============== index error ==================\n"); if (snapShotInfo[cur_obj_key].snapShotLittle.width != HCP_WIDTH || snapShotInfo[cur_obj_key].snapShotLittle.height != HCP_HEIGHT) printf("=================== width height error ======================\n"); batch_hcp[i].set_data(HCP_WIDTH, HCP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); count_bike.pop(); } hcp_res *result = nullptr; if (hcp_analysis_cf == SY_CONFIG_OPEN || hcf_recg_cf == SY_CONFIG_OPEN) { result = new hcp_res[OBJ_BATCH_COUNT]{}; HumanCarParsing_Process(hcp_handle, batch_hcp, OBJ_BATCH_COUNT, result); } //hf_result * result_f = nullptr; //if (hcf_handle != nullptr) //{ // result_f = new hf_result[OBJ_BATCH_COUNT]{}; // human_features_process(hcf_handle, batch_hcp, OBJ_BATCH_COUNT, result_f); //} #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("HumanCarParsing_Process: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("HumanCarParsing_Process = %lf ms\n", time_val); */ #endif cout << "----- end HumanCarParsing_Process -----" << endl; #endif int resIndex = 0; for (auto iter_key : erase_obj_key) { //printf("begin find. "); auto iter = snapShotInfo.find(iter_key); //printf("end find. "); if (iter == snapShotInfo.end()) { resIndex++; cout << iter_key.videoID << " " << iter_key.objID << " cant find" << endl; } else { hcp_result curRes{}; if (hcp_analysis_cf == SY_CONFIG_OPEN) { memcpy(&curRes.res_objs, result[resIndex].res_objs, sizeof(classify_obj_res)* HCP_FIR_INDEX_SIZE); //printf("hcp res_index = %d\n", result[resIndex].res_objs[0].res_index); } if (hcf_recg_cf == SY_CONFIG_OPEN) { memcpy(curRes.feature, result[resIndex].feature, sizeof(float)* HCF_FEA_SIZE); } //printf("finish memcpy. "); //CPUSaveImage(iter_key, snapShotInfo[iter_key]); //printf("finish callback. "); if (hcf_recg_cf == SY_CONFIG_OPEN || hcp_analysis_cf == SY_CONFIG_OPEN) { resIndex++; snapshot_res_callback(iter_key, &curRes); //printf("finish callback. "); if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq } //printf("finish release. \n"); } } //printf("finish callback\n"); erase_obj_key.clear(); vector().swap(erase_obj_key); if (hcp_analysis_cf == SY_CONFIG_OPEN || hcf_recg_cf == SY_CONFIG_OPEN) { if (result != nullptr) { delete[] result; result = nullptr; } } //if (hcf_handle != nullptr) //{ // if (result_f != nullptr) // { // delete[] result_f; // } //} //printf("real end hcp\n"); } } void snapshot_helper::hcp_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag) { if (!flag) { count_bike.push(obj_key); if (count_bike.size() >= OBJ_BATCH_COUNT && !AttributionAnalysis) { AttributionAnalysis = true; vector erase_obj_key; #ifdef LOG_INFO std::cout << "----- begin HumanCarParsing_Process -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = 0; i < OBJ_BATCH_COUNT; i++) { OBJ_KEY cur_obj_key = count_bike.front(); erase_obj_key.push_back(cur_obj_key); if (snapShotInfo[cur_obj_key].index.index != 1 && snapShotInfo[cur_obj_key].index.index != 2) printf("=============== index error ==================\n"); if (snapShotInfo[cur_obj_key].snapShotLittle.width != HCP_WIDTH || snapShotInfo[cur_obj_key].snapShotLittle.height != HCP_HEIGHT) printf("=================== width height error ======================\n"); batch_hcp[i].set_data(HCP_WIDTH, HCP_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); count_bike.pop(); } hcp_res *result = nullptr; if (hcp_analysis_cf == SY_CONFIG_OPEN || hcf_recg_cf == SY_CONFIG_OPEN) { result = new hcp_res[OBJ_BATCH_COUNT]{}; HumanCarParsing_Process(hcp_handle, batch_hcp, OBJ_BATCH_COUNT, result); } //hf_result * result_f = nullptr; //if (hcf_handle != nullptr) //{ // result_f = new hf_result[OBJ_BATCH_COUNT]{}; // human_features_process(hcf_handle, batch_hcp, OBJ_BATCH_COUNT, result_f); //} #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("HumanCarParsing_Process: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("HumanCarParsing_Process = %lf ms\n", time_val); */ #endif cout << "----- end HumanCarParsing_Process -----" << endl; #endif int resIndex = 0; for (auto iter_key : erase_obj_key) { //printf("begin find. "); auto iter = snapShotInfo.find(iter_key); //printf("end find. "); if (iter == snapShotInfo.end()) { resIndex++; cout << iter_key.videoID << " " << iter_key.objID << " cant find" << endl; } else { hcp_result curRes{}; if (hcp_analysis_cf == SY_CONFIG_OPEN) { memcpy(&curRes.res_objs, result[resIndex].res_objs, sizeof(classify_obj_res)* HCP_FIR_INDEX_SIZE); //printf("hcp res_index = %d\n", result[resIndex].res_objs[0].res_index); } if (hcf_recg_cf == SY_CONFIG_OPEN) { memcpy(curRes.feature, result[resIndex].feature, sizeof(float)* HCF_FEA_SIZE); } //printf("finish memcpy. "); //CPUSaveImage(iter_key, snapShotInfo[iter_key]); //printf("finish callback. "); if (hcf_recg_cf == SY_CONFIG_OPEN || hcp_analysis_cf == SY_CONFIG_OPEN) { resIndex++; snapshot_res_callback(iter_key, &curRes); //printf("finish callback. "); if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter);//modified by dyq } //printf("finish release. \n"); } } //printf("finish callback\n"); erase_obj_key.clear(); vector().swap(erase_obj_key); if (hcp_analysis_cf == SY_CONFIG_OPEN || hcf_recg_cf == SY_CONFIG_OPEN) { if (result != nullptr) { delete[] result; result = nullptr; } } //if (hcf_handle != nullptr) //{ // if (result_f != nullptr) // { // delete[] result_f; // } //} //printf("real end hcp\n"); } } } void snapshot_helper::vehicle_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis, bool flag) { } void snapshot_helper::vehicle_color_analysis() { int count_vehicle_unanalysis = count_vehicle_v.size() - count_vehivle_finishanalysis; //当前数组中还有多少个未进行二次属性分析 if (count_vehicle_unanalysis >= OBJ_BATCH_COUNT_VEHICLE && vehicle_analysis_cf == SY_CONFIG_OPEN) { #ifdef LOG_INFO cout << "----- begin VehicleColor_Process -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = count_vehivle_finishanalysis; i < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i - count_vehivle_finishanalysis].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vc_result *vcresult = new vc_result[OBJ_BATCH_COUNT_VEHICLE]{}; VehicleColor_Process(vc_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vcresult); #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("VehicleColor_Process: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("VehicleColor_Process = %lf ms\n", time_val); */ #endif #endif for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; i++) //for (auto iter_key : erase_obj_key) { auto iter = snapShotInfo.find(count_vehicle_v[i + count_vehivle_finishanalysis]); if (iter == snapShotInfo.end()) { //cout << " cant find" << endl; } else { vehicle_result curRes = {}; memcpy(&curRes.vc_res, &vcresult[i], sizeof(vc_result)); vehicle_result_v.push_back(curRes); } } if (vcresult != NULL) { delete[] vcresult; vcresult = NULL; } } } //针对车拆开的二次属性分析 void snapshot_helper::vehicle_color_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis) { count_vehicle_v.push_back(obj_key); int count_vehicle_unanalysis = count_vehicle_v.size() - count_vehivle_finishanalysis; //当前数组中还有多少个未进行二次属性分析 if (count_vehicle_unanalysis >= OBJ_BATCH_COUNT_VEHICLE && !AttributionAnalysis&& vehicle_analysis_cf == SY_CONFIG_OPEN) { AttributionAnalysis = true; #ifdef LOG_INFO cout << "----- begin VehicleColor_Process -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = count_vehivle_finishanalysis; i < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i - count_vehivle_finishanalysis].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vc_result *vcresult = new vc_result[OBJ_BATCH_COUNT_VEHICLE]{}; VehicleColor_Process(vc_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vcresult); #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("VehicleColor_Process: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("VehicleColor_Process = %lf ms\n", time_val); */ #endif #endif for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; i++) //for (auto iter_key : erase_obj_key) { auto iter = snapShotInfo.find(count_vehicle_v[i + count_vehivle_finishanalysis]); if (iter == snapShotInfo.end()) { //cout << " cant find" << endl; } else { vehicle_result curRes = {}; memcpy(&curRes.vc_res, &vcresult[i], sizeof(vc_result)); vehicle_result_v.push_back(curRes); } } if (vcresult != NULL) { delete[] vcresult; vcresult = NULL; } } } void snapshot_helper::vf_feature(sy_img *batch_vehicle_f, int batch_size, map &mp_head) { if (0 == batch_size) { if (vehicle_analysis_cf == SY_CONFIG_OPEN) { count_vehivle_finishanalysis += OBJ_BATCH_COUNT_VEHICLE; } return; } float ** fea = new float*[batch_size] {}; for (int i = 0; i < batch_size; ++i) { fea[i] = new float[FEATURESIZE] {}; } vf_features_process(vf_handle, batch_vehicle_f, batch_size, fea); if (vehicle_analysis_cf == SY_CONFIG_CLOSE) { //std::cerr << "*****************************vf_feature 1 *******************************************"<second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter); } } count_vehicle_v.erase(count_vehicle_v.begin() + count_vehivle_finishanalysis, count_vehicle_v.begin() + count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE); } else { //std::cerr << "*****************************vf_feature 2 *******************************************"<= OBJ_BATCH_COUNT_VEHICLE && vcf_recg_cf == SY_CONFIG_OPEN) //if (count_vehicle_v.size() >= OBJ_BATCH_COUNT_VEHICLE && vhd_handle != nullptr) { sy_img * batch_vehicle_f = batch_vehicle_vf; for (int i = count_vehivle_finishanalysis; i < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i - count_vehivle_finishanalysis].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vhd_result * vhd_res = new vhd_result[OBJ_BATCH_COUNT_VEHICLE]{}; /* for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; ++i) { vcwd_res[i].vhd_info = new vcwd_info[VD_MAXDETECTCOUNT]{}; vcwd_res[i].vrd_info = new vcwd_info[VD_MAXDETECTCOUNT]{}; }*/ vhd_features_process(vhd_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vhd_res); int w_index = 0; map mp; //检测到的车窗的索引和OBJ_KEY for (int vc_idx = count_vehivle_finishanalysis; vc_idx < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; vc_idx++) { OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; vhd_result & result = vhd_res[vc_idx - count_vehivle_finishanalysis]; for (int i = 0; i < result.obj_count_; ++i) //车头 { if (result.obj_results_[i].obj_rect.width_ != 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; if (batch_vehicle_f[w_index].data_ == 0) { cudaMalloc(&batch_vehicle_f[w_index].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_f[w_index].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_f[w_index].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_f[w_index].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_f[w_index].c_ = IMG_CHANNELS; ++w_index; mp.insert({ vc_idx, cur_obj_key }); break; } } } cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 if (w_index == 0) { if (vhd_res != NULL) { delete[] vhd_res; vhd_res = NULL; } return; } float ** fea = new float*[w_index] {}; for (int i = 0; i < w_index; ++i) { fea[i] = new float[FEATURESIZE] {}; } vf_features_process(vf_handle, batch_vehicle_f, w_index, fea); if (vehicle_analysis_cf == SY_CONFIG_CLOSE) { //std::cerr << "*****************************vf_feature 1 *******************************************"<second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter); } } count_vehicle_v.erase(count_vehicle_v.begin() + count_vehivle_finishanalysis, count_vehicle_v.begin() + count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE); } else { //std::cerr << "*****************************vf_feature 2 *******************************************"<= OBJ_BATCH_COUNT_VEHICLE && !AttributionAnalysis && vcf_recg_cf == SY_CONFIG_OPEN) //if (count_vehicle_v.size() >= OBJ_BATCH_COUNT_VEHICLE && vhd_handle != nullptr) { sy_img * batch_vehicle_f = batch_vehicle_vf; for (int i = count_vehivle_finishanalysis; i < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i - count_vehivle_finishanalysis].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vhd_result * vhd_res = new vhd_result[OBJ_BATCH_COUNT_VEHICLE]{}; /* for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; ++i) { vcwd_res[i].vhd_info = new vcwd_info[VD_MAXDETECTCOUNT]{}; vcwd_res[i].vrd_info = new vcwd_info[VD_MAXDETECTCOUNT]{}; }*/ vhd_features_process(vhd_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vhd_res); int w_index = 0; map mp; //检测到的车窗的索引和OBJ_KEY for (int vc_idx = count_vehivle_finishanalysis; vc_idx < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; vc_idx++) { OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; vhd_result & result = vhd_res[vc_idx- count_vehivle_finishanalysis]; for (int i = 0; i < result.obj_count_; ++i) //车头 { if (result.obj_results_[i].obj_rect.width_ != 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; if (batch_vehicle_f[w_index].data_ == 0) { cudaMalloc(&batch_vehicle_f[w_index].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_f[w_index].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_f[w_index].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_f[w_index].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_f[w_index].c_ = IMG_CHANNELS; ++w_index; mp.insert({ vc_idx, cur_obj_key }); break; } } } cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 if (w_index == 0) { if (vhd_res != NULL) { delete[] vhd_res; vhd_res = NULL; } return; } float ** fea = new float*[w_index] {}; for (int i = 0; i < w_index; ++i) { fea[i] = new float[FEATURESIZE] {}; } vf_features_process(vf_handle, batch_vehicle_f, w_index, fea); if (vehicle_analysis_cf == SY_CONFIG_CLOSE) { //std::cerr << "*****************************vf_feature 1 *******************************************"<second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } snapShotInfo.erase(iter); } } count_vehicle_v.erase(count_vehicle_v.begin()+ count_vehivle_finishanalysis, count_vehicle_v.begin() + count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE); } else { //std::cerr << "*****************************vf_feature 2 *******************************************"<= OBJ_BATCH_COUNT_VEHICLE && count_vehicle_v.size() >= OBJ_BATCH_COUNT_VEHICLE && vehicle_analysis_cf == SY_CONFIG_OPEN) { #ifdef LOG_INFO cout << "----- begin VEHICLEPLATEAnalysis -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vplate_result *vp_result = nullptr; if (vehicle_analysis_cf == SY_CONFIG_OPEN) { vp_result = new vplate_result[OBJ_BATCH_COUNT_VEHICLE]{}; VehiclePlateDetectRecog_Process(vp_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vp_result); } //double ** fea = nullptr; //int w_index = 0; //sy_img * batch_vehicle_f = batch_vehicle_vf; //if (vhd_handle != nullptr) //{ // vcwd_result * vcwd_res = new vcwd_result[OBJ_BATCH_COUNT_VEHICLE]{}; // for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; ++i) // { // vcwd_res[i].vhd_info = new vcwd_info[FEATURESIZE]{}; // vcwd_res[i].vrd_info = new vcwd_info[FEATURESIZE]{}; // } // vcwd_features_process(vhd_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vcwd_res); // // map mp; //检测到的车窗的索引和OBJ_KEY // for (int vc_idx = 0; vc_idx < OBJ_BATCH_COUNT_VEHICLE; vc_idx++) // { // OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; // vcwd_result & result = vcwd_res[vc_idx]; // for (int i = 0; i < result.h_count; ++i) //车头 // { // if (result.vhd_info[i].w_info.rect.width_ != 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) // { // int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; // if (batch_vehicle_f[w_index].data_ == 0) // { // cudaMalloc(&batch_vehicle_f[w_index].data_, w_imglength * sizeof(unsigned char)); // } // cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, // batch_vehicle_f[w_index].data_, result.vhd_info[i].w_info.rect.left_, result.vhd_info[i].w_info.rect.top_, result.vhd_info[i].w_info.rect.width_, result.vhd_info[i].w_info.rect.height_); // batch_vehicle_f[w_index].w_ = result.vhd_info[i].w_info.rect.width_; // batch_vehicle_f[w_index].h_ = result.vhd_info[i].w_info.rect.height_; // batch_vehicle_f[w_index].c_ = IMG_CHANNELS; // ++w_index; // mp.insert({ vc_idx, cur_obj_key }); // break; // } // } // } // cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 // if (w_index == 0) // { // for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; ++i) // { // delete[] vcwd_res[i].vhd_info; // delete[] vcwd_res[i].vrd_info; // } // delete[] vcwd_res; // return; // } // double ** fea = new double*[w_index] {}; // for (int i = 0; i < w_index; ++i) // { // fea[i] = new double[FEATURESIZE] {}; // } // vf_features_process(vf_handle, batch_vehicle_f, w_index, fea); //} #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("VEHICLEPLATEAnalysis: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("VehicleColor_Process = %lf ms\n", time_val); */ #endif #endif //printf("begin copy\n"); int resIndex = 0; for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; i++) //for (auto iter_key : erase_obj_key) { auto iter = snapShotInfo.find(count_vehicle_v[i]); if (iter == snapShotInfo.end()) resIndex++; else { vehicle_result &curRes = vehicle_result_v[i]; //VP memcpy(&curRes.vp_res, &vp_result[resIndex], sizeof(vplate_result)); //CPUSaveImage(count_vehicle_v[i], snapShotInfo[count_vehicle_v[i]]); snapshot_res_callback(count_vehicle_v[i], &curRes); //注释掉是之后释放 因为快照可能还在异步的保存 /* if (curRes.vr_res.vehicle_brand != NULL) { delete[] curRes.vr_res.vehicle_brand; curRes.vr_res.vehicle_brand = NULL; } if (curRes.vr_res.vehicle_subbrand != NULL) { delete[] curRes.vr_res.vehicle_subbrand; curRes.vr_res.vehicle_subbrand = NULL; } if (curRes.vr_res.vehicle_issue_year != NULL) { delete[] curRes.vr_res.vehicle_issue_year; curRes.vr_res.vehicle_issue_year = NULL; } if (curRes.vr_res.vehicle_type != NULL) { delete[] curRes.vr_res.vehicle_type; curRes.vr_res.vehicle_type = NULL; } if (curRes.vr_res.freight_ton != NULL) { delete[] curRes.vr_res.freight_ton; curRes.vr_res.freight_ton = NULL; }*/ if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } //delete by lm 20180803 checkCudaErrors(cuCtxPopCurrent(NULL)); snapShotInfo.erase(iter);//modified by dyq resIndex++; } } //printf("end copy\n"); vehicle_result_v.erase(vehicle_result_v.begin(), vehicle_result_v.begin() + OBJ_BATCH_COUNT_VEHICLE); count_vehicle_v.erase(count_vehicle_v.begin(), count_vehicle_v.begin() + OBJ_BATCH_COUNT_VEHICLE); count_vehivle_finishanalysis -= OBJ_BATCH_COUNT_VEHICLE; //前OBJ_BATCH_COUNT_VEHICLE个快照已经完成二次属性分析 if (vehicle_analysis_cf == SY_CONFIG_OPEN) { if (vp_result != NULL) { delete[] vp_result; vp_result = NULL; } } //if (vhd_handle != nullptr) //{ // for (int i = 0; i < w_index; ++i) // { // delete[] fea[i]; // //cudaFree(batch_vehicle_f[w_index].data_); // } // delete[] fea; // fea = nullptr; //} // printf("real finish vpdr\n"); } } void snapshot_helper::vehicle_plate_dr_analysis(OBJ_KEY obj_key, OBJ_VALUE obj_value, bool AttributionAnalysis) { if (count_vehivle_finishanalysis >= OBJ_BATCH_COUNT_VEHICLE && count_vehicle_v.size() >= OBJ_BATCH_COUNT_VEHICLE && !AttributionAnalysis && vehicle_analysis_cf == SY_CONFIG_OPEN) { AttributionAnalysis = true; #ifdef LOG_INFO cout << "----- begin VEHICLEPLATEAnalysis -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } vplate_result *vp_result = nullptr; if (vehicle_analysis_cf == SY_CONFIG_OPEN) { vp_result = new vplate_result[OBJ_BATCH_COUNT_VEHICLE]{}; VehiclePlateDetectRecog_Process(vp_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vp_result); } //double ** fea = nullptr; //int w_index = 0; //sy_img * batch_vehicle_f = batch_vehicle_vf; //if (vhd_handle != nullptr) //{ // vcwd_result * vcwd_res = new vcwd_result[OBJ_BATCH_COUNT_VEHICLE]{}; // for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; ++i) // { // vcwd_res[i].vhd_info = new vcwd_info[FEATURESIZE]{}; // vcwd_res[i].vrd_info = new vcwd_info[FEATURESIZE]{}; // } // vcwd_features_process(vhd_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vcwd_res); // // map mp; //检测到的车窗的索引和OBJ_KEY // for (int vc_idx = 0; vc_idx < OBJ_BATCH_COUNT_VEHICLE; vc_idx++) // { // OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; // vcwd_result & result = vcwd_res[vc_idx]; // for (int i = 0; i < result.h_count; ++i) //车头 // { // if (result.vhd_info[i].w_info.rect.width_ != 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) // { // int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; // if (batch_vehicle_f[w_index].data_ == 0) // { // cudaMalloc(&batch_vehicle_f[w_index].data_, w_imglength * sizeof(unsigned char)); // } // cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, // batch_vehicle_f[w_index].data_, result.vhd_info[i].w_info.rect.left_, result.vhd_info[i].w_info.rect.top_, result.vhd_info[i].w_info.rect.width_, result.vhd_info[i].w_info.rect.height_); // batch_vehicle_f[w_index].w_ = result.vhd_info[i].w_info.rect.width_; // batch_vehicle_f[w_index].h_ = result.vhd_info[i].w_info.rect.height_; // batch_vehicle_f[w_index].c_ = IMG_CHANNELS; // ++w_index; // mp.insert({ vc_idx, cur_obj_key }); // break; // } // } // } // cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 // if (w_index == 0) // { // for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; ++i) // { // delete[] vcwd_res[i].vhd_info; // delete[] vcwd_res[i].vrd_info; // } // delete[] vcwd_res; // return; // } // double ** fea = new double*[w_index] {}; // for (int i = 0; i < w_index; ++i) // { // fea[i] = new double[FEATURESIZE] {}; // } // vf_features_process(vf_handle, batch_vehicle_f, w_index, fea); //} #ifdef LOG_INFO #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("VEHICLEPLATEAnalysis: %.2f ms \n", time_val); #else /* gettimeofday(&t2, NULL); time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0; printf("VehicleColor_Process = %lf ms\n", time_val); */ #endif #endif //printf("begin copy\n"); int resIndex = 0; for (int i = 0; i < OBJ_BATCH_COUNT_VEHICLE; i++) //for (auto iter_key : erase_obj_key) { auto iter = snapShotInfo.find(count_vehicle_v[i]); if (iter == snapShotInfo.end()) resIndex++; else { vehicle_result &curRes = vehicle_result_v[i]; //VP memcpy(&curRes.vp_res, &vp_result[resIndex], sizeof(vplate_result)); //CPUSaveImage(count_vehicle_v[i], snapShotInfo[count_vehicle_v[i]]); snapshot_res_callback(count_vehicle_v[i], &curRes); //注释掉是之后释放 因为快照可能还在异步的保存 /* if (curRes.vr_res.vehicle_brand != NULL) { delete[] curRes.vr_res.vehicle_brand; curRes.vr_res.vehicle_brand = NULL; } if (curRes.vr_res.vehicle_subbrand != NULL) { delete[] curRes.vr_res.vehicle_subbrand; curRes.vr_res.vehicle_subbrand = NULL; } if (curRes.vr_res.vehicle_issue_year != NULL) { delete[] curRes.vr_res.vehicle_issue_year; curRes.vr_res.vehicle_issue_year = NULL; } if (curRes.vr_res.vehicle_type != NULL) { delete[] curRes.vr_res.vehicle_type; curRes.vr_res.vehicle_type = NULL; } if (curRes.vr_res.freight_ton != NULL) { delete[] curRes.vr_res.freight_ton; curRes.vr_res.freight_ton = NULL; }*/ if (iter->second.snapShotLittle.frame) { cudaFree(iter->second.snapShotLittle.frame); iter->second.snapShotLittle.frame = NULL; } //delete by lm 20180803 checkCudaErrors(cuCtxPopCurrent(NULL)); snapShotInfo.erase(iter);//modified by dyq resIndex++; } } //printf("end copy\n"); vehicle_result_v.erase(vehicle_result_v.begin(), vehicle_result_v.begin() + OBJ_BATCH_COUNT_VEHICLE); count_vehicle_v.erase(count_vehicle_v.begin(), count_vehicle_v.begin() + OBJ_BATCH_COUNT_VEHICLE); count_vehivle_finishanalysis -= OBJ_BATCH_COUNT_VEHICLE; //前OBJ_BATCH_COUNT_VEHICLE个快照已经完成二次属性分析 if (vehicle_analysis_cf == SY_CONFIG_OPEN) { if (vp_result != NULL) { delete[] vp_result; vp_result = NULL; } } //if (vhd_handle != nullptr) //{ // for (int i = 0; i < w_index; ++i) // { // delete[] fea[i]; // //cudaFree(batch_vehicle_f[w_index].data_); // } // delete[] fea; // fea = nullptr; //} // printf("real finish vpdr\n"); } } void snapshot_helper::VehicleRecog_Process(sy_img * batch_img, int batchsize, vr_result *&vresult) { int index_head = 0; int index_trail = 0; int index_other = 0; vector mp_head; vector mp_trail; vector mp_other; sy_img * batch_vehicle_head = new sy_img[batchsize]{}; sy_img * batch_vehicle_trail = new sy_img[batchsize]{}; sy_img * batch_vehicle_other = new sy_img[batchsize]{}; vhd_result * vhd_res = new vhd_result[batchsize]{}; vhd_features_process(vhd_handle, batch_img, batchsize, vhd_res); for (int vc_idx = 0; vc_idx < batchsize; vc_idx++) { OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; vhd_result & result = vhd_res[vc_idx]; for (int i = 0; i < result.obj_count_; ++i) { if (result.obj_results_[i].obj_index == 0 && batch_img[i].w_ == VEHICLE_WIDTH && batch_img[i].h_ == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[i].obj_rect.width_ * result.obj_results_[i].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_head[index_head].data_ == 0) { cudaMalloc(&batch_vehicle_head[index_head].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)batch_img[i].data_, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_head[index_head].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_head[index_head].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_head[index_head].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_head[index_head].c_ = IMG_CHANNELS; ++index_head; mp_head.push_back(vc_idx ); break; } if (result.obj_results_[i].obj_index == 1 && batch_img[i].w_ == VEHICLE_WIDTH && batch_img[i].h_ == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[i].obj_rect.width_ * result.obj_results_[i].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_trail[index_trail].data_ == 0) { cudaMalloc(&batch_vehicle_trail[index_trail].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)batch_img[i].data_, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_trail[index_trail].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_trail[index_trail].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_trail[index_trail].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_trail[index_trail].c_ = IMG_CHANNELS; ++index_trail; mp_trail.push_back(vc_idx); break; } else { int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; cudaMalloc(&batch_vehicle_other[index_other].data_, w_imglength * sizeof(unsigned char)); cudaMemcpy(batch_vehicle_other[index_other].data_, snapShotInfo[cur_obj_key].snapShotLittle.frame, w_imglength, cudaMemcpyDeviceToDevice); batch_vehicle_other[index_other].w_ = VEHICLE_WIDTH; batch_vehicle_other[index_other].h_ = VEHICLE_HEIGHT; batch_vehicle_other[index_other].c_ = IMG_CHANNELS; ++index_other; mp_other.push_back(vc_idx); break; } } } cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 if (index_head != 0) { vr_result *vrresult = new vr_result[index_head]{}; VehicleRecognition_Process(vr_handle, batch_vehicle_head, index_head, vrresult); int resIndex = 0; for (auto & item : mp_head) //for (auto iter_key : erase_obj_key) { vr_result &curRes = vresult[item]; memcpy(&vresult[item], &vrresult[resIndex], sizeof(vr_result)); resIndex++; } delete[] vrresult; } if (index_trail != 0) { vr_result *vrresult = new vr_result[index_trail]{}; VehicleRecognition_Process(vr_handle, batch_vehicle_trail, index_trail, vrresult); int resIndex = 0; for (auto & item : mp_trail) //for (auto iter_key : erase_obj_key) { vr_result &curRes = vresult[item]; memcpy(&vresult[item], &vrresult[resIndex], sizeof(vr_result)); resIndex++; } delete[] vrresult; } if (index_other != 0) { vr_result *vrresult = new vr_result[index_other]{}; VehicleRecognition_Process(vr_handle, batch_vehicle_other, index_other, vrresult); int resIndex = 0; for (auto & item : mp_other) //for (auto iter_key : erase_obj_key) { vr_result &curRes = vresult[item]; memcpy(&vresult[item], &vrresult[resIndex], sizeof(vr_result)); resIndex++; } delete[] vrresult; } if (vhd_res != NULL) { delete[] vhd_res; vhd_res = NULL; } for (size_t i = 0; i < batchsize; i++) { if (batch_vehicle_head[i].data_ != NULL) { cudaFree(batch_vehicle_head[i].data_); batch_vehicle_head[i].data_ = NULL; } } delete[] batch_vehicle_head; for (size_t i = 0; i < batchsize; i++) { if (batch_vehicle_trail[i].data_ != NULL) { cudaFree(batch_vehicle_trail[i].data_); batch_vehicle_trail[i].data_ = NULL; } } delete[] batch_vehicle_trail; for (size_t i = 0; i < batchsize; i++) { if (batch_vehicle_other[i].data_ != NULL) { cudaFree(batch_vehicle_other[i].data_); batch_vehicle_other[i].data_ = NULL; } } delete[] batch_vehicle_other; return; } void snapshot_helper::vehicle_recg_analysis() { int count_vehicle_unanalysis = count_vehicle_v.size() - count_vehivle_finishanalysis; //当前数组中还有多少个未进行二次属性分析 if (count_vehicle_unanalysis >= OBJ_BATCH_COUNT_VEHICLE && (vehicle_analysis_cf == SY_CONFIG_OPEN || vcf_recg_cf == SY_CONFIG_OPEN)) { #ifdef LOG_INFO cout << "----- begin VehicleColor_Process -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = count_vehivle_finishanalysis; i < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i - count_vehivle_finishanalysis].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } int index_head = 0; int index_trail = 0; int index_other = 0; map mp_head; map mp_trail; map mp_other; sy_img * batch_vehicle_head = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{}; sy_img * batch_vehicle_trail = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{}; sy_img * batch_vehicle_other = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{}; vhd_result * vhd_res = new vhd_result[OBJ_BATCH_COUNT_VEHICLE]{}; vhd_features_process(vhd_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vhd_res); for (int vc_idx = count_vehivle_finishanalysis; vc_idx < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; vc_idx++) { OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; vhd_result & result = vhd_res[vc_idx]; for (int i = 0; i < result.obj_count_; ++i) { if (result.obj_results_[i].obj_index == 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[i].obj_rect.width_ * result.obj_results_[i].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_head[index_head].data_ == 0) { cudaMalloc(&batch_vehicle_head[index_head].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_head[index_head].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_head[index_head].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_head[index_head].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_head[index_head].c_ = IMG_CHANNELS; ++index_head; mp_head.insert({ vc_idx, cur_obj_key }); break; } else if (result.obj_results_[i].obj_index == 1 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[i].obj_rect.width_ * result.obj_results_[i].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_trail[index_trail].data_ == 0) { cudaMalloc(&batch_vehicle_trail[index_trail].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_trail[index_trail].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_trail[index_trail].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_trail[index_trail].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_trail[index_trail].c_ = IMG_CHANNELS; ++index_trail; mp_trail.insert({ vc_idx, cur_obj_key }); break; } else { printf("***************** real else *********************\n"); int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; cudaMalloc(&batch_vehicle_other[index_other].data_, w_imglength * sizeof(unsigned char)); cudaMemcpy(batch_vehicle_other[index_other].data_, snapShotInfo[cur_obj_key].snapShotLittle.frame, w_imglength, cudaMemcpyDeviceToDevice); batch_vehicle_other[index_other].w_ = VEHICLE_WIDTH; batch_vehicle_other[index_other].h_ = VEHICLE_HEIGHT; batch_vehicle_other[index_other].c_ = IMG_CHANNELS; ++index_other; mp_other.insert({ vc_idx, cur_obj_key }); break; } } } cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 if (vehicle_analysis_cf == SY_CONFIG_OPEN) { if (index_head != 0) { vr_result *vrresult = new vr_result[index_head]{}; VehicleRecognition_Process(vr_handle, batch_vehicle_head, index_head, vrresult); int resIndex = 0; for (auto & item : mp_head) { auto iter = snapShotInfo.find(item.second); if (iter == snapShotInfo.end()) resIndex++; //cout << iter_key.videoID << " " <= OBJ_BATCH_COUNT_VEHICLE && !AttributionAnalysis&& vehicle_analysis_cf == SY_CONFIG_OPEN) { AttributionAnalysis = true; #ifdef LOG_INFO cout << "----- begin VehicleColor_Process -----" << endl; #ifdef _MSC_VER LARGE_INTEGER nFreq, nBeginTime, nEndTime; LARGE_INTEGER nSaveBeginTime, nSaveEndTime; QueryPerformanceFrequency(&nFreq); QueryPerformanceCounter(&nBeginTime); double time_val = 0.0; #else gettimeofday(&t1, NULL); #endif #endif for (int i = count_vehivle_finishanalysis; i < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; i++) { OBJ_KEY cur_obj_key = count_vehicle_v[i]; batch_vehicle[i - count_vehivle_finishanalysis].set_data(VEHICLE_WIDTH, VEHICLE_HEIGHT, IMG_CHANNELS, (unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame); } int index_head = 0; int index_trail = 0; int index_other = 0; map mp_head; map mp_trail; map mp_other; sy_img * batch_vehicle_head = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{}; sy_img * batch_vehicle_trail = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{}; sy_img * batch_vehicle_other = new sy_img[OBJ_BATCH_COUNT_VEHICLE]{}; vhd_result * vhd_res = new vhd_result[OBJ_BATCH_COUNT_VEHICLE]{}; vhd_features_process(vhd_handle, batch_vehicle, OBJ_BATCH_COUNT_VEHICLE, vhd_res); for (int vc_idx = count_vehivle_finishanalysis; vc_idx < count_vehivle_finishanalysis + OBJ_BATCH_COUNT_VEHICLE; vc_idx++) { OBJ_KEY cur_obj_key = count_vehicle_v[vc_idx]; vhd_result & result = vhd_res[vc_idx]; for (int i = 0; i < result.obj_count_; ++i) { if (result.obj_results_[i].obj_index == 0 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[i].obj_rect.width_ * result.obj_results_[i].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_head[index_head].data_ == 0) { cudaMalloc(&batch_vehicle_head[index_head].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_head[index_head].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_head[index_head].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_head[index_head].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_head[index_head].c_ = IMG_CHANNELS; ++index_head; mp_head.insert({ vc_idx, cur_obj_key }); break; } else if (result.obj_results_[i].obj_index == 1 && snapShotInfo[cur_obj_key].snapShotLittle.width == VEHICLE_WIDTH && snapShotInfo[cur_obj_key].snapShotLittle.height == VEHICLE_HEIGHT) { int w_imglength = result.obj_results_[i].obj_rect.width_ * result.obj_results_[i].obj_rect.height_ *IMG_CHANNELS; if (batch_vehicle_trail[index_trail].data_ == 0) { cudaMalloc(&batch_vehicle_trail[index_trail].data_, w_imglength * sizeof(unsigned char)); } cudacommon::CropImgGpu((unsigned char*)snapShotInfo[cur_obj_key].snapShotLittle.frame, VEHICLE_WIDTH, VEHICLE_HEIGHT, batch_vehicle_trail[index_trail].data_, result.obj_results_[i].obj_rect.left_, result.obj_results_[i].obj_rect.top_, result.obj_results_[i].obj_rect.width_, result.obj_results_[i].obj_rect.height_); batch_vehicle_trail[index_trail].w_ = result.obj_results_[i].obj_rect.width_; batch_vehicle_trail[index_trail].h_ = result.obj_results_[i].obj_rect.height_; batch_vehicle_trail[index_trail].c_ = IMG_CHANNELS; ++index_trail; mp_trail.insert({ vc_idx, cur_obj_key }); break; } else { int w_imglength = VEHICLE_WIDTH * VEHICLE_HEIGHT *IMG_CHANNELS; cudaMalloc(&batch_vehicle_other[index_other].data_, w_imglength * sizeof(unsigned char)); cudaMemcpy(batch_vehicle_other[index_other].data_, snapShotInfo[cur_obj_key].snapShotLittle.frame, w_imglength, cudaMemcpyDeviceToDevice); batch_vehicle_other[index_other].w_ = VEHICLE_WIDTH; batch_vehicle_other[index_other].h_ = VEHICLE_HEIGHT; batch_vehicle_other[index_other].c_ = IMG_CHANNELS; ++index_other; mp_other.insert({ vc_idx, cur_obj_key }); break; } } } cudaDeviceSynchronize();//将会一直处于阻塞状态,直到前面所有请求的任务已经被全部执行完毕 if (index_head != 0) { vr_result *vrresult = new vr_result[index_head]{}; VehicleRecognition_Process(vr_handle, batch_vehicle_head, index_head, vrresult); int resIndex = 0; for (auto & item : mp_head) //for (auto iter_key : erase_obj_key) { auto iter = snapShotInfo.find(item.second); if (iter == snapShotInfo.end()) resIndex++; //cout << iter_key.videoID << " " <= 4 && obj_value.index.index <= 6)) && obj_value.snapShotLittle.width == VEHICLE_WIDTH && obj_value.snapShotLittle.height == VEHICLE_HEIGHT) { newVideoObj.analysisRes = new vehicle_result{}; memcpy(newVideoObj.analysisRes, analysisRes, sizeof(vehicle_result)); //此处指针应该是浅拷贝 因此不需要释放两次analysisRes和newVideoObj.analysisRes,最终回调之后释放一次即可 } } //printf("finish copy anaysis. "); { std::lock_guard lock(analysisThreadMutex); finished_analysis_ss_info[obj_key] = newVideoObj; } /*if (m_task_info[obj_key.videoID].obj_callback != NULL) m_task_info[obj_key.videoID].obj_callback(&newVideoObj); if (newVideoObj.analysisRes != NULL) { delete newVideoObj.analysisRes; newVideoObj.analysisRes = NULL; }*/ } DWORD SnapshotThreadProcess(LPVOID param) { snapshot_helper *pThreadParam = (snapshot_helper *)param; try { while (true) { if (!pThreadParam->snapshotImageQueue.empty()) { SNAPSHOT_IMAGE_UNIT cur_image = {}; { std::lock_guard lock(pThreadParam->threadMutex); cur_image = pThreadParam->snapshotImageQueue.front(); cv::resize(cur_image.image, cur_image.image, cv::Size(cur_image.original_size.original_width, cur_image.original_size.original_height)); pThreadParam->snapshotImageQueue.pop(); } cv::imwrite(cur_image.file_name, cur_image.image); if (false == cur_image.is_image) { pThreadParam->finished_save_ss_info.insert(cur_image.obj_key); } } for (auto iter = pThreadParam->finished_save_ss_info.begin(); iter != pThreadParam->finished_save_ss_info.end();) { //cout << iter.videoID << " " << iter.objID << endl; std::lock_guard lock(pThreadParam->analysisThreadMutex); if (pThreadParam->finished_analysis_ss_info.find(*iter) != pThreadParam->finished_analysis_ss_info.end()) { if (pThreadParam->m_task_info[iter->videoID].obj_callback != NULL) pThreadParam->m_task_info[iter->videoID].obj_callback(&pThreadParam->finished_analysis_ss_info[*iter]); { if (pThreadParam->finished_analysis_ss_info[*iter].analysisRes != NULL) { delete pThreadParam->finished_analysis_ss_info[*iter].analysisRes; pThreadParam->finished_analysis_ss_info[*iter].analysisRes = NULL; } pThreadParam->finished_analysis_ss_info.erase(pThreadParam->finished_analysis_ss_info.find(*iter)); } iter = pThreadParam->finished_save_ss_info.erase(iter); } else iter++; } /* for (auto iter : pThreadParam->finished_save_ss_info) { if (pThreadParam->finished_analysis_ss_info.find(iter) != pThreadParam->finished_analysis_ss_info.end()) { if (pThreadParam->m_task_info[iter.videoID].obj_callback != NULL) pThreadParam->m_task_info[iter.videoID].obj_callback(&pThreadParam->finished_analysis_ss_info[iter]); if (pThreadParam->finished_analysis_ss_info[iter].analysisRes != NULL) { delete pThreadParam->finished_analysis_ss_info[iter].analysisRes; pThreadParam->finished_analysis_ss_info[iter].analysisRes = NULL; } pThreadParam->finished_analysis_ss_info.erase(pThreadParam->finished_analysis_ss_info.find(iter)); } } */ boost::this_thread::sleep(boost::posix_time::milliseconds(10)); } } catch (...) { std::cout << "Save Process Thread is Finished" << std::endl; } return 0; }