I am using a tflite model and I am trying to draw a RectF on the image where the object is detected.
Here is my CameraActivity.java class where I am detecting the object.
predictBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img = Bitmap.createScaledBitmap(img, 128, 128, true);
try {
DrawView drawView = new DrawView(getApplicationContext());
Android model = Android.newInstance(getApplicationContext());
// Creates inputs for reference.
TensorImage image = TensorImage.fromBitmap(img);
// Runs model inference and gets result.
Android.Outputs outputs = model.process(image);
Android.DetectionResult detectionResult = outputs.getDetectionResultList().get(0);
// Gets result from DetectionResult.
float score = detectionResult.getScoreAsFloat();
RectF location = detectionResult.getLocationAsRectF();
String category = detectionResult.getCategoryAsString();
// Releases model resources if no longer used.
mCanvas.drawBoundingBox();
model.close();
// here we will print out the results of the object to text views based on the image that is inputted by the user
// we print out object type and its accuracy score
mCanvasView.drawRect();
objecttv.setText(category);
scoretv.setText(Float.toString(score));
} catch (IOException e) {
// TODO Handle the exception
}
}
}
);
Here is my DrawView.java class
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawView extends View {
Paint boxPaint;
public static Canvas mCanvas;
public DrawView(Context context) {
super(context);
boxPaint = new Paint();
}
public void drawBoundingBox() {
// Refresh the view by calling onDraw function
invalidate();
}
public void onDraw(Canvas canvas, AttributeSet attrs) {
// Draw what you want
boxPaint.setColor(Color.RED);
boxPaint.setAlpha(200);
boxPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(location, boxPaint);
}
}
In my activity_camera.xml , I added the drawview to match the image I am uploading.
<com.example.aPROJECT.DrawView
android:id="@+id/canvasView"
android:layout_width="281dp"
android:layout_height="324dp"
android:layout_marginTop="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="281dp"
android:layout_height="324dp"
android:layout_marginTop="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_gallery"
tools:ignore="ImageContrastCheck" />
In the CameraActivity.java , on line mCanvas.drawBoundingBox(); it cannot access the mCanvas from the DrawView.java class.
On line mCanvasView.drawRect(); it cannot resolve the symbol .
What am I missing here?