I've got a simple layout:
<RelativeLayout
android:id="@+id/myP"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.camera.view.PreviewView
android:id="@+id/mPreviewView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.example.dochjavatestimplementation.pkgActivity.ExtendedImageView
android:id="@+id/imageViewOmgShowIt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</RelativeLayout>
The previewView is used to display the camera, the ExtendedImageView is used to display the display the found object rect.
My ImageAnalyzer:
imageAnalysis = new ImageAnalysis.Builder()
.setTargetResolution(new Size(mPreviewView.getWidth(),mPreviewView.getHeight()))
.build();
imageAnalysis.setAnalyzer(executor, new PaperImageAnalyser());
PaperImageAnalyser:
public class PaperImageAnalyser implements ImageAnalysis.Analyzer {
ObjectDetectorOptions options = new ObjectDetectorOptions.Builder()
.setDetectorMode(ObjectDetectorOptions.STREAM_MODE)
.enableClassification() // Optional
.build();
ObjectDetector objectDetector = ObjectDetection.getClient(options);
@Override
public void analyze(@NonNull ImageProxy imageProxy) {
@SuppressLint("UnsafeExperimentalUsageError") Image mediaImage = imageProxy.getImage();
if (mediaImage != null) {
InputImage image =InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
objectDetector.process(image)
.addOnSuccessListener(
new OnSuccessListener<List<DetectedObject>>() {
@Override
public void onSuccess(List<DetectedObject> detectedObjects) {
for (DetectedObject detectedObject : detectedObjects) {
Rect boundingBox = detectedObject.getBoundingBox();
CameraImp.imageViewOmgShowIt.drawFoundObj(boundingBox);
}
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("error",""+e.getMessage());
}
})
.addOnCompleteListener(new OnCompleteListener<List<DetectedObject>>() {
@SuppressLint("UnsafeExperimentalUsageError")
@Override
public void onComplete(@NonNull Task<List<DetectedObject>> task) {
imageProxy.getImage().close();
imageProxy.close();
}
});
}
}
}
Drawing the rect:
public void drawFoundObj(Rect boundingBox) {
foundYo = boundingBox;
invalidate(); //call draw
}
and
Paint mPaint = new Paint();
Canvas mCanvas = new Canvas();
@Override
protected void onDraw(Canvas canvas) {
//clear exsisting rects
Xfermode x = mPaint.getXfermode();
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaint.setXfermode(x);
mCanvas.drawPaint(mPaint);
//set current rect
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setStrokeWidth(3);
paint.setAlpha(40);
canvas.drawRect(foundYo,paint);
}
The results look like this:
As you can see the issue is that although the object gets somehow recognizes, the coordinates are way wrong compared to the position of the object.
What might the cause be?
Is the resolution of the image I receive from the imageanalyzer smaller so that causes maybe that the coordinates are smaller thus wronger?
Or am I drawing the image wrong?