chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
748 stars 127 forks source link

Fail to import Ultralytics in runtime #1107

Closed Markshilong closed 3 months ago

Markshilong commented 3 months ago

Chaquopy version

"15.0.1"

Devices or emulators where the issue happens

OnePlus CPH2417

Relevant parts of your code

test.py

from os.path import dirname, join
from ultralytics import YOLO

def func():
    return "success"

MainActivity.java

package com.example.shilongbutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button; // Import Button
import android.widget.TextView;
import android.widget.Toast;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //
        // "context" must be an Activity, Service or Application object from your app.
        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }

        Python py = Python.getInstance();
        PyObject pyObj = py.getModule("test"); // Name of the Python file

        // Corrected: Use findViewById directly as you're already in an Activity context
        Button toastButton = findViewById(R.id.button); // Cast the found view to Button
        toastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast myToast1 = Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT);
//                myToast1.show();

                PyObject result = pyObj.callAttr("func");

                Toast myToast2 = Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_SHORT);
                myToast2.show();
            }
        });
    }
}

Describe your issue

I have successfully build the app using:

chaquopy {
    defaultConfig {
        version = "3.8"
        // for .pyc
        buildPython("/Users/mark/miniconda3/envs/supervision/bin/python")

        pip {
            install("ultralytics==8.0.16")
            install("seaborn==0.12.2")
        }
    }
}

But every time when I open the app, it crashes instantly. From logcat, I can see it crashes when running from ultralytics import YOLO. How to fix this?

Here is my logcat:

