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

Python.start(new AndroidPlatform(this)) doesnt work #513

Closed
Josonlchui opened this issue Jul 2, 2021 · 4 comments
Closed

Python.start(new AndroidPlatform(this)) doesnt work #513

Josonlchui opened this issue Jul 2, 2021 · 4 comments

Comments

@Josonlchui
Copy link

Josonlchui commented Jul 2, 2021

I use chaquo 9.1.0. Try to integrate the python (v3.8) script to android studio with following coding. After running a few times of loading .pth. The"Python.start(new AndroidPlatform(this));" - crash with the following message.
I dont know how to solve it or can I stop the current Python.start() in the emulator or android, or any coding to prevent this happening?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("MainActivity", "oncreate");
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.submit);
        iv = (ImageView) findViewById(R.id.image_view);
        iv2 = (ImageView) findViewById(R.id.image_view2);

        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }

        Python py = Python.getInstance();

        btn.setClickable(true);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                drawable = (BitmapDrawable)iv.getDrawable();
                bitmap = drawable.getBitmap();
                imageString = getStringImage(bitmap);
/*
                Log.v("chaquopy", "start getModule");
                PyObject pyobj = py.getModule("coltobw"); //give python sctipt name
                Log.v("chaquopy", "start callAttr");
                PyObject obj = pyobj.callAttr( "main", imageString);

                imgStr = obj.toString();

                //convert string to byte array, to bitmap
                byte data[] = android.util.Base64.decode(imgStr, Base64.DEFAULT);
                Bitmap bmp = BitmapFactory.decodeByteArray(data,0, data.length );
                iv2.setImageBitmap(bmp);
                Log.v("chaquopy", "finish"); */

            }

        });

    }
    --------- beginning of crash
2021-07-01 16:13:36.935 4986-4986/com.example.jhrembg E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.jhrembg, PID: 4986
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jhrembg/com.example.jhrembg.MainActivity}: com.chaquo.python.PyException: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: com.chaquo.python.PyException: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
        at <python>.codecs.decode(codecs.py:322)
        at <python>.site.addpackage(site.py:164)
        at <python>.site.addsitedir(site.py:208)
        at <python>.java.android.importer.initialize_importlib(importer.py:78)
        at <python>.java.android.importer.initialize(importer.py:36)
        at <python>.java.android.initialize(__init__.py:14)
        at <python>.chaquopy_java.call(chaquopy_java.pyx:380)
        at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:352)
        at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
        at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
        at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
        at com.chaquo.python.android.AndroidPlatform.onStart(AndroidPlatform.java:132)
        at com.chaquo.python.Python.start(Python.java:54)
        at com.example.jhrembg.MainActivity.onCreate(MainActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-07-01 16:13:36.995 4986-4986/com.example.jhrembg I/Process: Sending signal. PID: 4986 SIG: 9
       ndk {
            abiFilters "armeabi-v7a","x86"
        }
        python {

            buildPython "C:/Users/user/anaconda3/envs/torch/python.exe"
            pip
                    {
                        install "opencv-contrib-python-headless"
                        install "pillow"
                        install "numpy"
                        install "torch"
                        install "torchvision"
                        install "scikit-image"
                    }
        }
        sourceSets {
            main {
                python.srcDir "src/main/python"
            }
        }
@mhsmith
Copy link
Member

mhsmith commented Jul 2, 2021

As it says in #307, ".pth" files in the top-level source directory will be treated as path configuration files. If this is intentional, please explain what you're trying to do. Or if the file contains something else like a PyTorch model, you'll have to either rename it to have a different extension, or put it in a subdirectory rather than in the source code root.

@Josonlchui
Copy link
Author

I have put the .pth file in subdirectory and it is the result of pytorch model training and cannot rename. I just wonder it worked at the beginning, but suddenly "Python.start(new AndroidPlatform(this)) crash". Anyway, is there any code I can use to kill Python and re-start again? thx.

@Josonlchui
Copy link
Author

I just find out I need to change .pth file to pytouch model to load into mobile, so the issue has been solved.
Moreover, can you guide me how to kill Python and re-start the process again. thx.

@mhsmith
Copy link
Member

mhsmith commented Jul 4, 2021

If you're generating the .pth file at runtime, then that would explain why it works the first time you start the process but crashes the second time.

However, as it says in the documentation, you shouldn't write any files to this location at runtime because they may be deleted when the app is upgraded. Instead, write files under os.environ["HOME"], which points to your app's internal storage directory.

You can stop the process in the usual way using System.exit in Java, or sys.exit in Python. However, in some circumstances Android will automatically restart it, as described here.

@mhsmith mhsmith closed this as completed Jul 9, 2021
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

2 participants