使用 chaquopy 时,应用程序在设备中崩溃,但在模拟器中没有

Posted

技术标签:

【中文标题】使用 chaquopy 时,应用程序在设备中崩溃,但在模拟器中没有【英文标题】:app crashes in device but not in emulator while using chaquopy 【发布时间】:2021-05-31 03:04:21 【问题描述】:

我在 android studio 中使用 chaquopy 创建了人脸检测。当我将应用程序运行到模拟器中时,它可以正常工作。但是当我打开手机时,它崩溃了。在运行中显示

I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
I/Toast: Show toast from OpPackageName:com.example.chaquopy, PackageName:com.example.chaquopy
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@57230c5
I/Adreno: BLTLIB: Semaphore Timeout, disabling MT
I/Adreno: BLTLIB: Semaphore Timeout count 1
I/Adreno: BLTLIB: Semaphore Timeout count 2
I/Adreno: BLTLIB: Semaphore Timeout count 3
I/Adreno: BLTLIB: Semaphore Timeout count 4
I/Adreno: BLTLIB: Semaphore Timeout count 5
I/Adreno: BLTLIB: Semaphore Timeout count 6
I/Adreno: BLTLIB: Semaphore Timeout count 7
I/art: Waiting for a blocking GC Alloc
I/art: Starting a blocking GC Alloc
    Starting a blocking GC Alloc
I/art: Alloc partial concurrent mark sweep GC freed 220(9KB) AllocSpace objects, 1(480KB) LOS objects, 8% free, 171MB/187MB, paused 262us total 14.558ms
    Starting a blocking GC Alloc
I/art: Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 27(752B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 237us total 25.550ms
    Forcing collection of SoftReferences for 35MB allocation
    Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 2(48B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 230us total 24.816ms
W/art: Throwing OutOfMemoryError "Failed to allocate a 36742016 byte allocation with 16777216 free bytes and 20MB until OOM"
I/art: Starting a blocking GC Alloc
    Starting a blocking GC Alloc
I/art: Starting a blocking GC Alloc
I/art: Alloc partial concurrent mark sweep GC freed 4(96B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 430us total 22.384ms
    Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 2(48B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 333us total 35.743ms
    Forcing collection of SoftReferences for 35MB allocation
    Starting a blocking GC Alloc
I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 249us total 28.176ms
    Starting a blocking GC HomogeneousSpaceCompact
I/art: HomogeneousSpaceCompact marksweep + semispace GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 8% free, 171MB/187MB, paused 29.551ms total 29.551ms
W/art: Throwing OutOfMemoryError "Failed to allocate a 36742016 byte allocation with 16777216 free bytes and 20MB until OOM"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chaquopy, PID: 32703
    java.lang.OutOfMemoryError: Failed to allocate a 36742016 byte allocation with 16777216 free bytes and 20MB until OOM
        at java.lang.StringFactory.newStringFromChars(StringFactory.java:218)
        at java.lang.StringFactory.newStringFromBytes(StringFactory.java:203)
        at java.lang.StringFactory.newStringFromBytes(StringFactory.java:63)
        at android.util.Base64.encodeToString(Base64.java:456)
        at com.example.chaquopy.MainActivity.getStringImage(MainActivity.java:102)
        at com.example.chaquopy.MainActivity.access$000(MainActivity.java:31)
        at com.example.chaquopy.MainActivity$1.onClick(MainActivity.java:76)
        at android.view.View.performClick(View.java:5619)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View$PerformClick.run(View.java:22298)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
W/AndroidRuntime: finished raiseRlimit, rlim_cur:4096  rlim_max:4096

我的代码是

 Submitbtn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Toast.makeText(MainActivity.this,"Submit Button is clicked",Toast.LENGTH_SHORT).show();
                //by clicking get image from image view
                bitmapDrawable = (BitmapDrawable) selectedImage.getDrawable();
                bitmap = bitmapDrawable.getBitmap();
                imageString = getStringImage(bitmap);
                //in imageString we get encoded image
                //passing the string into python script
                PyObject pyObject = py.getModule("scripts");
                PyObject obj = pyObject.callAttr("main", imageString);
                //converted to string
                String string = obj.toString();
                //converted to byte array
                byte data[] = android.util.Base64.decode(string,0);
                //converted to bitmap
                Bitmap returnBitmap = BitmapFactory.decodeByteArray(data,0,data.length);

                //now set bitmap to image view
                selectedImage.setImageBitmap(returnBitmap);

            
        );

private String getStringImage(Bitmap bitmap) 
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
        //storing in byte array
        byte[] imageBytes = byteArrayOutputStream.toByteArray();
        //encoding string
        String encodedImage = android.util.Base64.encodeToString(imageBytes, 0);
        return encodedImage;
    

在我的代码中,当用户单击 submitbtn 时,来自 imageview 的图像将编码为字符串并发送到 python 模块。然后在进行面部检测后的python脚本中,它再次返回字符串,剩下的主要是我做的。 我能做什么?

【问题讨论】:

【参考方案1】:

这里不需要使用 Base64。只需将byte[] 数组直接传递给Python,然后使用this FAQ 中显示的方法之一在Python 中接收它。

同样,从 Python 返回图像时,您应该返回一个bytes 对象,并使用toJava 将其转换为byte[] 数组,如chaquopy-matplotlib 示例所示。

另外,如果您的图片是 36 MB 的 PNG 格式,那么我认为它是高分辨率照片,因此您应该改用 JPG 格式。

【讨论】:

以上是关于使用 chaquopy 时,应用程序在设备中崩溃,但在模拟器中没有的主要内容,如果未能解决你的问题,请参考以下文章

在 android studio 中使用 chaquopy 运行 python 脚本

如何在使用 Chaquopy 时在 android 中显示加载动画

Chaquopy 不支持 AIML

Android Studio 无法使用 chaquopy python 打开 COM 端口

使用 chaquopy 在 android studio 中集成 python 代码(对象检测代码)

如何使用 chaquopy 在颤振中包含 python .py 文件?