chouaibMo / chouaibMo/MLKit-Jetpack-Compose

Face mesh aspect is distorted when drawn over preview view (width is too narrow)

Open
#1 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Kotlin
Stars
37
Forks
8
PR merge metrics
No merged PRs in 30d

Description

Your code does not correctly map the face mesh points from the analysis aspect ratio to the preview aspect ratio for both the rear and front cameras. This new adjustPoint() method below will properly handle both front and rear cameras and in both cases, the mesh will be the exact correct size and perfectly aligned over the user's face. I've also included a new adjustSize that will ensure that the bounding box is scaled properly for both cameras and is aligned over the mesh for the front camera. This is just a quick fix. In my version, I will be optimizing this code.

```
fun adjustPoint(
point: PointF,
imageWidth: Int,
imageHeight: Int,
screenWidth: Int,
screenHeight: Int,
isFrontCamera: Boolean = true
): PointF {
val imageAspectRatio = imageWidth.toFloat() / imageHeight
val screenAspectRatio = screenWidth.toFloat() / screenHeight

val scaleFactor = if (imageAspectRatio > screenAspectRatio) {
// Width is the limiting factor.
screenHeight.toFloat() / imageHeight
} else {
// Height is the limiting factor.
screenWidth.toFloat() / imageWidth
}

// Calculate the horizontal offset to center the mesh.
val offsetX = (screenWidth - (imageWidth * scaleFactor)) / 2

// Adjust x-coordinate for mirroring if using the front camera.
val x = if (isFrontCamera) {
screenWidth - ((point.x * scaleFactor) + offsetX) // Mirror the x-coordinate
} else {
(point.x * scaleFactor) + offsetX
}

val y = point.y * scaleFactor // No change in the y scaling

return PointF(x, y)
}

fun adjustSize(
size: Size,
imageWidth: Int,
imageHeight: Int,
screenWidth: Int,
screenHeight: Int,
isFrontCamera: Boolean = true
): Size {
val imageAspectRatio = imageWidth.toFloat() / imageHeight
val screenAspectRatio = screenWidth.toFloat() / screenHeight

val scaleFactor = if (imageAspectRatio > screenAspectRatio) {
// Width is the limiting factor.
screenHeight.toFloat() / imageHeight
} else {
// Height is the limiting factor.
screenWidth.toFloat() / imageWidth
}

// Adjust x-coordinate for mirroring if using the front camera.
val width = if (isFrontCamera) {
size.width * -scaleFactor/// imageWidth * screenWidth
} else {
size.width * scaleFactor/// imageWidth * screenWidth
}

val height = size.height * scaleFactor/// imageHeight * screenHeight
return Size(width, height)
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.