有Java等效于NumPy吗?

时间:2019-08-17 13:04:33

标签: java python numpy tensorflow

我试图从tflite模型中获取预测,以获取眼睛区域的地标。 我使用python脚本从tflite模型成功获取了值,但是我不知道如何使用MLKit firebase获得这些值。 Python脚本使用numpy库将图像转换为数组。 我想知道这里是什么问题? 有什么方法可以将numpy函数与Java代码一起使用? 如果不是,那么Java中numpy库的等效项是什么?

Python脚本

data = np.asarray( img, dtype="float32" ) 

# Inference on input data normalized to [0, 1]
inputImg = np.expand_dims(data,0).astype(np.float32)
input_details = interpreter.get_input_details()
interpreter.set_tensor(input_details[0]['index'], inputImg)

interpreter.invoke()

output_details = interpreter.get_output_details()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

Java代码(Android)的问题在于这些功能:

private float[][][][] bitmapToInputArray() {
        // [START mlkit_bitmap_input]
        Bitmap bitmap= getYourInputImage();
        bitmap = Bitmap.createScaledBitmap(bitmap, 112, 112, true);

        int batchNum = 0;
        float[][][][] input = new float[1][112][112][3];
        for (int x = 0; x < 112; x++) {
            for (int y = 0; y < 112; y++) {
                int pixel = bitmap.getPixel(x, y);
                // Normalize channel values to [-1.0, 1.0]. This requirement varies by
                // model. For example, some models might require values to be normalized
                // to the range [0.0, 1.0] instead.
                input[batchNum][x][y][0] = (Color.red(pixel) - 127) / 128.0f;
                input[batchNum][x][y][1] = (Color.green(pixel) - 127) / 128.0f;
                input[batchNum][x][y][2] = (Color.blue(pixel) - 127) / 128.0f;
                Log.i("Input","input"+input[batchNum][x][y]);

            }
        }
        // [END mlkit_bitmap_input]

      return input;
    }

 private void useInferenceResult(float[] probabilities) throws IOException {
        String[] result=new String[80];
        String x="";
        String y="";
        ArrayList<Point> listpoint= new ArrayList<Point>();

        double viewWidth = canvas.getWidth();
        double viewHeight = canvas.getHeight();
        double imageWidth = mutableBitmap.getWidth();
        double imageHeight = mutableBitmap.getHeight();
        Log.i("viewWidth","viewwidth "+viewWidth);
        Log.i("viewHeight","viewheight "+viewHeight);
        Log.i("imagewidth","imagewidth "+imageWidth);
        Log.i("imaageHeigh","imageheigh "+imageHeight);
        double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);
        Log.i("Scale","Scale"+scale);
        try {
            for (int i = 0; i < probabilities.length; i++) {

                Log.i("MLKit", String.format("%1.8f", probabilities[i]));
                float i1 = probabilities[i];
                Log.i("floaaat", "" + i1);
            }
        }
  • python脚本的结果:
  

[0.33135968 0.19592011 0.34212315 0.17297666 0.36624995 0.16413747    0.3894139 0.17440952 0.39828074 0.1978043 0.3891497 0.22268474    0.36345637 0.22974193 0.3401759 0.2193309 0.30167252 0.20411113    0.3167112 0.19134495 0.33793524 0.18388326 0.3642417 0.18049955    0.3903508 0.18533507 0.40906873 0.1957745 0.42142123 0.21091096    0.40550107 0.21829814 0.38345626 0.22071144 0.35900232 0.22142673    0.3363348 0.21877256 0.3161971 0.2133534 0.62843406 0.21482795    0.6389724 0.1914106 0.6628249 0.1835615 0.6858679 0.19583184    0.6946868 0.22111627 0.6840309 0.24444285 0.66027373 0.25241333    0.6351568 0.24192403 0.60499936 0.22642238 0.6210091 0.21289764    0.6423563 0.2042976 0.6685919 0.20277795 0.69201195 0.20948553    0.70882106 0.22015369 0.71931773 0.23518339 0.7076659 0.24166131    0.69054717 0.24350837 0.6694564 0.24258481 0.64537776 0.23927754    0.62199306 0.23511863]

  • Android Java代码的结果:
  

