From 6f5033eb7aa9b07e175b1fcf00298c0d2ee63c7e Mon Sep 17 00:00:00 2001 From: "will.zhang" <122812210@qq.com> Date: Sat, 14 Nov 2020 22:55:43 +0800 Subject: [PATCH] update mobile face net param --- app/src/main/cpp/task/NCNNRecognizer.h | 18 ++-- app/src/main/cpp/task/mobile_facenet.h | 95 ------------------- app/src/main/java/com/kk/afdd/Config.java | 2 +- .../main/java/com/kk/afdd/MainActivity.java | 17 ++-- 4 files changed, 16 insertions(+), 116 deletions(-) delete mode 100644 app/src/main/cpp/task/mobile_facenet.h diff --git a/app/src/main/cpp/task/NCNNRecognizer.h b/app/src/main/cpp/task/NCNNRecognizer.h index 1374237..f132b82 100644 --- a/app/src/main/cpp/task/NCNNRecognizer.h +++ b/app/src/main/cpp/task/NCNNRecognizer.h @@ -24,24 +24,20 @@ class NCNNRecognizer { } float *getFeature(cv::Mat croppedFace) { - ncnn::Extractor ex = recognizer.create_extractor(); - ex.set_num_threads(1); - ex.set_light_mode(true); - ncnn::Mat ncnnFace = ncnn::Mat::from_pixels(croppedFace.data, + ncnn::Mat ncnnFace = ncnn::Mat::from_pixels_resize(croppedFace.data, ncnn::Mat::PIXEL_BGR2RGB, croppedFace.cols, - croppedFace.rows); + croppedFace.rows, + 112, + 112); + ncnn::Extractor ex = recognizer.create_extractor(); + // x.set_num_threads(4); ex.input("data", ncnnFace); ncnn::Mat out; ex.extract("fc1", out); float *feature = new float[128]; for (int j = 0; j < 128; j++) { - // TODO not working ? - if (std::isnan(out[j]) || std::isinf(out[j])) { - feature[j] = 0; - } else { - feature[j] = out[j]; - } + feature[j] = out[j]; } return feature; } diff --git a/app/src/main/cpp/task/mobile_facenet.h b/app/src/main/cpp/task/mobile_facenet.h deleted file mode 100644 index ab41fd8..0000000 --- a/app/src/main/cpp/task/mobile_facenet.h +++ /dev/null @@ -1,95 +0,0 @@ -// -// Created by 朱林 on 2020/5/9. -// -#ifndef FACE_NCNN_MOBILE_FACENET_H -#define FACE_NCNN_MOBILE_FACENET_H -#pragma once - -#include -#include "net.h" -#include - -class FaceRecognize { -public: - int num_thread = 4; - bool run_gpu = false; - const int feature_dim = 128; - const int resize_w_h = 112; // 96 or 112 - - FaceRecognize() { - } - - ~FaceRecognize() { - recognizeNet.clear(); - if (run_gpu) { - ncnn::destroy_gpu_instance(); - } - } - - void setThreadNum(int threads) { - num_thread = threads; - } - - void setRunGpu(bool run) { - run_gpu = run; - } - - bool init(const std::string &bin_path, const std::string ¶m_path, bool run_gpu = false) { - setRunGpu(run_gpu); - if (run_gpu) { - ncnn::create_gpu_instance(); - } - ncnn::Option opt; - opt.lightmode = true; - opt.num_threads = num_thread; - if (run_gpu && ncnn::get_gpu_count() != 0) { - opt.use_vulkan_compute = true; - } - recognizeNet.opt = opt; - int ret = recognizeNet.load_param(param_path.data()); - if (ret != 0) { - return false; - } - ret = recognizeNet.load_model(bin_path.data()); - return ret == 0; - } - - float *getFeature(ncnn::Mat &img) { - return runNet(img); - } - - float calculateSimilar(const float *feature1, const float *feature2) { - float ret = 0.0, mod1 = 0.0, mod2 = 0.0; - for (std::vector::size_type i = 0; i != feature_dim; ++i) { - ret += feature1[i] * feature2[i]; - mod1 += feature1[i] * feature1[i]; - mod2 += feature2[i] * feature2[i]; - } - return (ret / sqrt(mod1) / sqrt(mod2) + 1) / 2.0f; - } - -private: - ncnn::Net recognizeNet; - - float *runNet(ncnn::Mat &img) { - ncnn::Extractor ex = recognizeNet.create_extractor(); - if (run_gpu && ncnn::get_gpu_count() != 0) { - ex.set_vulkan_compute(true); - } - ex.set_light_mode(true); - ex.set_num_threads(num_thread); - ncnn::Mat in; - ncnn::resize_bilinear(img, in, resize_w_h, resize_w_h); - ex.input("data", in); - ncnn::Mat out; - ex.extract("fc1", out); - float *feature = new float[feature_dim]; - for (int j = 0; j < 128; j++) { - feature[j] = out[j]; - } - return feature; - } -}; - - -#endif //FACE_NCNN_MOBILE_FACENET_H diff --git a/app/src/main/java/com/kk/afdd/Config.java b/app/src/main/java/com/kk/afdd/Config.java index a8b5037..1cdbddf 100644 --- a/app/src/main/java/com/kk/afdd/Config.java +++ b/app/src/main/java/com/kk/afdd/Config.java @@ -4,6 +4,6 @@ public class Config { public static final int PREVIEW_WIDTH = 240; public static final int PREVIEW_HEIGHT = 320; public static final int ROTATION = 90; - public static final int FPS = 5; + public static final int FPS = 2; public static final boolean MIRROR = true; } diff --git a/app/src/main/java/com/kk/afdd/MainActivity.java b/app/src/main/java/com/kk/afdd/MainActivity.java index baf94f4..f4fd84c 100644 --- a/app/src/main/java/com/kk/afdd/MainActivity.java +++ b/app/src/main/java/com/kk/afdd/MainActivity.java @@ -234,15 +234,14 @@ public void onFaceDetected(String faceInfoJson) { sFaceInfoType = new TypeToken>() { }.getType(); } - String ff = faceInfoJson; - int maxLogSize = 1000; - for(int i = 0; i <= ff.length() / maxLogSize; i++) { - int start = i * maxLogSize; - int end = (i+1) * maxLogSize; - end = Math.min(end, ff.length()); - Timber.v(ff.substring(start, end)); - } - faceInfoJson = faceInfoJson.replaceAll("nan", "0").replaceAll("inf", "0"); + // String ff = faceInfoJson; + // int maxLogSize = 1000; + // for(int i = 0; i <= ff.length() / maxLogSize; i++) { + // int start = i * maxLogSize; + // int end = (i+1) * maxLogSize; + // end = Math.min(end, ff.length()); + // Timber.v(ff.substring(start, end)); + // } List faceInfoList = mGson.fromJson(faceInfoJson, sFaceInfoType); if (faceInfoList.size() > 0) { for (FaceInfo faceInfo : faceInfoList) {