From 8235eece1e11148c722f2409a5af205fe064461f Mon Sep 17 00:00:00 2001 From: zhoujun Date: Tue, 8 Jun 2021 07:32:42 -0500 Subject: [PATCH] Speed up pixel assignment (#3052) --- .../paddle/lite/demo/ocr/MainActivity.java | 8 +++-- .../baidu/paddle/lite/demo/ocr/Predictor.java | 33 +++++++++---------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java b/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java index 1c83e218..b4ea34e2 100644 --- a/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java +++ b/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/MainActivity.java @@ -465,8 +465,12 @@ public class MainActivity extends AppCompatActivity { } public void btn_load_model_click(View view) { - tvStatus.setText("STATUS: load model ......"); - loadModel(); + if (predictor.isLoaded()){ + tvStatus.setText("STATUS: model has been loaded"); + }else{ + tvStatus.setText("STATUS: load model ......"); + loadModel(); + } } public void btn_run_model_click(View view) { diff --git a/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java b/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java index 1c294995..b474d888 100644 --- a/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java +++ b/deploy/android_demo/app/src/main/java/com/baidu/paddle/lite/demo/ocr/Predictor.java @@ -194,26 +194,25 @@ public class Predictor { "supported!"); return false; } - int[] channelStride = new int[]{width * height, width * height * 2}; - int p = scaleImage.getPixel(scaleImage.getWidth() - 1, scaleImage.getHeight() - 1); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int color = scaleImage.getPixel(x, y); - float[] rgb = new float[]{(float) red(color) / 255.0f, (float) green(color) / 255.0f, - (float) blue(color) / 255.0f}; - inputData[y * width + x] = (rgb[channelIdx[0]] - inputMean[0]) / inputStd[0]; - inputData[y * width + x + channelStride[0]] = (rgb[channelIdx[1]] - inputMean[1]) / inputStd[1]; - inputData[y * width + x + channelStride[1]] = (rgb[channelIdx[2]] - inputMean[2]) / inputStd[2]; - } + int[] channelStride = new int[]{width * height, width * height * 2}; + int[] pixels=new int[width*height]; + scaleImage.getPixels(pixels,0,scaleImage.getWidth(),0,0,scaleImage.getWidth(),scaleImage.getHeight()); + for (int i = 0; i < pixels.length; i++) { + int color = pixels[i]; + float[] rgb = new float[]{(float) red(color) / 255.0f, (float) green(color) / 255.0f, + (float) blue(color) / 255.0f}; + inputData[i] = (rgb[channelIdx[0]] - inputMean[0]) / inputStd[0]; + inputData[i + channelStride[0]] = (rgb[channelIdx[1]] - inputMean[1]) / inputStd[1]; + inputData[i+ channelStride[1]] = (rgb[channelIdx[2]] - inputMean[2]) / inputStd[2]; } } else if (channels == 1) { - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int color = inputImage.getPixel(x, y); - float gray = (float) (red(color) + green(color) + blue(color)) / 3.0f / 255.0f; - inputData[y * width + x] = (gray - inputMean[0]) / inputStd[0]; - } + int[] pixels=new int[width*height]; + scaleImage.getPixels(pixels,0,scaleImage.getWidth(),0,0,scaleImage.getWidth(),scaleImage.getHeight()); + for (int i = 0; i < pixels.length; i++) { + int color = pixels[i]; + float gray = (float) (red(color) + green(color) + blue(color)) / 3.0f / 255.0f; + inputData[i] = (gray - inputMean[0]) / inputStd[0]; } } else { Log.i(TAG, "Unsupported channel size " + Integer.toString(channels) + ", only channel 1 and 3 is " + -- GitLab