diff --git a/jni/VehicleNativeInterface.cpp b/jni/VehicleNativeInterface.cpp index ac095e3..bf725a3 100644 --- a/jni/VehicleNativeInterface.cpp +++ b/jni/VehicleNativeInterface.cpp @@ -126,28 +126,22 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI //�㷨���ò��� jclass cls_vehcileAnalysisParam = env->GetObjectClass(vehicleAnalysisParam); - jfieldID fid_dbpath = env->GetFieldID(cls_vehcileAnalysisParam, "dbPath", "Ljava/lang/String;"); - jfieldID fid_modelspath = env->GetFieldID(cls_vehcileAnalysisParam, "modelsPath", "Ljava/lang/String;"); + jfieldID fid_sdk_path = env->GetFieldID(cls_vehcileAnalysisParam, "sdk_path", "Ljava/lang/String;"); jfieldID fid_gpuid = env->GetFieldID(cls_vehcileAnalysisParam, "gpuId", "I"); - jstring str_dbpath = (jstring)env->GetObjectField(vehicleAnalysisParam, fid_dbpath); - const char *dbpath = env->GetStringUTFChars(str_dbpath, JNI_FALSE); - jstring str_modlespath = (jstring)env->GetObjectField(vehicleAnalysisParam, fid_modelspath); - const char *models_path = env->GetStringUTFChars(str_modlespath, JNI_FALSE); + jstring str_sdk_path = (jstring)env->GetObjectField(vehicleAnalysisParam, fid_sdk_path); + const char *sdk_path = env->GetStringUTFChars(str_sdk_path, JNI_FALSE); jint gpuid = env->GetIntField(vehicleAnalysisParam, fid_gpuid); void *vaHandle = NULL; VillageParam param; param.dev_id = gpuid; - param.db_path = dbpath; - param.model_path = models_path; + param.sdk_path = sdk_path; int ret = village_pic_init(&vaHandle, param); - if (ret != SUCCESS) - { + if (ret != SUCCESS) { printf("jni info:va_init failed."); return ret; } - if (ret == SUCCESS) { @@ -166,8 +160,7 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI temp[0] = (jlong)vaHandle; env->SetLongArrayRegion(handle, 0, 1, temp); } - env->ReleaseStringUTFChars(str_dbpath, dbpath); - env->ReleaseStringUTFChars(str_modlespath, models_path); + env->ReleaseStringUTFChars(str_sdk_path, sdk_path); return ret; } @@ -564,7 +557,7 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI for (int k = 0; k < line_info.vec_pt.size(); k++) { auto pt = line_info.vec_pt[k]; - jobject line_info_point = env->NewObject(cls_SyPoint, mid_SyPoint, pt.x_, pt.y_); + jobject line_info_point = env->NewObject(cls_SyPoint, mid_SyPoint, pt.x, pt.y); env->SetObjectArrayElement(ptArray, k, line_info_point); } @@ -584,7 +577,7 @@ JNIEXPORT jint JNICALL Java_com_objecteye_nativeinterface_vehicle_VehicleNativeI for (int k = 0; k < road_info.vec_pt.size(); k++) { auto pt = road_info.vec_pt[k]; - jobject road_info_point = env->NewObject(cls_SyPoint, mid_SyPoint, pt.x_, pt.y_); + jobject road_info_point = env->NewObject(cls_SyPoint, mid_SyPoint, pt.x, pt.y); env->SetObjectArrayElement(ptArray, k, road_info_point); } diff --git a/src/PicAnalysis.cpp b/src/PicAnalysis.cpp index a0c7f66..cf06196 100644 --- a/src/PicAnalysis.cpp +++ b/src/PicAnalysis.cpp @@ -19,7 +19,7 @@ int PicAnalysis::init(VillageParam param) { int ret = SY_FAILED; - ret = m_vehicle_analysis.init(dev_id, 16); + ret = m_vehicle_analysis.init(dev_id, param.sdk_path, 16); if(0 != ret){ return -1; } @@ -27,37 +27,38 @@ int PicAnalysis::init(VillageParam param) { // head_tail_param ht_param; // ht_param.devId = dev_id; // ht_param.max_batch = 16; + // ht_param.sdk_path = param.sdk_path; // ret = m_head_tail_algorithm.init(ht_param); // if(0 != ret){ // return -1; // } - ret = m_clothes_algorithm.init(dev_id); + ret = m_clothes_algorithm.init(dev_id, param.sdk_path); if(0 != ret){ return -1; } - ret = m_human_algorithm.init(dev_id); + ret = m_human_algorithm.init(dev_id, param.sdk_path); if(0 != ret){ return -1; } - ret = m_human_car_algorithm.init(dev_id); + ret = m_human_car_algorithm.init(dev_id, param.sdk_path); if(0 != ret){ return -1; } - ret = m_motor_rainshed_algorithm.init(dev_id); + ret = m_motor_rainshed_algorithm.init(dev_id, param.sdk_path); if(0 != ret){ return -1; } - ret = m_motor_phone_algorithm.init(dev_id); + ret = m_motor_phone_algorithm.init(dev_id, param.sdk_path); if(0 != ret){ return -1; } - ret = m_road_seg_algorithm.init(dev_id); + ret = m_road_seg_algorithm.init(dev_id, param.sdk_path); if(0 != ret){ return -1; } @@ -356,9 +357,9 @@ vector PicAnalysis::analysis_img(vector vec_img){ } //压黄实线 - result_info.cross_line = m_road_seg_algorithm.check_cross_line(road_info.vec_line, vehicle_rect); + result_info.cross_line = m_road_seg_algorithm.check_cross_line(road_info.vec_line, vehicle_rect, src.width, src.height); // 压导流线 - result_info.cross_diversion_line = m_road_seg_algorithm.check_cross_region(road_info.vec_road, vehicle_rect, 3); //3是导流线区域 + result_info.cross_diversion_line = m_road_seg_algorithm.check_cross_region(road_info.vec_road, vehicle_rect, src.width, src.height, 3); //3是导流线区域 } } diff --git a/src/ai_engine_module/HumanAnalysis.cpp b/src/ai_engine_module/HumanAnalysis.cpp index 75ea6c2..e2cb8d6 100644 --- a/src/ai_engine_module/HumanAnalysis.cpp +++ b/src/ai_engine_module/HumanAnalysis.cpp @@ -11,12 +11,14 @@ HumanAnalysis::~HumanAnalysis() release(); } -int HumanAnalysis::init(int devId){ +int HumanAnalysis::init(int devId, std::string sdk_root){ ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); + std::string model_path = sdk_root + "/models/hp/hp220908_310p.om"; + hp_param param; - param.modelNames = "./models/hp/hp220908_310p.om"; + param.modelNames = (char*)model_path.data(); param.devId = devId; cout << "hp_init start " << endl; diff --git a/src/ai_engine_module/HumanAnalysis.h b/src/ai_engine_module/HumanAnalysis.h index c358902..ee7a996 100644 --- a/src/ai_engine_module/HumanAnalysis.h +++ b/src/ai_engine_module/HumanAnalysis.h @@ -14,7 +14,7 @@ public: HumanAnalysis(/* args */); ~HumanAnalysis(); - int init(int devId); + int init(int devId, std::string sdk_root); vector detect(vector vec_img); diff --git a/src/ai_engine_module/HumanCarAnalysis.cpp b/src/ai_engine_module/HumanCarAnalysis.cpp index 89eb814..bc0e8c8 100644 --- a/src/ai_engine_module/HumanCarAnalysis.cpp +++ b/src/ai_engine_module/HumanCarAnalysis.cpp @@ -13,12 +13,14 @@ HumanCarAnalysis::~HumanCarAnalysis() release(); } -int HumanCarAnalysis::init(int devId){ +int HumanCarAnalysis::init(int devId, std::string sdk_root){ ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED); + std::string model_path = sdk_root + "/models/hcp/hcp211008_310p.om"; + hcp_param param; - param.modelNames = "./models/hcp/hcp211008_310p.om"; + param.modelNames = (char*)model_path.data(); param.devId = devId; cout << "hcp_init start " << endl; diff --git a/src/ai_engine_module/HumanCarAnalysis.h b/src/ai_engine_module/HumanCarAnalysis.h index c3c2dd6..38034f1 100644 --- a/src/ai_engine_module/HumanCarAnalysis.h +++ b/src/ai_engine_module/HumanCarAnalysis.h @@ -41,7 +41,7 @@ public: HumanCarAnalysis(/* args */); ~HumanCarAnalysis(); - int init(int devId); + int init(int devId, std::string sdk_root); std::vector detect(vector vec_img); diff --git a/src/ai_engine_module/MotorPhoneAnalysis.cpp b/src/ai_engine_module/MotorPhoneAnalysis.cpp index cb48601..a011287 100644 --- a/src/ai_engine_module/MotorPhoneAnalysis.cpp +++ b/src/ai_engine_module/MotorPhoneAnalysis.cpp @@ -10,11 +10,13 @@ MotorPhoneAnalysis::~MotorPhoneAnalysis() release(); } -int MotorPhoneAnalysis::init(int devId){ +int MotorPhoneAnalysis::init(int devId, std::string sdk_root){ ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); + std::string model_path = sdk_root + "/models/motor_phone/motor_phone1127_310p.om"; + motor_phone_param param; - param.modelNames = "./models/motor_phone/motor_phone1127_310p.om"; + param.modelNames = (char*)model_path.data(); param.thresld = 0.25; param.devId = devId; diff --git a/src/ai_engine_module/MotorPhoneAnalysis.h b/src/ai_engine_module/MotorPhoneAnalysis.h index e93b3ec..f2eb306 100644 --- a/src/ai_engine_module/MotorPhoneAnalysis.h +++ b/src/ai_engine_module/MotorPhoneAnalysis.h @@ -11,7 +11,7 @@ public: MotorPhoneAnalysis(/* args */); ~MotorPhoneAnalysis(); - int init(int devId); + int init(int devId, std::string sdk_root); vector detect(vector vec_img); diff --git a/src/ai_engine_module/MotorRainshedAnalysis.cpp b/src/ai_engine_module/MotorRainshedAnalysis.cpp index 72452a7..0988fbf 100644 --- a/src/ai_engine_module/MotorRainshedAnalysis.cpp +++ b/src/ai_engine_module/MotorRainshedAnalysis.cpp @@ -10,11 +10,13 @@ MotorRainshedAnalysis::~MotorRainshedAnalysis() release(); } -int MotorRainshedAnalysis::init(int devId){ +int MotorRainshedAnalysis::init(int devId, std::string sdk_root){ ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); + std::string model_path = sdk_root + "/models/rainshed/motor_rainshed_231123_310p.om"; + mrc_param param; - param.modelNames = "./models/rainshed/motor_rainshed_231123_310p.om"; + param.modelNames = (char*)model_path.data(); param.thresld = 0.0; param.devId = devId; diff --git a/src/ai_engine_module/MotorRainshedAnalysis.h b/src/ai_engine_module/MotorRainshedAnalysis.h index dd19bee..dec2388 100644 --- a/src/ai_engine_module/MotorRainshedAnalysis.h +++ b/src/ai_engine_module/MotorRainshedAnalysis.h @@ -11,7 +11,7 @@ public: MotorRainshedAnalysis(/* args */); ~MotorRainshedAnalysis(); - int init(int devId); + int init(int devId, std::string sdk_root); vector detect(vector vec_img); diff --git a/src/ai_engine_module/RoadSegAnalysis.cpp b/src/ai_engine_module/RoadSegAnalysis.cpp index 6c751de..40c495d 100644 --- a/src/ai_engine_module/RoadSegAnalysis.cpp +++ b/src/ai_engine_module/RoadSegAnalysis.cpp @@ -12,11 +12,13 @@ RoadSegAnalysis::~RoadSegAnalysis() release(); } -int RoadSegAnalysis::init(int devId){ +int RoadSegAnalysis::init(int devId, std::string sdk_root){ ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); + std::string model_path = sdk_root + "/models/road_seg/tzroad_seg240108_310p.om"; + rs_param param; - param.modelNames = "./models/road_seg/tzroad_seg240108_310p.om"; + param.modelNames = (char*)model_path.data(); param.thresld = 0.25; param.devId = devId; @@ -93,33 +95,20 @@ RoadInfo RoadSegAnalysis::parse_road(rs_result one_result, sy_img src) cv::Mat seg_output = seg_post_process(large_resolution, one_result.seg_array, combined, poly_masks, region_classes, lanes, cats, x_sort); //m_masks:mask前的结果 poly_masks后的结果 for (int i = 0; i < lanes.size(); ++i) { - std::vector points = lanes[i]; LineInfo info; - for (size_t j = 0; j < points.size(); j++) - { - sy_point pt; - pt.x_ = points[j].x; - pt.y_ = points[j].y; - - info.vec_pt.push_back(pt); - } + info.vec_pt = lanes[i]; info.line_type = cats[i]; one_road.vec_line.push_back(info); } for (int i = 0; i < poly_masks.size(); ++i) { - std::vector points = poly_masks[i]; SegInfo seg_info; - for (int j = 0; j < points.size(); ++j) { - sy_point pt; - pt.x_ = points[j].x; - pt.y_ = points[j].y; - - seg_info.vec_pt.push_back(pt); - } + seg_info.vec_pt = poly_masks[i]; seg_info.seg_type = region_classes[i]; one_road.vec_road.push_back(seg_info); } + + return one_road; } std::vector RoadSegAnalysis::parse_direct(rs_result one_result, sy_img src) { @@ -133,21 +122,26 @@ std::vector RoadSegAnalysis::parse_direct(rs_result one_result, sy_img cv::Mat seg_output = seg_post_process(large_resolution, one_result.direct_seg, combined, poly_masks, region_classes, lanes, cats, x_sort); //m_masks:mask前的结果 poly_masks后的结果 + + cv::Mat image(cv::Size(640, 360), CV_8UC3, cv::Scalar(0, 0, 0)); + std::vector vec_road; for (int i = 0; i < poly_masks.size(); ++i) { - std::vector points = poly_masks[i]; SegInfo seg_info; - for (int j = 0; j < points.size(); ++j) { - sy_point pt; - pt.x_ = points[j].x; - pt.y_ = points[j].y; - - seg_info.vec_pt.push_back(pt); - } seg_info.seg_type = region_classes[i]; + seg_info.vec_pt = poly_masks[i]; + + int k = seg_info.seg_type; + const cv::Scalar color(seg_colors[k][0], seg_colors[k][1], seg_colors[k][2]); + + polylines(image, seg_info.vec_pt, true, color, 3, cv::LINE_AA); + vec_road.push_back(seg_info); } + cv::imwrite("./direct.jpg", image); + image.release(); + return vec_road; } @@ -202,17 +196,15 @@ int RoadSegAnalysis::check_reverse_driving(std::vector& vec_direct, sy_ for (size_t i = 0; i < vec_direct.size(); i++) { SegInfo& region = vec_direct[i]; if (region.seg_type == 1) { - std::vector vec_pt; - for (size_t j = 0; j < region.vec_pt.size(); j++) { - double dist = pointPolygonTest(polygon_pts, cv::Point2f(region.vec_pt[j].x_, region.vec_pt[j].y_), false); + for (size_t j = 0; j < polygon_pts.size(); j++) { + double dist = pointPolygonTest(region.vec_pt, polygon_pts[j], false); if (dist > 0) { coming_count ++; } } } else if (region.seg_type == 2) { - std::vector vec_pt; - for (size_t j = 0; j < region.vec_pt.size(); j++) { - double dist = pointPolygonTest(polygon_pts, cv::Point2f(region.vec_pt[j].x_, region.vec_pt[j].y_), false); + for (size_t j = 0; j < polygon_pts.size(); j++) { + double dist = pointPolygonTest(region.vec_pt, polygon_pts[j], false); if (dist > 0) { leaving_count ++; } @@ -259,10 +251,10 @@ int RoadSegAnalysis::check_reverse_driving(std::vector& vec_direct, sy_ return -1; } -int RoadSegAnalysis::check_cross_line(std::vector& vec_line, sy_rect src_rc) { +int RoadSegAnalysis::check_cross_line(std::vector& vec_line, sy_rect src_rc, int src_width, int src_height) { - float scale_w = 640.0 / src_rc.width_; - float scale_h = 360.0 / src_rc.height_; + float scale_w = 640.0 / src_width; + float scale_h = 360.0 / src_height; sy_rect rc; rc.left_ = src_rc.left_ * scale_w; @@ -294,9 +286,8 @@ int RoadSegAnalysis::check_cross_line(std::vector& vec_line, sy_rect s if (line.line_type == 1 || line.line_type == 2) { // 黄实线 int in_count = 0; - std::vector vec_pt; for (size_t j = 0; j < line.vec_pt.size(); j++) { - double dist = pointPolygonTest(polygon_pts, cv::Point2f(line.vec_pt[j].x_, line.vec_pt[j].y_), false); + double dist = pointPolygonTest(polygon_pts, line.vec_pt[j], false); if (dist > 0) { in_count ++; } @@ -312,7 +303,16 @@ int RoadSegAnalysis::check_cross_line(std::vector& vec_line, sy_rect s return -1; } -int RoadSegAnalysis::check_cross_region(std::vector& vec_reg, sy_rect rc, int region_type) { +int RoadSegAnalysis::check_cross_region(std::vector& vec_reg, sy_rect src_rc, int src_width, int src_height, int region_type) { + + float scale_w = 640.0 / src_width; + float scale_h = 360.0 / src_height; + + sy_rect rc; + rc.left_ = src_rc.left_ * scale_w; + rc.width_ = src_rc.width_ * scale_w; + rc.top_ = src_rc.top_ * scale_h; + rc.height_ = src_rc.height_ * scale_h; std::vector polygon_pts; cv::Point pt_lt; @@ -336,11 +336,11 @@ int RoadSegAnalysis::check_cross_region(std::vector& vec_reg, sy_rect r for (size_t i = 0; i < vec_reg.size(); i++) { SegInfo& seg = vec_reg[i]; if (seg.seg_type == region_type) { - // 黄实线 + int in_count = 0; - std::vector vec_pt; + // 车辆与region相交 for (size_t j = 0; j < seg.vec_pt.size(); j++) { - double dist = pointPolygonTest(polygon_pts, cv::Point2f(seg.vec_pt[j].x_, seg.vec_pt[j].y_), false); + double dist = pointPolygonTest(polygon_pts, seg.vec_pt[j], false); if (dist > 0) { in_count ++; } @@ -350,76 +350,20 @@ int RoadSegAnalysis::check_cross_region(std::vector& vec_reg, sy_rect r {//有5个点就认为是压线了 return 1; } + + // 车辆与region相交情形未检测出来,检测车辆在region中情形 + for (size_t j = 0; j < polygon_pts.size(); j++) { + double dist = pointPolygonTest(seg.vec_pt, polygon_pts[j], false); + if (dist > 0) { + return 1; + } + } } } return -1; } -// cv::Mat imshow_lanes(cv::Mat img, const rs_lane* lanes, int lane_count) { -// float scale_w = img.cols / 640.0; -// float scale_h = img.rows / 360.0; -// std::vector, int>> combined; -// lanes_process(lanes, lane_count, combined, scale_w, scale_h); - -// for (const auto& lane_info : combined) { -// const auto& xys = lane_info.first; -// int cls = lane_info.second; -// cv::Scalar color(lane_colors[cls][0],lane_colors[cls][1],lane_colors[cls][2]); -// for (size_t i = 1; i < xys.size(); ++i) { -// cv::line(img, xys[i - 1], xys[i], color, 4); -// } -// } - -// return img; -// } - -// void RoadSegAnalysis::post_direct(rs_result one_result, sy_img src){ -// int src_width = src.w_; -// int src_height = src.h_; -// int w = 640; -// int h = 360; -// float alpha = 0.75; -// cv::Mat overlayed_direct_img(cv::Size(w,h), CV_8UC3, cv::Scalar(0, 0, 0)); -// // 将车道线标签转换为彩色图像 -// for (int i = 0; i < h; ++i) { -// for (int j = 0; j < w; ++j) { -// int idx = one_result.direct_seg[(i * w + j)]; -// overlayed_direct_img.at(i, j)[0] = seg_colors[idx][0]; // R通道; -// overlayed_direct_img.at(i, j)[1] = seg_colors[idx][1]; // G通道 -// overlayed_direct_img.at(i, j)[2] = seg_colors[idx][2]; // B通道 -// } -// } -// cv::resize(overlayed_direct_img, overlayed_direct_img, cv::Size(src_width,src_height), 0, 0, cv::INTER_LINEAR); -// // 将原始图像和彩色车道线图进行混合 -// // cv::addWeighted(cvImg, alpha, overlayed_direct_img, 1 - alpha, 0, overlayed_direct_img); -// cv::Mat img_direct_lane = imshow_lanes(overlayed_direct_img, one_result.reg_array, one_result.lane_count); -// cv::imwrite("img_direct_lane.jpg", img_direct_lane); - - -// std::vector, int>> combined; -// lanes_process(one_result.reg_array, one_result.lane_count, combined); -// std::vector> poly_masks, lanes, direct_masks, merge_masks; -// std::vector region_classes, cats, direct_classes, merge_classes; -// cv::Mat background_mask; -// std::map x_sort; -// bool large_resolution = false; -// if (src_height > 1920) large_resolution = true; -// cv::Mat seg_output = seg_post_process(large_resolution, one_result.seg_array, combined, poly_masks, region_classes, lanes, cats, x_sort, background_mask); //m_masks:mask前的结果 poly_masks后的结果 -// cv::Mat direct_output = direct_post_process(large_resolution, one_result.direct_seg, direct_masks, direct_classes); -// cv::Mat merge_output = merge_direct_process(large_resolution, one_result.direct_seg, merge_masks, merge_classes); - -// cv::Mat image(cv::Size(w,h), CV_8UC3, cv::Scalar(0, 0, 0)); -// cv::Mat vis_image = mask_to_rgb(image, seg_output); -// cv::Mat direct_image = mask_to_rgb(image, direct_output); -// cv::Mat merge_image = mask_to_rgb(image, merge_output); -// cv::imwrite("vis_image.jpg", vis_image); -// cv::imwrite("direct_image.jpg", direct_image); -// cv::imwrite("merge_image.jpg", merge_image); -// cv::imwrite("background_mask.jpg", background_mask); -// } - - cv::Mat RoadSegAnalysis::mask_to_rgb(cv::Mat img, cv::Mat mask) { cv::Mat masks = img.clone(); int reg_cls = 8; @@ -612,10 +556,6 @@ cv::Mat RoadSegAnalysis::seg_post_process(bool large_resolution, unsigned char * if (x_sort.count(centr_x)) centr_x += 0.0001; x_sort.insert(std::make_pair(centr_x, count)); ++ count; - // for (auto iter: contours_poly) { - // std::cout << "contour " << iter << " " << cat << std::endl; - // } - } } } diff --git a/src/ai_engine_module/RoadSegAnalysis.h b/src/ai_engine_module/RoadSegAnalysis.h index 20dda80..7f5ae43 100644 --- a/src/ai_engine_module/RoadSegAnalysis.h +++ b/src/ai_engine_module/RoadSegAnalysis.h @@ -18,15 +18,15 @@ public: RoadSegAnalysis(/* args */); ~RoadSegAnalysis(); - int init(int devId); + int init(int devId, std::string sdk_root); std::vector detect(vector vec_img); int check_reverse_driving(std::vector& vec_direct, sy_rect rc, int src_width, int src_height, int head_or_tail); - int check_cross_line(std::vector& vec_line, sy_rect rc); + int check_cross_line(std::vector& vec_line, sy_rect rc, int src_width, int src_height); - int check_cross_region(std::vector& vec_reg, sy_rect rc, int region_type); + int check_cross_region(std::vector& vec_reg, sy_rect rc, int src_width, int src_height, int region_type); private: int release(); diff --git a/src/ai_engine_module/VehicleAnalysis.cpp b/src/ai_engine_module/VehicleAnalysis.cpp index 2f67e73..9f760a1 100644 --- a/src/ai_engine_module/VehicleAnalysis.cpp +++ b/src/ai_engine_module/VehicleAnalysis.cpp @@ -9,7 +9,7 @@ VehicleAnalysis::~VehicleAnalysis() { release(); } -int VehicleAnalysis::init(int devId, int max_batch_size) { +int VehicleAnalysis::init(int devId, std::string sdk_root, int max_batch_size) { param.vehicle_detect_config= SY_CONFIG_OPEN; //1.开启车检测 SY_CONFIG_CLOSE SY_CONFIG_OPEN param.vehicle_recg_config= SY_CONFIG_OPEN; //4.开启车型识别 @@ -52,9 +52,11 @@ int VehicleAnalysis::init(int devId, int max_batch_size) { //车颜色阈值 param.vc_thresld = 0.5; + string dbPath = sdk_root + "/models/vehicle_analysis/db/vr_h0725x210605_r191230.db"; + string models_path = sdk_root + "/models/vehicle_analysis/"; //车型参数 - param.dbPath="./models/vehicle_analysis/db/vr_h0725x210605_r191230.db"; - param.models_Path="./models/vehicle_analysis/"; //所有模型的地址 + param.dbPath= (char*)dbPath.data(); + param.models_Path= (char*)models_path.data(); //所有模型的地址 cout << "va_init start " << endl; // 内部有 ctx diff --git a/src/ai_engine_module/VehicleAnalysis.h b/src/ai_engine_module/VehicleAnalysis.h index cdf26f5..2d1a98e 100644 --- a/src/ai_engine_module/VehicleAnalysis.h +++ b/src/ai_engine_module/VehicleAnalysis.h @@ -38,7 +38,7 @@ public: VehicleAnalysis(/* args */); ~VehicleAnalysis(); - int init(int devId, int max_batch_size); + int init(int devId, std::string sdk_root, int max_batch_size); va_result* detect(vector vec_img); diff --git a/src/ai_engine_module/VehicleHeadTail.cpp b/src/ai_engine_module/VehicleHeadTail.cpp index 74416a6..0d5d2d9 100644 --- a/src/ai_engine_module/VehicleHeadTail.cpp +++ b/src/ai_engine_module/VehicleHeadTail.cpp @@ -17,8 +17,10 @@ int VehicleHeadTail::init(head_tail_param param){ LOG_INFO("head_tail version: head_tail_256.2024.12.18"); + string model_path = param.sdk_root + "/models/car_head_tail/head_tail_256_241220_310P.om"; + m_cnn_cls = new CnnCls(); - int ret = m_cnn_cls->Init("./models/car_head_tail/head_tail_256_241220_310P.om"); + int ret = m_cnn_cls->Init(model_path.c_str()); if (ret != SY_SUCCESS) { delete m_cnn_cls; m_cnn_cls = nullptr; diff --git a/src/ai_engine_module/VehicleHeadTail.h b/src/ai_engine_module/VehicleHeadTail.h index d329622..9dceccb 100644 --- a/src/ai_engine_module/VehicleHeadTail.h +++ b/src/ai_engine_module/VehicleHeadTail.h @@ -14,6 +14,7 @@ typedef struct head_tail_param { int devId; //ָ指定显卡id int max_batch; + std::string sdk_root; head_tail_param() :devId(0), max_batch(8){}; } head_tail_param; diff --git a/src/ai_engine_module/VidClothes.cpp b/src/ai_engine_module/VidClothes.cpp index 574eddf..8984b0e 100644 --- a/src/ai_engine_module/VidClothes.cpp +++ b/src/ai_engine_module/VidClothes.cpp @@ -9,11 +9,13 @@ VidClothes::~VidClothes() release(); } -int VidClothes::init(int devId){ +int VidClothes::init(int devId, std::string sdk_root){ ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED); + std::string model_path = sdk_root + "/models/vid_clothes/vidClothes0325_310P.om"; + vidclothes_param param; - param.modelNames = "./models/vid_clothes/vidClothes0325_310P.om"; + param.modelNames = (char*)model_path.data(); param.thresld = 0.0; param.devId = devId; diff --git a/src/ai_engine_module/VidClothes.h b/src/ai_engine_module/VidClothes.h index 5a4f1de..ea8b41e 100644 --- a/src/ai_engine_module/VidClothes.h +++ b/src/ai_engine_module/VidClothes.h @@ -7,7 +7,7 @@ public: VidClothes(/* args */); ~VidClothes(); - int init(int devId); + int init(int devId, std::string sdk_root); vector detect(vector vec_img); diff --git a/src/ai_engine_module/va_test.cpp0 b/src/ai_engine_module/va_test.cpp0 deleted file mode 100644 index 11a398e..0000000 --- a/src/ai_engine_module/va_test.cpp0 +++ /dev/null @@ -1,864 +0,0 @@ -#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:" < vec_path; diff --git a/src/village_inc.h b/src/village_inc.h index 0785d34..4d9af79 100644 --- a/src/village_inc.h +++ b/src/village_inc.h @@ -5,10 +5,13 @@ #include #include "vehicle_analysis.h" +#include "opencv2/opencv.hpp" +#include "opencv2/imgcodecs/legacy/constants_c.h" +#include "opencv2/imgproc/types_c.h" + struct VillageParam { int dev_id; - std::string db_path; - std::string model_path; + std::string sdk_path; }; typedef struct pendant_info @@ -57,12 +60,12 @@ typedef struct VehicleInfo { struct LineInfo{ int line_type; - std::vector vec_pt; + std::vector vec_pt; }; struct SegInfo { int seg_type; - std::vector vec_pt; + std::vector vec_pt; }; typedef struct AnalysisResult {