---------------------------- PROCESS STARTED (11458) for package com.example.shilongbutton ----------------------------
2024-03-07 15:21:37.382  2829-2998  OplusThermalStats       pid-2829                             E  Error getting package info: com.example.shilongbutton
2024-03-07 15:21:37.385 11458-11458 Compatibil...geReporter com.example.shilongbutton            D  Compat change id reported: 171979766; UID 10266; state: ENABLED
2024-03-07 15:21:37.385 11458-11458 oplus.andr...actoryImpl com.example.shilongbutton            I  get feature:IOplusAutoResolutionFeature
2024-03-07 15:21:37.385 11458-11458 oplus.andr...actoryImpl com.example.shilongbutton            I  getOplusAutoResolutionFeature
2024-03-07 15:21:37.385 11458-11458 CompactWindowAppManager com.example.shilongbutton            D  initCompactApplicationInfo 
2024-03-07 15:21:37.393 11458-11458 libc                    com.example.shilongbutton            W  Access denied finding property "ro.odm.prev.product.name"
2024-03-07 15:21:37.395 11458-11458 OplusFeatu...figManager com.example.shilongbutton            D  Milliseconds spent on initOplusFeatureCache(): 1
2024-03-07 15:21:37.397 11458-11458 LoadedApk               com.example.shilongbutton            D  mApplicationInfo overrideDisplayId:null
2024-03-07 15:21:37.397 11458-11458 ziparchive              com.example.shilongbutton            W  Unable to open '/data/app/~~17H1Wbnsd7bvKzR_gBvK9A==/com.example.shilongbutton-cNbSzyv5N2b7Ru7dvefhMg==/base.dm': No such file or directory
2024-03-07 15:21:37.397 11458-11458 ziparchive              com.example.shilongbutton            W  Unable to open '/data/app/~~17H1Wbnsd7bvKzR_gBvK9A==/com.example.shilongbutton-cNbSzyv5N2b7Ru7dvefhMg==/base.dm': No such file or directory
2024-03-07 15:21:37.468 11458-11458 nativeloader            com.example.shilongbutton            D  Configuring clns-4 for other apk /data/app/~~17H1Wbnsd7bvKzR_gBvK9A==/com.example.shilongbutton-cNbSzyv5N2b7Ru7dvefhMg==/base.apk. target_sdk_version=34, uses_libraries=, library_path=/data/app/~~17H1Wbnsd7bvKzR_gBvK9A==/com.example.shilongbutton-cNbSzyv5N2b7Ru7dvefhMg==/lib/arm64:/data/app/~~17H1Wbnsd7bvKzR_gBvK9A==/com.example.shilongbutton-cNbSzyv5N2b7Ru7dvefhMg==/base.apk!/lib/arm64-v8a, permitted_path=/data:/mnt/expand:/data/user/0/com.example.shilongbutton
2024-03-07 15:21:37.479 11458-11458 GraphicsEnvironment     com.example.shilongbutton            V  ANGLE Developer option for 'com.example.shilongbutton' set to: 'default'
2024-03-07 15:21:37.479 11458-11458 GraphicsEnvironment     com.example.shilongbutton            V  ANGLE GameManagerService for com.example.shilongbutton: false
2024-03-07 15:21:37.479 11458-11458 GraphicsEnvironment     com.example.shilongbutton            V  App is not on the allowlist for updatable production driver.
2024-03-07 15:21:37.481 11458-11458 NetworkSecurityConfig   com.example.shilongbutton            D  No Network Security Config specified, using platform default
2024-03-07 15:21:37.482 11458-11458 NetworkSecurityConfig   com.example.shilongbutton            D  No Network Security Config specified, using platform default
2024-03-07 15:21:37.508 11458-13037 e.shilongbutto          com.example.shilongbutton            E  Loaded layer handle (9823924826946661709) for layer /my_product/lib64/libcolorx-loader.so
2024-03-07 15:21:37.508 11458-13037 e.shilongbutto          com.example.shilongbutton            E  Looking for entrypoint ColorX_Check
2024-03-07 15:21:37.508 11458-13037 e.shilongbutto          com.example.shilongbutton            E  Found ColorX_Check for /my_product/lib64/libcolorx-loader.so
2024-03-07 15:21:37.508 11458-13037 e.shilongbutto          com.example.shilongbutton            E  Check failed
2024-03-07 15:21:37.522 11458-11458 MultiApp.Impl           com.example.shilongbutton            E  OplusMultiAppImpl
2024-03-07 15:21:37.523 11458-11458 AppCompatDelegate       com.example.shilongbutton            D  Checking for metadata for AppLocalesMetadataHolderService : Service not found
2024-03-07 15:21:37.544 11458-11458 oplus.andr...actoryImpl com.example.shilongbutton            I  Unknow feature:IOplusTextViewRTLUtilForUG
2024-03-07 15:21:37.565 11458-11458 e.shilongbutto          com.example.shilongbutton            W  Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
2024-03-07 15:21:37.565 11458-11458 e.shilongbutto          com.example.shilongbutton            W  Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
2024-03-07 15:21:37.577 11458-11458 OplusCusto...ionManager com.example.shilongbutton            E  sInstance is null, start a new sInstance
2024-03-07 15:21:37.611 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7411): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/zlib.so" dev="dm-17" ino=15361 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.627 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7412): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/java/chaquopy.so" dev="dm-17" ino=30588 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.631 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7413): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/_ctypes.so" dev="dm-17" ino=23027 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.635 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7414): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/_struct.so" dev="dm-17" ino=15418 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.643 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7415): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/_bz2.so" dev="dm-17" ino=29011 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.643 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7416): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/_lzma.so" dev="dm-17" ino=29383 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.655 11458-11458 e.shilongbutton         com.example.shilongbutton            W  type=1400 audit(0.0:7417): avc: granted { execute } for path="/data/data/com.example.shilongbutton/files/chaquopy/bootstrap-native/arm64-v8a/math.so" dev="dm-17" ino=29013 scontext=u:r:untrusted_app:s0:c10,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c10,c257,c512,c768 tclass=file app=com.example.shilongbutton
2024-03-07 15:21:37.812 11458-11458 asset                   com.example.shilongbutton            W  seek out of range: want -20, end=22
2024-03-07 15:21:37.898  4439-4505  AuthDialogHelper        pid-4439                             E  CheckNotificationCanBeSetSecond: check com.example.shilongbutton , Notification master switch on, unable to pop dialog
2024-03-07 15:21:38.378 11458-11458 AndroidRuntime          com.example.shilongbutton            D  Shutting down VM
2024-03-07 15:21:38.380 11458-11458 AndroidRuntime          com.example.shilongbutton            E  FATAL EXCEPTION: main
                                                                                                    Process: com.example.shilongbutton, PID: 11458
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shilongbutton/com.example.shilongbutton.MainActivity}: com.chaquo.python.PyException: ImportError: Failed to initialize: Bad git executable.
                                                                                                    The git executable must be specified in one of the following ways:
                                                                                                        - be included in your $PATH
                                                                                                        - be set via $GIT_PYTHON_GIT_EXECUTABLE
                                                                                                        - explicitly set via git.refresh()

                                                                                                    All git commands will error until this is rectified.

                                                                                                    This initial message can be silenced or aggravated in the future by setting the
                                                                                                    $GIT_PYTHON_REFRESH environment variable. Use one of the following values:
                                                                                                        - quiet|q|silence|s|silent|none|n|0: for no message or exception
                                                                                                        - warn|w|warning|log|l|1: for a warning message (logged at level CRITICAL, displayed by default)
                                                                                                        - error|e|exception|raise|r|2: for a raised exception

                                                                                                    Example:
                                                                                                        export GIT_PYTHON_REFRESH=quiet

                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3788)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3950)
                                                                                                        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:109)
                                                                                                        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:2346)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:238)
                                                                                                        at android.os.Looper.loop(Looper.java:349)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8241)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
                                                                                                    Caused by: com.chaquo.python.PyException: ImportError: Failed to initialize: Bad git executable.
                                                                                                    The git executable must be specified in one of the following ways:
                                                                                                        - be included in your $PATH
                                                                                                        - be set via $GIT_PYTHON_GIT_EXECUTABLE
                                                                                                        - explicitly set via git.refresh()

                                                                                                    All git commands will error until this is rectified.

                                                                                                    This initial message can be silenced or aggravated in the future by setting the
                                                                                                    $GIT_PYTHON_REFRESH environment variable. Use one of the following values:
                                                                                                        - quiet|q|silence|s|silent|none|n|0: for no message or exception
                                                                                                        - warn|w|warning|log|l|1: for a warning message (logged at level CRITICAL, displayed by default)
                                                                                                        - error|e|exception|raise|r|2: for a raised exception

                                                                                                    Example:
                                                                                                        export GIT_PYTHON_REFRESH=quiet

                                                                                                        at <python>.git.<module>(__init__.py:142)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.yolo.utils.<module>(__init__.py:17)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.nn.autobackend.<module>(autobackend.py:15)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.yolo.engine.predictor.<module>(predictor.py:34)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.yolo.v8.classify.predict.<module>(predict.py:5)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.yolo.v8.classify.<module>(__init__.py:3)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.yolo.v8.<module>(__init__.py:3)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.yolo.<module>(__init__.py:3)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.ultralytics.<module>(__init__.py:5)
                                                                                                        at <python>.java.chaquopy.import_override(import.pxi:60)
                                                                                                        at <python>.test.<module>(test.py:4)
                                                                                                        at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
                                                                                                        at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:843)
                                                                                                        at <python>.java.android.importer.exec_module(importer.py:634)
2024-03-07 15:21:38.380 11458-11458 AndroidRuntime          com.example.shilongbutton            E      at <python>.java.android.importer.exec_module(importer.py:721)
                                                                                                        at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
                                                                                                        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
                                                                                                        at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
                                                                                                        at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
                                                                                                        at <python>.importlib.import_module(__init__.py:127)
                                                                                                        at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModuleNative(chaquopy_java.pyx:129)
                                                                                                        at com.chaquo.python.Python.getModuleNative(Native Method)
                                                                                                        at com.chaquo.python.Python.getModule(Python.java:84)
                                                                                                        at com.example.shilongbutton.MainActivity.onCreate(MainActivity.java:29)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8145)
                                                                                                        at android.app.Activity.performCreate(Activity.java:8125)
                                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1344)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3754)
                                                                                                        ... 12 more
2024-03-07 15:21:38.390 11458-11458 Process                 com.example.shilongbutton            I  Sending signal. PID: 11458 SIG: 9
---------------------------- PROCESS ENDED (11458) for package com.example.shilongbutton ----------------------------
mhsmith commented 3 months ago

See #1106, and in future please search for existing issues before creating new ones.