Skip to content

Commit

Permalink
Update android demo (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
RangiLyu committed Dec 25, 2021
1 parent efddda7 commit d91255b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
16 changes: 14 additions & 2 deletions demo_android_ncnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ Download ncnn-android-vulkan.zip from ncnn repo or build ncnn-android from sourc
## Step2.
Unzip ncnn-android-vulkan.zip into demo_android_ncnn/app/src/main/cpp or change the ncnn_DIR path to yours in demo_android_ncnn/app/src/main/cpp/CMakeLists.txt

```bash
# e.g. change to `ncnn-20211208-android-vulkan` if download version 200211208
set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20211208-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
```

## Step3.
Copy the NanoDet ncnn model file (nanodet_m.param and nanodet_m.bin) from models folder into demo_android_ncnn/app/src/main/assets
Copy the NanoDet ncnn model file and rename to nanodet.param and nanodet.bin from models folder into demo_android_ncnn/app/src/main/assets

* [NanoDet ncnn model download link](https://github.com/RangiLyu/nanodet/releases/download/v0.3.0/nanodet_m_ncnn_model.zip)
* [NanoDet ncnn model download link](https://drive.google.com/file/d/1cuVBJiFKwyq1-l3AwHoP2boTesUQP-6K/view?usp=sharing)

If you want to run yolov4-tiny and yolov5s, download them and also put in demo_android_ncnn/app/src/main/assets.

Expand All @@ -28,6 +33,13 @@ Open demo_android_ncnn folder with Android Studio and then build it.
# Screenshot
![](Android_demo.jpg)

# Notice

* The FPS in the app includes pre-process, post-process and visualization, not equal to the model inference time.

* If meet error like `No version of NDK matched the requested version`, set `android { ndkVersion` to your ndk version.

* If you want to use custom model, remember to change the hyperparams in `demo_android_ncnn/app/src/main/cpp/NanoDet.h` the same with your training config.

# Reference

Expand Down
2 changes: 1 addition & 1 deletion demo_android_ncnn/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)

set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20201218-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
set(ncnn_DIR ${CMAKE_SOURCE_DIR}/ncnn-20211208-android-vulkan/${ANDROID_ABI}/lib/cmake/ncnn)
find_package(ncnn REQUIRED)

add_library(yolov5 SHARED
Expand Down
14 changes: 7 additions & 7 deletions demo_android_ncnn/app/src/main/cpp/NanoDet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ NanoDet::~NanoDet()

void NanoDet::preprocess(JNIEnv *env, jobject image, ncnn::Mat& in)
{
in = ncnn::Mat::from_android_bitmap_resize(env, image, ncnn::Mat::PIXEL_RGBA2BGR, input_size, input_size);
in = ncnn::Mat::from_android_bitmap_resize(env, image, ncnn::Mat::PIXEL_RGBA2BGR, input_size[1], input_size[0]);
// in = ncnn::Mat::from_pixels(image.data, ncnn::Mat::PIXEL_BGR, img_w, img_h);
//in = ncnn::Mat::from_pixels_resize(image.data, ncnn::Mat::PIXEL_BGR, img_w, img_h, this->input_width, this->input_height);

Expand All @@ -91,8 +91,8 @@ void NanoDet::preprocess(JNIEnv *env, jobject image, ncnn::Mat& in)
std::vector<BoxInfo> NanoDet::detect(JNIEnv *env, jobject image, float score_threshold, float nms_threshold) {
AndroidBitmapInfo img_size;
AndroidBitmap_getInfo(env, image, &img_size);
float width_ratio = (float) img_size.width / (float) this->input_size;
float height_ratio = (float) img_size.height / (float) this->input_size;
float width_ratio = (float) img_size.width / (float) this->input_size[1];
float height_ratio = (float) img_size.height / (float) this->input_size[0];

ncnn::Mat input;
this->preprocess(env, image, input);
Expand Down Expand Up @@ -167,8 +167,8 @@ void NanoDet::decode_infer(ncnn::Mat& feats, std::vector<CenterPrior>& center_pr

BoxInfo NanoDet::disPred2Bbox(const float*& dfl_det, int label, float score, int x, int y, int stride, float width_ratio, float height_ratio)
{
float ct_x = (x + 0.5) * stride;
float ct_y = (y + 0.5) * stride;
float ct_x = x * stride;
float ct_y = y * stride;
std::vector<float> dis_pred;
dis_pred.resize(4);
for (int i = 0; i < 4; i++)
Expand All @@ -187,8 +187,8 @@ BoxInfo NanoDet::disPred2Bbox(const float*& dfl_det, int label, float score, int
}
float xmin = (std::max)(ct_x - dis_pred[0], .0f) * width_ratio;
float ymin = (std::max)(ct_y - dis_pred[1], .0f) * height_ratio;
float xmax = (std::min)(ct_x + dis_pred[2], (float)this->input_size) * width_ratio;
float ymax = (std::min)(ct_y + dis_pred[3], (float)this->input_size) * height_ratio;
float xmax = (std::min)(ct_x + dis_pred[2], (float)this->input_size[1]) * width_ratio;
float ymax = (std::min)(ct_y + dis_pred[3], (float)this->input_size[0]) * height_ratio;

//std::cout << xmin << "," << ymin << "," << xmax << "," << xmax << "," << std::endl;
return BoxInfo { xmin, ymin, xmax, ymax, score, label };
Expand Down
2 changes: 1 addition & 1 deletion demo_android_ncnn/app/src/main/cpp/NanoDet.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class NanoDet{

ncnn::Net *Net;
// modify these parameters to the same with your config if you want to use your own model
int input_size[2] = {416, 416}; // input height and width
int input_size[2] = {320, 320}; // input height and width
int num_class = 80; // number of classes. 80 for COCO
int reg_max = 7; // `reg_max` set in the training config. Default: 7.
std::vector<int> strides = { 8, 16, 32, 64 }; // strides of the multi-level feature.
Expand Down
2 changes: 1 addition & 1 deletion demo_android_ncnn/app/src/main/cpp/jni_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C" JNIEXPORT void JNICALL
Java_com_rangi_nanodet_NanoDet_init(JNIEnv *env, jclass, jobject assetManager, jboolean useGPU) {
if (NanoDet::detector == nullptr) {
AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
NanoDet::detector = new NanoDet(mgr, "nanodet_m.param", "nanodet_m.bin", useGPU);
NanoDet::detector = new NanoDet(mgr, "nanodet.param", "nanodet.bin", useGPU);
}
}

Expand Down

0 comments on commit d91255b

Please sign in to comment.