Я работаю над проектом Android Studio, в котором использую пользовательскую модель, завернутую в Google ML Kit, для обнаружения объектов в режиме реального времени. Однако я столкнулся с проблемой, из-за которой ограничивающие рамки, возвращаемые моделью, не соответствуют предварительному просмотру, отображаемому на экране. Хотя этикетки кажутся точными. Входное изображение имеет размер 1920 x 1920, а разрешение предварительного просмотра – 2239 x 1080. Вот моя функция:
private void BindPreview(ProcessCameraProvider CameraProvider) {
speaker.speakText("Please hold phone in front of you to detect obstacles");
preview = new Preview.Builder()
.setTargetResolution(new Size(1920, 1920))
.build();
cameraSelector = new CameraSelector.Builder().requireLensFacing(camFacing).build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
imageAnalysis = new ImageAnalysis.Builder()
.setTargetResolution(new Size(1920, 1920))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build();
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this),
new ImageAnalysis.Analyzer() {
@ExperimentalGetImage
@Override
public void analyze(@NonNull ImageProxy imageProxy) {
Image image = imageProxy.getImage();
if (image != null) {
InputImage inputImage = InputImage.fromMediaImage(image, imageProxy.getImageInfo().getRotationDegrees());
Task task = objectDetector.process(inputImage);
task.addOnSuccessListener(
new OnSuccessListener() {
@Override
public void onSuccess(List detectedObjects) {
if (!detectedObjects.isEmpty()) {
objectQueue.addAll(detectedObjects);
Matrix mappingMatrix = ProjectHelper.getMappingMatrix(imageProxy, previewView);
for (DetectedObject object : detectedObjects) {
Rect boundingBox = ProjectHelper.mapBoundingBox(object.getBoundingBox(), mappingMatrix);
rectangleOverlayView.updateRect(boundingBox);
rectangleOverlayView.invalidate();
}
}
}
}
)
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("Object Detection", e.getMessage());
}
}
)
.addOnCompleteListener(
new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
imageProxy.close();
image.close();
}
}
);
}
}
});
CameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, imageAnalysis, preview);
}
Я получил функцию getMappingMatrix из другого вопроса о переполнении стека, но, похоже, она не очень помогла. Вот эта функция:
public static Matrix getMappingMatrix(ImageProxy imageProxy, PreviewView previewView) {
Rect cropRect = imageProxy.getCropRect();
int rotationDegrees = imageProxy.getImageInfo().getRotationDegrees();
Matrix matrix = new Matrix();
// A float array of the source vertices (crop rect) in clockwise order.
float[] source = {
cropRect.left,
cropRect.top,
cropRect.right,
cropRect.top,
cropRect.right,
cropRect.bottom,
cropRect.left,
cropRect.bottom
};
// A float array of the destination vertices in clockwise order.
float[] destination = {
0f,
0f,
previewView.getWidth(),
0f,
previewView.getWidth(),
previewView.getHeight(),
0f,
previewView.getHeight()
};
// The destination vertexes need to be shifted based on rotation degrees.
// The rotation degree represents the clockwise rotation needed to correct
// the image.
// Each vertex is represented by 2 float numbers in the vertices array.
int vertexSize = 2;
// The destination needs to be shifted 1 vertex for every 90° rotation.
int shiftOffset = rotationDegrees / 90 * vertexSize;
float[] tempArray = destination.clone();
for (int toIndex = 0; toIndex < source.length; toIndex++) {
int fromIndex = (toIndex + shiftOffset) % source.length;
destination[toIndex] = tempArray[fromIndex];
}
matrix.setPolyToPoly(source, 0, destination, 0, 4);
return matrix;
}
Я использую модель, рекомендованную документацией ML Kit.
Классификатор изображений EfficientNet-Lite
Вот мой onCreate функция:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_detection);
LocalModel localModel =
new LocalModel.Builder()
.setAssetFilePath("2.tflite")
.build();
CustomObjectDetectorOptions customObjectDetectorOptions =
new CustomObjectDetectorOptions.Builder(localModel)
.setDetectorMode(CustomObjectDetectorOptions.STREAM_MODE)
.enableClassification()
.setClassificationConfidenceThreshold(0.5f)
.setMaxPerObjectLabelCount(1)
.build();
objectDetector = ObjectDetection.getClient(customObjectDetectorOptions);
previewView = findViewById(R.id.cameraPreview);
context = this;
rectangleOverlayView = findViewById(R.id.rectangle_overlay);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
if(ContextCompat.checkSelfPermission(ObjectDetectionActivity.this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
activityResultLauncher.launch(Manifest.permission.CAMERA);
} else{
BindPreview(cameraProvider);
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace(); // Handle exceptions as needed
Log.e("CamerX Camera Provider", e.getMessage());
}
}, ContextCompat.getMainExecutor(this));
}
Подробнее здесь: https://stackoverflow.com/questions/781 ... -boxes-mis
Android Studio: пользовательская модель с набором ML, возвращающая неправильные ограничивающие рамки, несовпадения с пре ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как преобразовать X AnyLabeling JSON в ограничивающие рамки, ориентированные на YOLOv8
Anonymous » » в форуме Python - 0 Ответы
- 35 Просмотры
-
Последнее сообщение Anonymous
-