diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/src/main/cpp/yolo.cpp b/app/src/main/cpp/yolo.cpp index 02ff8d7..2eadf0d 100644 --- a/app/src/main/cpp/yolo.cpp +++ b/app/src/main/cpp/yolo.cpp @@ -348,6 +348,39 @@ /***模型分割*************/ +//目标类别 +static const char *class_names[] = { + "三脚架", "三通", "人", "切断阀", "危险告知牌", "压力测试仪", "压力表", "反光衣", + "呼吸面罩", "喉箍", "圆头水枪", "安全告知牌", "安全帽", "安全标识", "安全绳", "对讲机", + "尖头水枪", "开关", "报警装置", "接头", "施工路牌", "气体检测仪", "水带", "水带_矩形", + "流量计", "消火栓箱", "灭火器", "照明设备", "熄火保护", "电线暴露", "电路图", "警戒线", + "调压器", "调长器", "贴纸", "跨电线", "路锥", "软管", "过滤器", "配电箱", "长柄阀门", + "阀门", "风管" +}; + +//目标颜色数组 +static const unsigned char colors[19][3] = { + {54, 67, 244}, + {99, 30, 233}, + {176, 39, 156}, + {183, 58, 103}, + {181, 81, 63}, + {243, 150, 33}, + {244, 169, 3}, + {212, 188, 0}, + {136, 150, 0}, + {80, 175, 76}, + {74, 195, 139}, + {57, 220, 205}, + {59, 235, 255}, + {7, 193, 255}, + {0, 152, 255}, + {34, 87, 255}, + {72, 85, 121}, + {158, 158, 158}, + {139, 125, 96} +}; + Yolo::Yolo() { blob_pool_allocator.set_size_compare_ratio(0.f); workspace_pool_allocator.set_size_compare_ratio(0.f); @@ -807,20 +840,11 @@ //初始化ArrayList对象 jobject arraylist_obj = env->NewObject(list_clazz, arraylist_init); for (const auto &item: objects) { - auto rect = item.rect; - - float array[5]; - array[0] = rect.x; - array[1] = rect.y; - array[2] = rect.x + rect.width; - array[3] = rect.y + rect.height; - array[4] = (float) item.label; - - jfloatArray result_array = env->NewFloatArray(5); - env->SetFloatArrayRegion(result_array, 0, 5, array); + char index[16]; + sprintf(index, "%d", item.label); //add - env->CallBooleanMethod(arraylist_obj, arraylist_add, result_array); + env->CallBooleanMethod(arraylist_obj, arraylist_add, env->NewStringUTF(index)); } //回调 env->CallVoidMethod(j_callback, j_method_id, arraylist_obj); @@ -842,45 +866,6 @@ } int Yolo::draw(cv::Mat &rgb, const std::vector &objects) { - static const char *class_names[] = { - "tripod", "tee", "person", - "shut-off valve", "hazard signs", "pressure tester", - "pressure gauge", "reflective clothing", "respirator masks", - "throat foil", "round-headed water gun", "safety signs", - "helmet", "security identification", "safety ropes", - "intercom", "pointed water gun", "switch", - "alarm device", "joint", "construction street signs", - "gas detectors", "hoses", "hose_rectangle", - "flow-meter", "fire hydrant box", "fire extinguisher", - "lighting equipment", "flame-out protection", "exposed wires", - "circuit diagram", "cordon", "regulator", - "length adjuster", "stickers", "across wires", - "road cones", "hose", "filter", - "distribution box", "long-shank valves", "valve", "ducts" - }; - - static const unsigned char colors[19][3] = { - {54, 67, 244}, - {99, 30, 233}, - {176, 39, 156}, - {183, 58, 103}, - {181, 81, 63}, - {243, 150, 33}, - {244, 169, 3}, - {212, 188, 0}, - {136, 150, 0}, - {80, 175, 76}, - {74, 195, 139}, - {57, 220, 205}, - {59, 235, 255}, - {7, 193, 255}, - {0, 152, 255}, - {34, 87, 255}, - {72, 85, 121}, - {158, 158, 158}, - {139, 125, 96} - }; - int color_index = 0; for (const auto &obj: objects) { @@ -892,8 +877,8 @@ cv::rectangle(rgb, obj.rect, cc, 2); - char text[256]; - sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); + char text[64]; + sprintf(text, "%s", class_names[obj.label]); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, @@ -906,21 +891,15 @@ if (x + label_size.width > rgb.cols) x = rgb.cols - label_size.width; - cv::Size size = cv::Size(label_size.width, label_size.height + baseLine); - cv::Rect rc = cv::Rect(cv::Point(x, y), size); + cv::Point org = cv::Point(x, y + label_size.height); + + cv::Rect rc = cv::getTextSize(rgb.size(), text, org, chinese_font, 8); cv::rectangle(rgb, rc, cc, -1); cv::Scalar text_scalar = (color[0] + color[1] + color[2] >= 381) ? cv::Scalar(0, 0, 0) : cv::Scalar(255, 255, 255); - - - cv::putText(rgb, text, - cv::Point(x, y + label_size.height), - cv::FONT_HERSHEY_SIMPLEX, - 0.5, - text_scalar, 1 - ); + cv::putText(rgb, text, org, text_scalar, chinese_font, 8); } return 0; } diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/src/main/cpp/yolo.cpp b/app/src/main/cpp/yolo.cpp index 02ff8d7..2eadf0d 100644 --- a/app/src/main/cpp/yolo.cpp +++ b/app/src/main/cpp/yolo.cpp @@ -348,6 +348,39 @@ /***模型分割*************/ +//目标类别 +static const char *class_names[] = { + "三脚架", "三通", "人", "切断阀", "危险告知牌", "压力测试仪", "压力表", "反光衣", + "呼吸面罩", "喉箍", "圆头水枪", "安全告知牌", "安全帽", "安全标识", "安全绳", "对讲机", + "尖头水枪", "开关", "报警装置", "接头", "施工路牌", "气体检测仪", "水带", "水带_矩形", + "流量计", "消火栓箱", "灭火器", "照明设备", "熄火保护", "电线暴露", "电路图", "警戒线", + "调压器", "调长器", "贴纸", "跨电线", "路锥", "软管", "过滤器", "配电箱", "长柄阀门", + "阀门", "风管" +}; + +//目标颜色数组 +static const unsigned char colors[19][3] = { + {54, 67, 244}, + {99, 30, 233}, + {176, 39, 156}, + {183, 58, 103}, + {181, 81, 63}, + {243, 150, 33}, + {244, 169, 3}, + {212, 188, 0}, + {136, 150, 0}, + {80, 175, 76}, + {74, 195, 139}, + {57, 220, 205}, + {59, 235, 255}, + {7, 193, 255}, + {0, 152, 255}, + {34, 87, 255}, + {72, 85, 121}, + {158, 158, 158}, + {139, 125, 96} +}; + Yolo::Yolo() { blob_pool_allocator.set_size_compare_ratio(0.f); workspace_pool_allocator.set_size_compare_ratio(0.f); @@ -807,20 +840,11 @@ //初始化ArrayList对象 jobject arraylist_obj = env->NewObject(list_clazz, arraylist_init); for (const auto &item: objects) { - auto rect = item.rect; - - float array[5]; - array[0] = rect.x; - array[1] = rect.y; - array[2] = rect.x + rect.width; - array[3] = rect.y + rect.height; - array[4] = (float) item.label; - - jfloatArray result_array = env->NewFloatArray(5); - env->SetFloatArrayRegion(result_array, 0, 5, array); + char index[16]; + sprintf(index, "%d", item.label); //add - env->CallBooleanMethod(arraylist_obj, arraylist_add, result_array); + env->CallBooleanMethod(arraylist_obj, arraylist_add, env->NewStringUTF(index)); } //回调 env->CallVoidMethod(j_callback, j_method_id, arraylist_obj); @@ -842,45 +866,6 @@ } int Yolo::draw(cv::Mat &rgb, const std::vector &objects) { - static const char *class_names[] = { - "tripod", "tee", "person", - "shut-off valve", "hazard signs", "pressure tester", - "pressure gauge", "reflective clothing", "respirator masks", - "throat foil", "round-headed water gun", "safety signs", - "helmet", "security identification", "safety ropes", - "intercom", "pointed water gun", "switch", - "alarm device", "joint", "construction street signs", - "gas detectors", "hoses", "hose_rectangle", - "flow-meter", "fire hydrant box", "fire extinguisher", - "lighting equipment", "flame-out protection", "exposed wires", - "circuit diagram", "cordon", "regulator", - "length adjuster", "stickers", "across wires", - "road cones", "hose", "filter", - "distribution box", "long-shank valves", "valve", "ducts" - }; - - static const unsigned char colors[19][3] = { - {54, 67, 244}, - {99, 30, 233}, - {176, 39, 156}, - {183, 58, 103}, - {181, 81, 63}, - {243, 150, 33}, - {244, 169, 3}, - {212, 188, 0}, - {136, 150, 0}, - {80, 175, 76}, - {74, 195, 139}, - {57, 220, 205}, - {59, 235, 255}, - {7, 193, 255}, - {0, 152, 255}, - {34, 87, 255}, - {72, 85, 121}, - {158, 158, 158}, - {139, 125, 96} - }; - int color_index = 0; for (const auto &obj: objects) { @@ -892,8 +877,8 @@ cv::rectangle(rgb, obj.rect, cc, 2); - char text[256]; - sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); + char text[64]; + sprintf(text, "%s", class_names[obj.label]); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, @@ -906,21 +891,15 @@ if (x + label_size.width > rgb.cols) x = rgb.cols - label_size.width; - cv::Size size = cv::Size(label_size.width, label_size.height + baseLine); - cv::Rect rc = cv::Rect(cv::Point(x, y), size); + cv::Point org = cv::Point(x, y + label_size.height); + + cv::Rect rc = cv::getTextSize(rgb.size(), text, org, chinese_font, 8); cv::rectangle(rgb, rc, cc, -1); cv::Scalar text_scalar = (color[0] + color[1] + color[2] >= 381) ? cv::Scalar(0, 0, 0) : cv::Scalar(255, 255, 255); - - - cv::putText(rgb, text, - cv::Point(x, y + label_size.height), - cv::FONT_HERSHEY_SIMPLEX, - 0.5, - text_scalar, 1 - ); + cv::putText(rgb, text, org, text_scalar, chinese_font, 8); } return 0; } diff --git a/app/src/main/cpp/yolo.h b/app/src/main/cpp/yolo.h index 1cbd0e5..4e040c5 100644 --- a/app/src/main/cpp/yolo.h +++ b/app/src/main/cpp/yolo.h @@ -16,7 +16,7 @@ #define YOLO_H #include - +#include "myfontface.h" #include struct Object { @@ -91,6 +91,9 @@ ncnn::UnlockedPoolAllocator blob_pool_allocator; ncnn::PoolAllocator workspace_pool_allocator; + //中文渲染字体 + MyFontFace chinese_font; + /** * 全局引用 * */ diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/src/main/cpp/yolo.cpp b/app/src/main/cpp/yolo.cpp index 02ff8d7..2eadf0d 100644 --- a/app/src/main/cpp/yolo.cpp +++ b/app/src/main/cpp/yolo.cpp @@ -348,6 +348,39 @@ /***模型分割*************/ +//目标类别 +static const char *class_names[] = { + "三脚架", "三通", "人", "切断阀", "危险告知牌", "压力测试仪", "压力表", "反光衣", + "呼吸面罩", "喉箍", "圆头水枪", "安全告知牌", "安全帽", "安全标识", "安全绳", "对讲机", + "尖头水枪", "开关", "报警装置", "接头", "施工路牌", "气体检测仪", "水带", "水带_矩形", + "流量计", "消火栓箱", "灭火器", "照明设备", "熄火保护", "电线暴露", "电路图", "警戒线", + "调压器", "调长器", "贴纸", "跨电线", "路锥", "软管", "过滤器", "配电箱", "长柄阀门", + "阀门", "风管" +}; + +//目标颜色数组 +static const unsigned char colors[19][3] = { + {54, 67, 244}, + {99, 30, 233}, + {176, 39, 156}, + {183, 58, 103}, + {181, 81, 63}, + {243, 150, 33}, + {244, 169, 3}, + {212, 188, 0}, + {136, 150, 0}, + {80, 175, 76}, + {74, 195, 139}, + {57, 220, 205}, + {59, 235, 255}, + {7, 193, 255}, + {0, 152, 255}, + {34, 87, 255}, + {72, 85, 121}, + {158, 158, 158}, + {139, 125, 96} +}; + Yolo::Yolo() { blob_pool_allocator.set_size_compare_ratio(0.f); workspace_pool_allocator.set_size_compare_ratio(0.f); @@ -807,20 +840,11 @@ //初始化ArrayList对象 jobject arraylist_obj = env->NewObject(list_clazz, arraylist_init); for (const auto &item: objects) { - auto rect = item.rect; - - float array[5]; - array[0] = rect.x; - array[1] = rect.y; - array[2] = rect.x + rect.width; - array[3] = rect.y + rect.height; - array[4] = (float) item.label; - - jfloatArray result_array = env->NewFloatArray(5); - env->SetFloatArrayRegion(result_array, 0, 5, array); + char index[16]; + sprintf(index, "%d", item.label); //add - env->CallBooleanMethod(arraylist_obj, arraylist_add, result_array); + env->CallBooleanMethod(arraylist_obj, arraylist_add, env->NewStringUTF(index)); } //回调 env->CallVoidMethod(j_callback, j_method_id, arraylist_obj); @@ -842,45 +866,6 @@ } int Yolo::draw(cv::Mat &rgb, const std::vector &objects) { - static const char *class_names[] = { - "tripod", "tee", "person", - "shut-off valve", "hazard signs", "pressure tester", - "pressure gauge", "reflective clothing", "respirator masks", - "throat foil", "round-headed water gun", "safety signs", - "helmet", "security identification", "safety ropes", - "intercom", "pointed water gun", "switch", - "alarm device", "joint", "construction street signs", - "gas detectors", "hoses", "hose_rectangle", - "flow-meter", "fire hydrant box", "fire extinguisher", - "lighting equipment", "flame-out protection", "exposed wires", - "circuit diagram", "cordon", "regulator", - "length adjuster", "stickers", "across wires", - "road cones", "hose", "filter", - "distribution box", "long-shank valves", "valve", "ducts" - }; - - static const unsigned char colors[19][3] = { - {54, 67, 244}, - {99, 30, 233}, - {176, 39, 156}, - {183, 58, 103}, - {181, 81, 63}, - {243, 150, 33}, - {244, 169, 3}, - {212, 188, 0}, - {136, 150, 0}, - {80, 175, 76}, - {74, 195, 139}, - {57, 220, 205}, - {59, 235, 255}, - {7, 193, 255}, - {0, 152, 255}, - {34, 87, 255}, - {72, 85, 121}, - {158, 158, 158}, - {139, 125, 96} - }; - int color_index = 0; for (const auto &obj: objects) { @@ -892,8 +877,8 @@ cv::rectangle(rgb, obj.rect, cc, 2); - char text[256]; - sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); + char text[64]; + sprintf(text, "%s", class_names[obj.label]); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, @@ -906,21 +891,15 @@ if (x + label_size.width > rgb.cols) x = rgb.cols - label_size.width; - cv::Size size = cv::Size(label_size.width, label_size.height + baseLine); - cv::Rect rc = cv::Rect(cv::Point(x, y), size); + cv::Point org = cv::Point(x, y + label_size.height); + + cv::Rect rc = cv::getTextSize(rgb.size(), text, org, chinese_font, 8); cv::rectangle(rgb, rc, cc, -1); cv::Scalar text_scalar = (color[0] + color[1] + color[2] >= 381) ? cv::Scalar(0, 0, 0) : cv::Scalar(255, 255, 255); - - - cv::putText(rgb, text, - cv::Point(x, y + label_size.height), - cv::FONT_HERSHEY_SIMPLEX, - 0.5, - text_scalar, 1 - ); + cv::putText(rgb, text, org, text_scalar, chinese_font, 8); } return 0; } diff --git a/app/src/main/cpp/yolo.h b/app/src/main/cpp/yolo.h index 1cbd0e5..4e040c5 100644 --- a/app/src/main/cpp/yolo.h +++ b/app/src/main/cpp/yolo.h @@ -16,7 +16,7 @@ #define YOLO_H #include - +#include "myfontface.h" #include struct Object { @@ -91,6 +91,9 @@ ncnn::UnlockedPoolAllocator blob_pool_allocator; ncnn::PoolAllocator workspace_pool_allocator; + //中文渲染字体 + MyFontFace chinese_font; + /** * 全局引用 * */ diff --git a/app/src/main/cpp/yolov8ncnn.cpp b/app/src/main/cpp/yolov8ncnn.cpp index e6a48e7..2a2b977 100644 --- a/app/src/main/cpp/yolov8ncnn.cpp +++ b/app/src/main/cpp/yolov8ncnn.cpp @@ -132,6 +132,9 @@ //检测 g_yolo->detect(rgb, objects); + + //绘制结果 + g_yolo->draw(rgb, objects); } else { draw_unsupported(rgb); } diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/src/main/cpp/yolo.cpp b/app/src/main/cpp/yolo.cpp index 02ff8d7..2eadf0d 100644 --- a/app/src/main/cpp/yolo.cpp +++ b/app/src/main/cpp/yolo.cpp @@ -348,6 +348,39 @@ /***模型分割*************/ +//目标类别 +static const char *class_names[] = { + "三脚架", "三通", "人", "切断阀", "危险告知牌", "压力测试仪", "压力表", "反光衣", + "呼吸面罩", "喉箍", "圆头水枪", "安全告知牌", "安全帽", "安全标识", "安全绳", "对讲机", + "尖头水枪", "开关", "报警装置", "接头", "施工路牌", "气体检测仪", "水带", "水带_矩形", + "流量计", "消火栓箱", "灭火器", "照明设备", "熄火保护", "电线暴露", "电路图", "警戒线", + "调压器", "调长器", "贴纸", "跨电线", "路锥", "软管", "过滤器", "配电箱", "长柄阀门", + "阀门", "风管" +}; + +//目标颜色数组 +static const unsigned char colors[19][3] = { + {54, 67, 244}, + {99, 30, 233}, + {176, 39, 156}, + {183, 58, 103}, + {181, 81, 63}, + {243, 150, 33}, + {244, 169, 3}, + {212, 188, 0}, + {136, 150, 0}, + {80, 175, 76}, + {74, 195, 139}, + {57, 220, 205}, + {59, 235, 255}, + {7, 193, 255}, + {0, 152, 255}, + {34, 87, 255}, + {72, 85, 121}, + {158, 158, 158}, + {139, 125, 96} +}; + Yolo::Yolo() { blob_pool_allocator.set_size_compare_ratio(0.f); workspace_pool_allocator.set_size_compare_ratio(0.f); @@ -807,20 +840,11 @@ //初始化ArrayList对象 jobject arraylist_obj = env->NewObject(list_clazz, arraylist_init); for (const auto &item: objects) { - auto rect = item.rect; - - float array[5]; - array[0] = rect.x; - array[1] = rect.y; - array[2] = rect.x + rect.width; - array[3] = rect.y + rect.height; - array[4] = (float) item.label; - - jfloatArray result_array = env->NewFloatArray(5); - env->SetFloatArrayRegion(result_array, 0, 5, array); + char index[16]; + sprintf(index, "%d", item.label); //add - env->CallBooleanMethod(arraylist_obj, arraylist_add, result_array); + env->CallBooleanMethod(arraylist_obj, arraylist_add, env->NewStringUTF(index)); } //回调 env->CallVoidMethod(j_callback, j_method_id, arraylist_obj); @@ -842,45 +866,6 @@ } int Yolo::draw(cv::Mat &rgb, const std::vector &objects) { - static const char *class_names[] = { - "tripod", "tee", "person", - "shut-off valve", "hazard signs", "pressure tester", - "pressure gauge", "reflective clothing", "respirator masks", - "throat foil", "round-headed water gun", "safety signs", - "helmet", "security identification", "safety ropes", - "intercom", "pointed water gun", "switch", - "alarm device", "joint", "construction street signs", - "gas detectors", "hoses", "hose_rectangle", - "flow-meter", "fire hydrant box", "fire extinguisher", - "lighting equipment", "flame-out protection", "exposed wires", - "circuit diagram", "cordon", "regulator", - "length adjuster", "stickers", "across wires", - "road cones", "hose", "filter", - "distribution box", "long-shank valves", "valve", "ducts" - }; - - static const unsigned char colors[19][3] = { - {54, 67, 244}, - {99, 30, 233}, - {176, 39, 156}, - {183, 58, 103}, - {181, 81, 63}, - {243, 150, 33}, - {244, 169, 3}, - {212, 188, 0}, - {136, 150, 0}, - {80, 175, 76}, - {74, 195, 139}, - {57, 220, 205}, - {59, 235, 255}, - {7, 193, 255}, - {0, 152, 255}, - {34, 87, 255}, - {72, 85, 121}, - {158, 158, 158}, - {139, 125, 96} - }; - int color_index = 0; for (const auto &obj: objects) { @@ -892,8 +877,8 @@ cv::rectangle(rgb, obj.rect, cc, 2); - char text[256]; - sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); + char text[64]; + sprintf(text, "%s", class_names[obj.label]); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, @@ -906,21 +891,15 @@ if (x + label_size.width > rgb.cols) x = rgb.cols - label_size.width; - cv::Size size = cv::Size(label_size.width, label_size.height + baseLine); - cv::Rect rc = cv::Rect(cv::Point(x, y), size); + cv::Point org = cv::Point(x, y + label_size.height); + + cv::Rect rc = cv::getTextSize(rgb.size(), text, org, chinese_font, 8); cv::rectangle(rgb, rc, cc, -1); cv::Scalar text_scalar = (color[0] + color[1] + color[2] >= 381) ? cv::Scalar(0, 0, 0) : cv::Scalar(255, 255, 255); - - - cv::putText(rgb, text, - cv::Point(x, y + label_size.height), - cv::FONT_HERSHEY_SIMPLEX, - 0.5, - text_scalar, 1 - ); + cv::putText(rgb, text, org, text_scalar, chinese_font, 8); } return 0; } diff --git a/app/src/main/cpp/yolo.h b/app/src/main/cpp/yolo.h index 1cbd0e5..4e040c5 100644 --- a/app/src/main/cpp/yolo.h +++ b/app/src/main/cpp/yolo.h @@ -16,7 +16,7 @@ #define YOLO_H #include - +#include "myfontface.h" #include struct Object { @@ -91,6 +91,9 @@ ncnn::UnlockedPoolAllocator blob_pool_allocator; ncnn::PoolAllocator workspace_pool_allocator; + //中文渲染字体 + MyFontFace chinese_font; + /** * 全局引用 * */ diff --git a/app/src/main/cpp/yolov8ncnn.cpp b/app/src/main/cpp/yolov8ncnn.cpp index e6a48e7..2a2b977 100644 --- a/app/src/main/cpp/yolov8ncnn.cpp +++ b/app/src/main/cpp/yolov8ncnn.cpp @@ -132,6 +132,9 @@ //检测 g_yolo->detect(rgb, objects); + + //绘制结果 + g_yolo->draw(rgb, objects); } else { draw_unsupported(rgb); } diff --git a/app/src/main/java/com/casic/br/app/external/INativeCallback.kt b/app/src/main/java/com/casic/br/app/external/INativeCallback.kt index e3fe678..2474cfb 100644 --- a/app/src/main/java/com/casic/br/app/external/INativeCallback.kt +++ b/app/src/main/java/com/casic/br/app/external/INativeCallback.kt @@ -14,7 +14,7 @@ ) /** - * 检测 + * 检测。只需要返回类别,目标在C++层已经绘制 */ - fun onDetect(output: ArrayList) + fun onDetect(output: ArrayList) } \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/src/main/cpp/yolo.cpp b/app/src/main/cpp/yolo.cpp index 02ff8d7..2eadf0d 100644 --- a/app/src/main/cpp/yolo.cpp +++ b/app/src/main/cpp/yolo.cpp @@ -348,6 +348,39 @@ /***模型分割*************/ +//目标类别 +static const char *class_names[] = { + "三脚架", "三通", "人", "切断阀", "危险告知牌", "压力测试仪", "压力表", "反光衣", + "呼吸面罩", "喉箍", "圆头水枪", "安全告知牌", "安全帽", "安全标识", "安全绳", "对讲机", + "尖头水枪", "开关", "报警装置", "接头", "施工路牌", "气体检测仪", "水带", "水带_矩形", + "流量计", "消火栓箱", "灭火器", "照明设备", "熄火保护", "电线暴露", "电路图", "警戒线", + "调压器", "调长器", "贴纸", "跨电线", "路锥", "软管", "过滤器", "配电箱", "长柄阀门", + "阀门", "风管" +}; + +//目标颜色数组 +static const unsigned char colors[19][3] = { + {54, 67, 244}, + {99, 30, 233}, + {176, 39, 156}, + {183, 58, 103}, + {181, 81, 63}, + {243, 150, 33}, + {244, 169, 3}, + {212, 188, 0}, + {136, 150, 0}, + {80, 175, 76}, + {74, 195, 139}, + {57, 220, 205}, + {59, 235, 255}, + {7, 193, 255}, + {0, 152, 255}, + {34, 87, 255}, + {72, 85, 121}, + {158, 158, 158}, + {139, 125, 96} +}; + Yolo::Yolo() { blob_pool_allocator.set_size_compare_ratio(0.f); workspace_pool_allocator.set_size_compare_ratio(0.f); @@ -807,20 +840,11 @@ //初始化ArrayList对象 jobject arraylist_obj = env->NewObject(list_clazz, arraylist_init); for (const auto &item: objects) { - auto rect = item.rect; - - float array[5]; - array[0] = rect.x; - array[1] = rect.y; - array[2] = rect.x + rect.width; - array[3] = rect.y + rect.height; - array[4] = (float) item.label; - - jfloatArray result_array = env->NewFloatArray(5); - env->SetFloatArrayRegion(result_array, 0, 5, array); + char index[16]; + sprintf(index, "%d", item.label); //add - env->CallBooleanMethod(arraylist_obj, arraylist_add, result_array); + env->CallBooleanMethod(arraylist_obj, arraylist_add, env->NewStringUTF(index)); } //回调 env->CallVoidMethod(j_callback, j_method_id, arraylist_obj); @@ -842,45 +866,6 @@ } int Yolo::draw(cv::Mat &rgb, const std::vector &objects) { - static const char *class_names[] = { - "tripod", "tee", "person", - "shut-off valve", "hazard signs", "pressure tester", - "pressure gauge", "reflective clothing", "respirator masks", - "throat foil", "round-headed water gun", "safety signs", - "helmet", "security identification", "safety ropes", - "intercom", "pointed water gun", "switch", - "alarm device", "joint", "construction street signs", - "gas detectors", "hoses", "hose_rectangle", - "flow-meter", "fire hydrant box", "fire extinguisher", - "lighting equipment", "flame-out protection", "exposed wires", - "circuit diagram", "cordon", "regulator", - "length adjuster", "stickers", "across wires", - "road cones", "hose", "filter", - "distribution box", "long-shank valves", "valve", "ducts" - }; - - static const unsigned char colors[19][3] = { - {54, 67, 244}, - {99, 30, 233}, - {176, 39, 156}, - {183, 58, 103}, - {181, 81, 63}, - {243, 150, 33}, - {244, 169, 3}, - {212, 188, 0}, - {136, 150, 0}, - {80, 175, 76}, - {74, 195, 139}, - {57, 220, 205}, - {59, 235, 255}, - {7, 193, 255}, - {0, 152, 255}, - {34, 87, 255}, - {72, 85, 121}, - {158, 158, 158}, - {139, 125, 96} - }; - int color_index = 0; for (const auto &obj: objects) { @@ -892,8 +877,8 @@ cv::rectangle(rgb, obj.rect, cc, 2); - char text[256]; - sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); + char text[64]; + sprintf(text, "%s", class_names[obj.label]); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, @@ -906,21 +891,15 @@ if (x + label_size.width > rgb.cols) x = rgb.cols - label_size.width; - cv::Size size = cv::Size(label_size.width, label_size.height + baseLine); - cv::Rect rc = cv::Rect(cv::Point(x, y), size); + cv::Point org = cv::Point(x, y + label_size.height); + + cv::Rect rc = cv::getTextSize(rgb.size(), text, org, chinese_font, 8); cv::rectangle(rgb, rc, cc, -1); cv::Scalar text_scalar = (color[0] + color[1] + color[2] >= 381) ? cv::Scalar(0, 0, 0) : cv::Scalar(255, 255, 255); - - - cv::putText(rgb, text, - cv::Point(x, y + label_size.height), - cv::FONT_HERSHEY_SIMPLEX, - 0.5, - text_scalar, 1 - ); + cv::putText(rgb, text, org, text_scalar, chinese_font, 8); } return 0; } diff --git a/app/src/main/cpp/yolo.h b/app/src/main/cpp/yolo.h index 1cbd0e5..4e040c5 100644 --- a/app/src/main/cpp/yolo.h +++ b/app/src/main/cpp/yolo.h @@ -16,7 +16,7 @@ #define YOLO_H #include - +#include "myfontface.h" #include struct Object { @@ -91,6 +91,9 @@ ncnn::UnlockedPoolAllocator blob_pool_allocator; ncnn::PoolAllocator workspace_pool_allocator; + //中文渲染字体 + MyFontFace chinese_font; + /** * 全局引用 * */ diff --git a/app/src/main/cpp/yolov8ncnn.cpp b/app/src/main/cpp/yolov8ncnn.cpp index e6a48e7..2a2b977 100644 --- a/app/src/main/cpp/yolov8ncnn.cpp +++ b/app/src/main/cpp/yolov8ncnn.cpp @@ -132,6 +132,9 @@ //检测 g_yolo->detect(rgb, objects); + + //绘制结果 + g_yolo->draw(rgb, objects); } else { draw_unsupported(rgb); } diff --git a/app/src/main/java/com/casic/br/app/external/INativeCallback.kt b/app/src/main/java/com/casic/br/app/external/INativeCallback.kt index e3fe678..2474cfb 100644 --- a/app/src/main/java/com/casic/br/app/external/INativeCallback.kt +++ b/app/src/main/java/com/casic/br/app/external/INativeCallback.kt @@ -14,7 +14,7 @@ ) /** - * 检测 + * 检测。只需要返回类别,目标在C++层已经绘制 */ - fun onDetect(output: ArrayList) + fun onDetect(output: ArrayList) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt b/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt index 93a543d..80af23d 100644 --- a/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt +++ b/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt @@ -6,6 +6,7 @@ import android.os.Bundle import android.os.Handler import android.os.Message +import android.util.Log import android.view.SurfaceHolder import android.view.View import android.view.WindowManager @@ -15,12 +16,10 @@ import com.amap.api.location.AMapLocation import com.casic.br.app.R import com.casic.br.app.databinding.ActivityStartCheckByYoloBinding -import com.casic.br.app.extensions.convert2YoloResult import com.casic.br.app.extensions.getSceneByTarget import com.casic.br.app.extensions.initImmersionBar import com.casic.br.app.extensions.isInScene import com.casic.br.app.external.INativeCallback -import com.casic.br.app.external.YoloResult import com.casic.br.app.external.Yolov8ncnn import com.casic.br.app.model.DictionaryModel import com.casic.br.app.utils.LocaleConstant @@ -233,6 +232,7 @@ val sceneName = data.getStringExtra("sceneName").toString() binding.titleView.setTitle(sceneName) detectedScene = sceneName + yolov8ncnn.onRestart() isDetectTarget = true } }) @@ -258,34 +258,17 @@ override fun onSegmentation( segmentationOutput: ArrayList, detectOutput: ArrayList ) { - //转成泛型集合 - val segmentationResults = ArrayList() - segmentationOutput.forEach { - segmentationResults.add(it.convert2YoloResult(this)) - } - val detectResults = ArrayList() - detectOutput.forEach { - detectResults.add(it.convert2YoloResult(this)) - } - binding.detectView.updateTargetPosition(segmentationResults, detectResults) } - override fun onDetect(output: ArrayList) { - //转成泛型集合 - val results = ArrayList() - output.forEach { - val yolo = it.convert2YoloResult(this) - results.add(yolo) - } - + override fun onDetect(output: ArrayList) { + Log.d(kTag, "onDetect: ${output.toJson()}") if (isDetectTarget) { - binding.detectView.updateTargetPosition(results) - if (results.isEmpty()) { + if (output.isEmpty()) { weakReferenceHandler.sendEmptyMessage(2024083001) } else { - results.forEach { - val label = LocaleConstant.CLASS_NAMES_ARRAY[it.type] + output.forEach { + val label = LocaleConstant.CLASS_NAMES_ARRAY[it.toInt()] if (label.isInScene(detectedScene)) { targetSet.add(label) //显示角标并弹框 @@ -297,16 +280,16 @@ } } } else { - if (results.isEmpty()) { + if (output.isEmpty()) { return } //暂停算法 yolov8ncnn.onPause() - val firstYoloResult = results.first() + val firstYoloResult = output.first().toInt() //取结果的第一个为场景判断 - val label = LocaleConstant.CLASS_NAMES_ARRAY[firstYoloResult.type] + val label = LocaleConstant.CLASS_NAMES_ARRAY[firstYoloResult] val scene = label.getSceneByTarget() val message = weakReferenceHandler.obtainMessage() diff --git a/app/build.gradle b/app/build.gradle index 8b72cd2..876b951 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ targetSdkVersion 33 versionCode 1000 versionName "1.0.0.0" - + ndkVersion "26.1.10909125" ndk { abiFilters "arm64-v8a", "armeabi-v7a", "x86_64", "x86" } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9ddca05..c4a7b65 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10) -set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-2.4.13.7-android/sdk/native/jni) +set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv-mobile-4.10.0-android/sdk/native/jni) find_package(OpenCV REQUIRED core imgproc) set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20240102-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn) diff --git a/app/src/main/cpp/yolo.cpp b/app/src/main/cpp/yolo.cpp index 02ff8d7..2eadf0d 100644 --- a/app/src/main/cpp/yolo.cpp +++ b/app/src/main/cpp/yolo.cpp @@ -348,6 +348,39 @@ /***模型分割*************/ +//目标类别 +static const char *class_names[] = { + "三脚架", "三通", "人", "切断阀", "危险告知牌", "压力测试仪", "压力表", "反光衣", + "呼吸面罩", "喉箍", "圆头水枪", "安全告知牌", "安全帽", "安全标识", "安全绳", "对讲机", + "尖头水枪", "开关", "报警装置", "接头", "施工路牌", "气体检测仪", "水带", "水带_矩形", + "流量计", "消火栓箱", "灭火器", "照明设备", "熄火保护", "电线暴露", "电路图", "警戒线", + "调压器", "调长器", "贴纸", "跨电线", "路锥", "软管", "过滤器", "配电箱", "长柄阀门", + "阀门", "风管" +}; + +//目标颜色数组 +static const unsigned char colors[19][3] = { + {54, 67, 244}, + {99, 30, 233}, + {176, 39, 156}, + {183, 58, 103}, + {181, 81, 63}, + {243, 150, 33}, + {244, 169, 3}, + {212, 188, 0}, + {136, 150, 0}, + {80, 175, 76}, + {74, 195, 139}, + {57, 220, 205}, + {59, 235, 255}, + {7, 193, 255}, + {0, 152, 255}, + {34, 87, 255}, + {72, 85, 121}, + {158, 158, 158}, + {139, 125, 96} +}; + Yolo::Yolo() { blob_pool_allocator.set_size_compare_ratio(0.f); workspace_pool_allocator.set_size_compare_ratio(0.f); @@ -807,20 +840,11 @@ //初始化ArrayList对象 jobject arraylist_obj = env->NewObject(list_clazz, arraylist_init); for (const auto &item: objects) { - auto rect = item.rect; - - float array[5]; - array[0] = rect.x; - array[1] = rect.y; - array[2] = rect.x + rect.width; - array[3] = rect.y + rect.height; - array[4] = (float) item.label; - - jfloatArray result_array = env->NewFloatArray(5); - env->SetFloatArrayRegion(result_array, 0, 5, array); + char index[16]; + sprintf(index, "%d", item.label); //add - env->CallBooleanMethod(arraylist_obj, arraylist_add, result_array); + env->CallBooleanMethod(arraylist_obj, arraylist_add, env->NewStringUTF(index)); } //回调 env->CallVoidMethod(j_callback, j_method_id, arraylist_obj); @@ -842,45 +866,6 @@ } int Yolo::draw(cv::Mat &rgb, const std::vector &objects) { - static const char *class_names[] = { - "tripod", "tee", "person", - "shut-off valve", "hazard signs", "pressure tester", - "pressure gauge", "reflective clothing", "respirator masks", - "throat foil", "round-headed water gun", "safety signs", - "helmet", "security identification", "safety ropes", - "intercom", "pointed water gun", "switch", - "alarm device", "joint", "construction street signs", - "gas detectors", "hoses", "hose_rectangle", - "flow-meter", "fire hydrant box", "fire extinguisher", - "lighting equipment", "flame-out protection", "exposed wires", - "circuit diagram", "cordon", "regulator", - "length adjuster", "stickers", "across wires", - "road cones", "hose", "filter", - "distribution box", "long-shank valves", "valve", "ducts" - }; - - static const unsigned char colors[19][3] = { - {54, 67, 244}, - {99, 30, 233}, - {176, 39, 156}, - {183, 58, 103}, - {181, 81, 63}, - {243, 150, 33}, - {244, 169, 3}, - {212, 188, 0}, - {136, 150, 0}, - {80, 175, 76}, - {74, 195, 139}, - {57, 220, 205}, - {59, 235, 255}, - {7, 193, 255}, - {0, 152, 255}, - {34, 87, 255}, - {72, 85, 121}, - {158, 158, 158}, - {139, 125, 96} - }; - int color_index = 0; for (const auto &obj: objects) { @@ -892,8 +877,8 @@ cv::rectangle(rgb, obj.rect, cc, 2); - char text[256]; - sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); + char text[64]; + sprintf(text, "%s", class_names[obj.label]); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, @@ -906,21 +891,15 @@ if (x + label_size.width > rgb.cols) x = rgb.cols - label_size.width; - cv::Size size = cv::Size(label_size.width, label_size.height + baseLine); - cv::Rect rc = cv::Rect(cv::Point(x, y), size); + cv::Point org = cv::Point(x, y + label_size.height); + + cv::Rect rc = cv::getTextSize(rgb.size(), text, org, chinese_font, 8); cv::rectangle(rgb, rc, cc, -1); cv::Scalar text_scalar = (color[0] + color[1] + color[2] >= 381) ? cv::Scalar(0, 0, 0) : cv::Scalar(255, 255, 255); - - - cv::putText(rgb, text, - cv::Point(x, y + label_size.height), - cv::FONT_HERSHEY_SIMPLEX, - 0.5, - text_scalar, 1 - ); + cv::putText(rgb, text, org, text_scalar, chinese_font, 8); } return 0; } diff --git a/app/src/main/cpp/yolo.h b/app/src/main/cpp/yolo.h index 1cbd0e5..4e040c5 100644 --- a/app/src/main/cpp/yolo.h +++ b/app/src/main/cpp/yolo.h @@ -16,7 +16,7 @@ #define YOLO_H #include - +#include "myfontface.h" #include struct Object { @@ -91,6 +91,9 @@ ncnn::UnlockedPoolAllocator blob_pool_allocator; ncnn::PoolAllocator workspace_pool_allocator; + //中文渲染字体 + MyFontFace chinese_font; + /** * 全局引用 * */ diff --git a/app/src/main/cpp/yolov8ncnn.cpp b/app/src/main/cpp/yolov8ncnn.cpp index e6a48e7..2a2b977 100644 --- a/app/src/main/cpp/yolov8ncnn.cpp +++ b/app/src/main/cpp/yolov8ncnn.cpp @@ -132,6 +132,9 @@ //检测 g_yolo->detect(rgb, objects); + + //绘制结果 + g_yolo->draw(rgb, objects); } else { draw_unsupported(rgb); } diff --git a/app/src/main/java/com/casic/br/app/external/INativeCallback.kt b/app/src/main/java/com/casic/br/app/external/INativeCallback.kt index e3fe678..2474cfb 100644 --- a/app/src/main/java/com/casic/br/app/external/INativeCallback.kt +++ b/app/src/main/java/com/casic/br/app/external/INativeCallback.kt @@ -14,7 +14,7 @@ ) /** - * 检测 + * 检测。只需要返回类别,目标在C++层已经绘制 */ - fun onDetect(output: ArrayList) + fun onDetect(output: ArrayList) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt b/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt index 93a543d..80af23d 100644 --- a/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt +++ b/app/src/main/java/com/casic/br/app/view/StartCheckByYoloActivity.kt @@ -6,6 +6,7 @@ import android.os.Bundle import android.os.Handler import android.os.Message +import android.util.Log import android.view.SurfaceHolder import android.view.View import android.view.WindowManager @@ -15,12 +16,10 @@ import com.amap.api.location.AMapLocation import com.casic.br.app.R import com.casic.br.app.databinding.ActivityStartCheckByYoloBinding -import com.casic.br.app.extensions.convert2YoloResult import com.casic.br.app.extensions.getSceneByTarget import com.casic.br.app.extensions.initImmersionBar import com.casic.br.app.extensions.isInScene import com.casic.br.app.external.INativeCallback -import com.casic.br.app.external.YoloResult import com.casic.br.app.external.Yolov8ncnn import com.casic.br.app.model.DictionaryModel import com.casic.br.app.utils.LocaleConstant @@ -233,6 +232,7 @@ val sceneName = data.getStringExtra("sceneName").toString() binding.titleView.setTitle(sceneName) detectedScene = sceneName + yolov8ncnn.onRestart() isDetectTarget = true } }) @@ -258,34 +258,17 @@ override fun onSegmentation( segmentationOutput: ArrayList, detectOutput: ArrayList ) { - //转成泛型集合 - val segmentationResults = ArrayList() - segmentationOutput.forEach { - segmentationResults.add(it.convert2YoloResult(this)) - } - val detectResults = ArrayList() - detectOutput.forEach { - detectResults.add(it.convert2YoloResult(this)) - } - binding.detectView.updateTargetPosition(segmentationResults, detectResults) } - override fun onDetect(output: ArrayList) { - //转成泛型集合 - val results = ArrayList() - output.forEach { - val yolo = it.convert2YoloResult(this) - results.add(yolo) - } - + override fun onDetect(output: ArrayList) { + Log.d(kTag, "onDetect: ${output.toJson()}") if (isDetectTarget) { - binding.detectView.updateTargetPosition(results) - if (results.isEmpty()) { + if (output.isEmpty()) { weakReferenceHandler.sendEmptyMessage(2024083001) } else { - results.forEach { - val label = LocaleConstant.CLASS_NAMES_ARRAY[it.type] + output.forEach { + val label = LocaleConstant.CLASS_NAMES_ARRAY[it.toInt()] if (label.isInScene(detectedScene)) { targetSet.add(label) //显示角标并弹框 @@ -297,16 +280,16 @@ } } } else { - if (results.isEmpty()) { + if (output.isEmpty()) { return } //暂停算法 yolov8ncnn.onPause() - val firstYoloResult = results.first() + val firstYoloResult = output.first().toInt() //取结果的第一个为场景判断 - val label = LocaleConstant.CLASS_NAMES_ARRAY[firstYoloResult.type] + val label = LocaleConstant.CLASS_NAMES_ARRAY[firstYoloResult] val scene = label.getSceneByTarget() val message = weakReferenceHandler.obtainMessage() diff --git a/app/src/main/res/layout/activity_start_check_by_yolo.xml b/app/src/main/res/layout/activity_start_check_by_yolo.xml index d0aa323..ba4123e 100644 --- a/app/src/main/res/layout/activity_start_check_by_yolo.xml +++ b/app/src/main/res/layout/activity_start_check_by_yolo.xml @@ -29,11 +29,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" /> - -