Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提取sdcard上图片的人脸关键点 #99

Open
CrazyShado opened this issue Jul 13, 2020 · 6 comments
Open

提取sdcard上图片的人脸关键点 #99

CrazyShado opened this issue Jul 13, 2020 · 6 comments

Comments

@CrazyShado
Copy link

CrazyShado commented Jul 13, 2020

从sdcard上选择一张图片输入,转换为bitmap,

   Bitmap bm = BitmapFactory.decodeFile("mnt/sdcard/test_face.jpeg");
        int width = bm.getWidth();
        int height = bm.getHeight();
        byte[] data =  bitmapToNv21(bm,width,height);
        FaceTracking.getInstance().Update(data, height, width);

然后将bitmap转换为nv21格式,但出现报错。
nv21转换函数:

 /**
     * Bitmap转化为ARGB数据,再转化为NV21数据
     *
     * @param src    传入ARGB_8888的Bitmap
     * @param width  NV21图像的宽度
     * @param height NV21图像的高度
     * @return nv21数据
     */
    public static byte[] bitmapToNv21(Bitmap src, int width, int height) {
        if (src != null && src.getWidth() >= width && src.getHeight() >= height) {
            int[] argb = new int[width * height];
            src.getPixels(argb, 0, width, 0, 0, width, height);
            return argbToNv21(argb, width, height);
        } else {
            return null;
        }
    }

/**
     * ARGB数据转化为NV21数据
     *
     * @param argb   argb数据
     * @param width  宽度
     * @param height 高度
     * @return nv21数据
     */
    private static byte[] argbToNv21(int[] argb, int width, int height) {
        int frameSize = width * height;
        int yIndex = 0;
        int uvIndex = frameSize;
        int index = 0;
        byte[] nv21 = new byte[width * height * 3 / 2];
        for (int j = 0; j < height; ++j) {
            for (int i = 0; i < width; ++i) {
                int R = (argb[index] & 0xFF0000) >> 16;
                int G = (argb[index] & 0x00FF00) >> 8;
                int B = argb[index] & 0x0000FF;
                int Y = (66 * R + 129 * G + 25 * B + 128 >> 8) + 16;
                int U = (-38 * R - 74 * G + 112 * B + 128 >> 8) + 128;
                int V = (112 * R - 94 * G - 18 * B + 128 >> 8) + 128;
                nv21[yIndex++] = (byte) (Y < 0 ? 0 : (Y > 255 ? 255 : Y));
                if (j % 2 == 0 && index % 2 == 0 && uvIndex < nv21.length - 2) {
                    nv21[uvIndex++] = (byte) (V < 0 ? 0 : (V > 255 ? 255 : V));
                    nv21[uvIndex++] = (byte) (U < 0 ? 0 : (U > 255 ? 255 : U));
                }

                ++index;
            }
        }
        return nv21;
    }

通过该方法转换后,sdk报错:

Assertion failed (sz.width % 2 == 0 && sz.height % 3 == 0) in cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<1>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0>; cv::impl::{anonymous}::SizePolicy sizePolicy = (cv::impl::<unnamed>::SizePolicy)1u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&], file /build/3_4_pack-android/opencv/modules/imgproc/src/color.simd_helpers.hpp, line 104

@yangkezun
Copy link
Collaborator

1、高宽的输入是不是反了
2、自己把byte数据在外围保存一张图片,看看转换是不是对的

@yeyupiaoling
Copy link

@CrazyShado 你能正常读取本地图片进行预测了吗?求助

@yeyupiaoling
Copy link

@yangkezun 我也是使用他上面的方法,我图片输入大小为(640,480)就没有报他的那个错误,但是没有预测结果。

@CrazyShado
Copy link
Author

@CrazyShado 你能正常读取本地图片进行预测了吗?求助

我换阿里的mnn了,

@yeyupiaoling
Copy link

@CrazyShado 哦哦,感觉这个挺好的,有活体检测

@CrazyShado
Copy link
Author

挺好

阿里的也有了,包括各种角度也有的,输入图片也没限制

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants