我面对带有mlkit对象检测的java.lang.OutOfMemoryError

时间:2019-06-06 23:05:48

标签: android object-detection firebase-mlkit

你好,我做了一个小应用程序来检测物体。 Wen,我打开应用程序,它运行正常,并且我检测到消息对象。 但是后来我得到了这个例外

  

E / AndroidRuntime:致命例外:FrameProcessorsWorker进程:detection.plaque.plate_detection_vision,PID:24423 java.lang.OutOfMemoryError:无法分配3133452字节分配,其中包含544616可用字节和531KB,直到java.nio.ByteBuffer的OOM。侦测到com.google.firebase.ml.vision.common.FirebaseVisionImage.zznm(未知来源)时在com.google.firebase.ml.vision.objects.FirebaseVisionObjectDetector.processImage(未知来源)处分配(ByteBuffer.java:56) com.otaliastudios.cameraview.CameraView $ Callbacks $ 11.run(CameraView.java:1830)的android.os.Handler.handleCallback(Handler.java:739)的.plaque.plate_detection_vision.MainActivity $ 1.process(MainActivity.java:65) )的android.os.HandlerThread.run(HandlerThread.java:61)的android.os.Looper.loop(Looper.java:155)的android.os.Handler.dispatchMessage(Handler.java:95)

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 个答案:

答案 0 :(得分:0)

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

enter image description here