2019-08-17 14:47:50.617 21349-21349 / com.example.irisdetection I / MLKit:0,23961355   2019-08-17 14:47:50.620 21349-21349 / com.example.irisdetection I / MLKit:0,25104424   2019-08-17 14:47:50.621 21349-21349 / com.example.irisdetection I / MLKit:0,28179651   2019-08-17 14:47:50.622 21349-21349 / com.example.irisdetection I / MLKit:0,31467810   2019-08-17 14:47:50.623 21349-21349 / com.example.irisdetection I / MLKit:0,33257431   2019-08-17 14:47:50.624 21349-21349 / com.example.irisdetection I / MLKit:0,32645294   2019-08-17 14:47:50.625 21349-21349 / com.example.irisdetection I / MLKit:0,29138848   2019-08-17 14:47:50.626 21349-21349 / com.example.irisdetection I / MLKit:025581932   2019-08-17 14:47:50.627 21349-21349 / com.example.irisdetection I / MLKit:0,19593856   2019-08-17 14:47:50.628 21349-21349 / com.example.irisdetection I / MLKit:0,21698779   2019-08-17 14:47:50.631 21349-21349 / com.example.irisdetection I / MLKit:0,24266151   2019-08-17 14:47:50.632 21349-21349 / com.example.irisdetection I / MLKit:0,27562365   2019-08-17 14:47:50.633 21349-21349 / com.example.irisdetection I / MLKit:0,30823168   2019-08-17 14:47:50.635 21349-21349 / com.example.irisdetection I / MLKit:0,33465266   2019-08-17 14:47:50.636 21349-21349 / com.example.irisdetection I / MLKit:0,35355449   2019-08-17 14:47:50.637 21349-21349 / com.example.irisdetection I / MLKit:0,34009647   2019-08-17 14:47:50.638 21349-21349 / com.example.irisdetection I / MLKit:0,31358159   2019-08-17 14:47:50.640 21349-21349 / com.example.irisdetection I / MLKit:0,28156102   2019-08-17 14:47:50.642 21349-21349 / com.example.irisdetection I / MLKit:0,25063315   2019-08-17 14:47:50.643 21349-21349 / com.example.irisdetection I / MLKit:0,21878451   2019-08-17 14:47:50.644 21349-21349 / com.example.irisdetection I / MLKit:0,69623101   2019-08-17 14:47:50.646 21349-21349 / com.example.irisdetection I / MLKit:0,70167470   2019-08-17 14:47:50.646 21349-21349 / com.example.irisdetection I / MLKit:0,73317540   2019-08-17 14:47:50.648 21349-21349 / com.example.irisdetection I / MLKit:0,76974392   2019-08-17 14:47:50.649 21349-21349 / com.example.irisdetection I / MLKit:0,79195201   2019-08-17 14:47:50.651 21349-21349 / com.example.irisdetection I / MLKit:0,78359401   2019-08-17 14:47:50.652 21349-21349 / com.example.irisdetection I / MLKit:0,75674009   2019-08-17 14:47:50.653 21349-21349 / com.example.irisdetection I / MLKit:0,71786618   2019-08-17 14:47:50.654 21349-21349 / com.example.irisdetection I / MLKit:0,66782737   2019-08-17 14:47:50.655 21349-21349 / com.example.irisdetection I / MLKit:0,68930006   2019-08-17 14:47:50.656 21349-21349 / com.example.irisdetection I / MLKit:0,71668541   2019-08-17 14:47:50.657 21349-21349 / com.example.irisdetection I / MLKit:0,75279719   2019-08-17 14:47:50.658 21349-21349 / com.example.irisdetection I / MLKit:0,78872705   2019-08-17 14:47:50.659 21349-21349 / com.example.irisdetection I / MLKit:0,81867975   2019-08-17 14:47:50.661 21349-21349 / com.example.irisdetection I / MLKit:0,83806717   2019-08-17 14:47:50.662 21349-21349 / com.example.irisdetection I / MLKit:0,82371044   2019-08-17 14:47:50.664 21349-21349 / com.example.irisdetection I / MLKit:0,79749656   2019-08-17 14:47:50.665 21349-21349 / com.example.irisdetection I / MLKit:0,76317006   2019-08-17 14:47:50.666 21349-21349 / com.example.irisdetection I / MLKit:0,72700304   2019-08-17 14:47:50.667 21349-21349 / com.example.irisdetection I / MLKit:0,69159627

2 个答案:

答案 0 :(得分:0)

nd4j怎么样?它是deeplearning4j(深度学习的Java框架)的一部分。

答案 1 :(得分:0)

对我来说,将颜色的顺序从 RGB 更改为 BRG 有帮助。如果您使用 cv2.imread 获取图像,那么它将是 BGR 和 8 位调色板(像素为 256 色)。所以你做的几乎是正确的,只需改变顺序