你好,我做了一个小应用程序来检测物体。 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();
}
});
}});}