tumuyan / RealSR-NCNN-Android

An Android application for super-resolution & interpolation. Contains RealSR-NCNN, SRMD-NCNN, RealCUGAN-NCNN, Real-ESRGAN-NCNN, Waifu2x-NCNN, Anime4kcpp, nearest, bilinear, bicubic, AVIR...
Other
968 stars 85 forks source link

How can I directly use the functions of RealSR without calling through the CLI? #68

Open laverne01 opened 2 weeks ago

laverne01 commented 2 weeks ago

I tried creating a convert function in JNI (which calls the process function of RealSR) and used it in Kotlin. However, the output image after upscaling is just black. I would appreciate your advice on what might be going wrong.

JNIEXPORT jobject JNICALL Java_com_example_realsrandroid_ImageUpscale_convert( JNIEnv *env, jobject /* this */, jobject bitmapIn, jobject assetManager) { ncnn::Mat in_img = ncnn::Mat::from_android_bitmap(env, bitmapIn,ANDROID_BITMAP_FORMAT_RGBA_8888); ncnn::Mat out_img = ncnn::Mat(in_img.w * scale, in_img.h * scale, in_img.c, (size_t) in_img.elemsize, in_img.elempack); realsr.process(in_img, out_img); out_img.to_android_bitmap(env, bitmapIn, ANDROID_BITMAP_FORMAT_RGBA_8888); return bitmapIn; }

I have initialized RealSR on it.

realsr.scale = 4; realsr.tilesize = 200; realsr.prepadding = 10; AAssetManager *mgr = AAssetManager_fromJava(env, assetManager); realsr.load(mgr);

RobbWatershed commented 2 weeks ago

I've buried hours into that problem and didn't find a working solution for retrieving the output bitmap in memory from NCNN's native environment to my app's Java/Kotlin environment.

I eventually ended up :

Here's the Kotlin side of the JNI interface I made : https://github.com/avluis/Hentoid/blob/master/app/ai-upscale/src/main/java/me/devsaki/hentoid/ai_upscale/AiUpscaler.kt

If you find anything more elegant, I'd be super glad to know 🙃

laverne01 commented 2 weeks ago

@RobbWatershed Thank you, this helps me a lot!

tumuyan commented 1 week ago

Calling functions directly with jni instead of writing to a file is difficult. I tried but failed, so that's why I'm using the current solution

laverne01 commented 1 week ago

Calling the function directly makes the code look better. I will try it out and will message back if I have a solution. Thank you.