提问者:小点点

我面临一个java. lang.OutOfMemoryError与mlkit目标检测


你好,我做了一个小应用程序来检测对象。温我打开应用程序它工作正常,我检测到消息对象。但后来我得到了这个异常

plate_detection_vision,PID: 24423 java.lang.OutOfMemoryError:未能分配3133452字节分配544616空闲字节和531KB,直到OOM在java.nio.ByteBuffer.分配(ByteBuffer.java:56)在com.google.firebase.ml.vision.object.FirebaseVisionImage.zznm(未知来源)。FirebaseVisionObjectDetector.process Image(未知来源)plate_detection_vision.MainActivity1 dolar.process(MainActivity.java:65)在com.otaliastudios.Cameron aView.Camera$回调$11.run(CameraView.java:1830)在android.os.Handler.handleCallback(Handler.java:739)在android.os.Handler.调度消息(Handler.java:95)在android.os.Loopo.loop(Looper.java:155)在android.os.HandlerThread.run(HandlerThread.java:61)

package detection.plaque.plate_detection_vision;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.WorkerThread;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.Surface;
import android.widget.Toast;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.FirebaseApp;
import com.google.firebase.ml.vision.FirebaseVision;
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata;
import com.google.firebase.ml.vision.objects.FirebaseVisionObject;
import com.google.firebase.ml.vision.objects.FirebaseVisionObjectDetector;
import com.google.firebase.ml.vision.objects.FirebaseVisionObjectDetectorOptions;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.FrameProcessor;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CameraView cameraView;
    private String TAG;
    Context tt=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraView =findViewById(R.id.cameraView);
        cameraView.setLifecycleOwner(this);
        FirebaseApp.initializeApp(this);
        cameraView.addFrameProcessor(new FrameProcessor() {
            @Override
            @WorkerThread
            public void process(@NonNull com.otaliastudios.cameraview.Frame frame) {
                byte[] data = frame.getData();
                FirebaseVisionImageMetadata imageMetadata = new FirebaseVisionImageMetadata.Builder()
                        .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
                        .setHeight(frame.getSize().getHeight())
                        .setWidth(frame.getSize().getWidth())
                        //.setRotation(90)
                        .build();
                FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(data, imageMetadata);
                FirebaseVisionObjectDetectorOptions options = new FirebaseVisionObjectDetectorOptions.Builder()
                        .setDetectorMode(FirebaseVisionObjectDetectorOptions.STREAM_MODE)
                        .enableMultipleObjects()
                        .enableClassification()
                        .build();
                FirebaseVisionObjectDetector objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(options);
                objectDetector.processImage(image)
                        .addOnSuccessListener(
                                new OnSuccessListener<List<FirebaseVisionObject>>() {
                                    @Override
                                    public void onSuccess(List<FirebaseVisionObject> detectedObjects) {

                                        for (FirebaseVisionObject obj : detectedObjects) {
                                            Integer id = obj.getTrackingId();
                                            Rect bounds = obj.getBoundingBox();
                                            int kat = obj.getClassificationCategory();
                                            Toast.makeText(tt, "object detected",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                })
                        .addOnFailureListener(
                                new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Toast.makeText(tt, "Oops, something went wrong!",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                });
}});}

共1个答案

匿名用户

我通过增加分配给Gradle守护进程的内存解决了类似的问题VM在Android Studio的项目视图下的gradle. settings文件中。