Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions to draw detection results on android Canvas #450

Merged
merged 3 commits into from
Sep 23, 2022

Conversation

juliabeliaeva
Copy link
Contributor

@juliabeliaeva juliabeliaeva commented Sep 16, 2022

This PR includes:

  1. Utility functions to draw detected objects on Canvas.
  2. Function getPreviewImageBounds to calculate the correct coordinates and dimensions of the preview image to use for drawing.
  3. Base class for View implementations which could be used for visualization.

Sample usage code:

class DetectorView(context: Context, attrs: AttributeSet) : DetectorViewBase<Result>(context, attrs) {
    private val objectPaint = ...
    private val textPaint = ...

    private var bounds: PreviewImageBounds? = null

    override fun onDetectionSet(detection: Result?) {
        bounds = detection?.let {
            getPreviewImageBounds(it.width, it.height, width, height, ScaleType.FILL_CENTER) 
            // important to use the same scale type as set in the target preview
        }
    }

    override fun Canvas.drawDetection(detection: Result) {
        val currentBounds = bounds ?: PreviewImageBounds(0f, 0f, width.toFloat(), height.toFloat())
        drawObject(detection.detection, currentBounds, objectPaint, textPaint)
    }
}

This view can be added along to the camera preview so it would draw its content on top of it:

<androidx.constraintlayout.widget.ConstraintLayout 
    //...
    <androidx.camera.view.PreviewView
            android:id="@+id/view_finder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:scaleType="fillCenter"/>
    <DetectorView
            android:id="@+id/detector_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

@juliabeliaeva juliabeliaeva marked this pull request as draft September 19, 2022 12:24
@juliabeliaeva juliabeliaeva marked this pull request as ready for review September 20, 2022 23:25
@ermolenkodev ermolenkodev merged commit dc97543 into Kotlin:master Sep 23, 2022
@juliabeliaeva juliabeliaeva deleted the android-visualization branch December 27, 2022 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants