我如何解决在tflte的run方法中发生的空指针错误

时间:2019-03-27 14:40:46

标签: android tensorflow tensorflow-lite

我想使用使用CNN和RNN组合训练的预训练模型预测手写单词。离线手写单词识别器,无需使用服务器。

public void pickImage(View view) {
        bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.img1);
        img.setImageBitmap(bitmap);
        Toast.makeText(getApplicationContext(),"Width:- "+bitmap.getWidth()+" Height "+bitmap.getHeight(),Toast.LENGTH_SHORT).show();
        convertBitmapToByte();
    }
    private void convertBitmapToByte()
    {
        if (mdata == null)
        {
            Toast.makeText(getApplicationContext(),"Return",Toast.LENGTH_SHORT).show();
            return;
        }

        mdata.rewind();
        bitmap.getPixels(getPixels,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
        int pixel =0;
        for(int i=0;i<748;++i)
        {
            for(int j=0;j<294;++j)
            {
                final int val = getPixels[pixel++];
                mdata.putFloat(convertToGreyScale(val));
            }
        }
    }
    private float convertToGreyScale(int colour)
    {
        return (((colour >> 16) & 0xFF)+((colour >>8) & 0xFF)+((colour & 0xFF)))/3.0f/255.0f;
    }

    public void predAndSet(View view) {
        tv.setText("Temp");
        if(mdata == null)
        {
            Toast.makeText(getApplicationContext(),"Return",Toast.LENGTH_SHORT).show();
            return;
        }
        interpreter.run(mdata,mResult);
        tv.setText(Arrays.toString(mResult[0]));
    }
}

Stacktrace

Process: com.soham.s.charrecongizer, PID: 29211
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
                      at android.view.View.performClick(View.java:6669)
                      at android.view.View.performClickInternal(View.java:6638)
                      at android.view.View.access$3100(View.java:789)
                      at android.view.View$PerformClick.run(View.java:26145)
                      at android.os.Handler.handleCallback(Handler.java:873)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6863)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
                      at android.view.View.performClick(View.java:6669) 
                      at android.view.View.performClickInternal(View.java:6638) 
                      at android.view.View.access$3100(View.java:789) 
                      at android.view.View$PerformClick.run(View.java:26145) 
                      at android.os.Handler.handleCallback(Handler.java:873) 
                      at android.os.Handler.dispatchMessage(Handler.java:99) 
                      at android.os.Looper.loop(Looper.java:193) 
                      at android.app.ActivityThread.main(ActivityThread.java:6863) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.tensorflow.lite.Interpreter.run(java.lang.Object, java.lang.Object)' on a null object reference
                      at com.soham.s.charrecongizer.MainActivity.predAndSet(MainActivity.java:103)

我想看到图像中的文字可以预测并显示在文本框中

0 个答案:

没有答案