Skip to content

Commit

Permalink
Merge pull request #57 from Yoonit-Labs/fix/head-movement-definition
Browse files Browse the repository at this point in the history
[Fix] Head movement definition
  • Loading branch information
TeruyaHaroldo authored Apr 1, 2021
2 parents 37f85cd + 5b50add commit ec28300
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ Here we explaining the above gif and how reached the "results". Each "movement"
| Head Direction | Attribute | _v_ < -36° | -36° < _v_ < -12° | -12° < _v_ < 12° | 12° < _v_ < 36° | 36° < _v_ |
| - | - | - | - | - | - | - |
| Vertical | `headEulerAngleX` | Super Down | Down | Frontal | Up | Super Up |
| Horizontal | `headEulerAngleY` | Super Right | Right | Frontal | Left | Super Left |
| Tilt | `headEulerAngleZ` | Super Left | Left | Frontal | Right | Super Right |
| Horizontal | `headEulerAngleY` | Super Left | Left | Frontal | Right | Super Right |
| Tilt | `headEulerAngleZ` | Super Right | Right | Frontal | Left | Super Left |

### KeyError

Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/ai/cyberlabs/yoonit/camerademo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -400,20 +400,20 @@ class MainActivity : AppCompatActivity() {

headHorizontalAngleTextView.text = headEulerAngleY.toString()
headHorizontalTextView.text =
if (headEulerAngleY < -36) "Super Right"
else if (-36 < headEulerAngleY && headEulerAngleY < -12) "Right"
if (headEulerAngleY < -36) "Super Left"
else if (-36 < headEulerAngleY && headEulerAngleY < -12) "Left"
else if (-12 < headEulerAngleY && headEulerAngleY < 12) "Frontal"
else if (12 < headEulerAngleY && headEulerAngleY < 36) "Left"
else if (36 < headEulerAngleY) "Super Left"
else if (12 < headEulerAngleY && headEulerAngleY < 36) "Right"
else if (36 < headEulerAngleY) "Super Right"
else "-"

headTiltAngleTextView.text = headEulerAngleZ.toString()
headTiltTextView.text =
if (headEulerAngleZ < -36) "Super Left"
else if (-36 < headEulerAngleZ && headEulerAngleZ < -12) "Left"
if (headEulerAngleZ < -36) "Super Right"
else if (-36 < headEulerAngleZ && headEulerAngleZ < -12) "Right"
else if (-12 < headEulerAngleZ && headEulerAngleZ < 12) "Frontal"
else if (12 < headEulerAngleZ && headEulerAngleZ < 36) "Right"
else if (36 < headEulerAngleZ) "Super Right"
else if (12 < headEulerAngleZ && headEulerAngleZ < 36) "Left"
else if (36 < headEulerAngleZ) "Super Left"
else "-"
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:text="Left Eye Open:" />
android:text="Left Eye:" />
<TextView
android:id="@+id/leftEyeTextView"
android:layout_width="wrap_content"
Expand All @@ -371,7 +371,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:text="Right Eye Open:" />
android:text="Right Eye:" />
<TextView
android:id="@+id/rightEyeTextView"
android:layout_width="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion yoonit-camera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ dependencies {
implementation "androidx.camera:camera-view:1.0.0-alpha15"

// Facefy
implementation 'com.github.Yoonit-Labs:android-yoonit-facefy:1.0.3'
implementation 'com.github.Yoonit-Labs:android-yoonit-facefy:1.0.4'

// MLKit
implementation "com.google.mlkit:barcode-scanning:16.0.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ class CoordinatesController(
* @param faceDetected the face detected object.
* @param imageWidth the camera image input width.
* @param imageHeight the camera image input height.
* @param rotationDegrees the camera rotation in degrees.
* @return the detection box rect of the face detected. Return empty rect if null or detection box is
* out of the screen.
*/
fun getDetectionBox(
faceDetected: FaceDetected?,
imageWidth: Float,
imageHeight: Float,
rotationDegrees: Float
imageHeight: Float
): RectF {
faceDetected?.let { faceDetected ->
var detectionBox = faceDetected.boundingBox
Expand Down Expand Up @@ -78,14 +76,8 @@ class CoordinatesController(
((this.graphicView.height.toFloat() * imageAspectRatio) - this.graphicView.width.toFloat()) / 2
}

val x = if (rotationDegrees == 90f) {
this.scale(detectionBox.centerX().toFloat(), scaleFactor) - postScaleWidthOffset
} else {
this.graphicView.width - (this.scale(detectionBox.centerX().toFloat(),
scaleFactor) - postScaleWidthOffset)
}
val y =
this.scale(detectionBox.centerY().toFloat(), scaleFactor) - postScaleHeightOffset
val x = this.scale(detectionBox.centerX().toFloat(), scaleFactor) - postScaleWidthOffset
val y = this.scale(detectionBox.centerY().toFloat(), scaleFactor) - postScaleHeightOffset

val left = x - this.scale(detectionBox.width() / 2.0f, scaleFactor)
val top = y - this.scale(detectionBox.height() / 2.0f, scaleFactor)
Expand Down Expand Up @@ -165,8 +157,7 @@ class CoordinatesController(
fun getFaceContours(
contours: MutableList<PointF>,
imageWidth: Float,
imageHeight: Float,
rotationDegrees: Float
imageHeight: Float
): MutableList<PointF> {
if (imageHeight <= 0 || imageWidth <= 0) {
return mutableListOf()
Expand Down Expand Up @@ -195,14 +186,8 @@ class CoordinatesController(
val faceContours = mutableListOf<PointF>()

contours.forEach { point ->
val x = if (rotationDegrees == 90f) {
this.scale(point.x, scaleFactor) - postScaleWidthOffset
} else {
this.graphicView.width - (this.scale(point.x, scaleFactor) - postScaleWidthOffset)
}

val x = this.scale(point.x, scaleFactor) - postScaleWidthOffset
val y = this.scale(point.y, scaleFactor) - postScaleHeightOffset

faceContours.add(PointF(x, y))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class FaceAnalyzer(
val bitmap = mediaImage
.toRGBBitmap(context)
.rotate(imageProxy.imageInfo.rotationDegrees.toFloat())
.mirror(imageProxy.imageInfo.rotationDegrees.toFloat())

this.facefy.detect(
bitmap,
Expand All @@ -67,8 +68,7 @@ class FaceAnalyzer(
val detectionBox = this.coordinatesController.getDetectionBox(
faceDetected,
imageProxy.width.toFloat(),
imageProxy.height.toFloat(),
imageProxy.imageInfo.rotationDegrees.toFloat()
imageProxy.height.toFloat()
)

// Verify if has error on detection box.
Expand All @@ -83,8 +83,7 @@ class FaceAnalyzer(
val faceContours = this.coordinatesController.getFaceContours(
faceDetected.contours,
imageProxy.width.toFloat(),
imageProxy.height.toFloat(),
imageProxy.imageInfo.rotationDegrees.toFloat()
imageProxy.height.toFloat()
)

// Get face bitmap.
Expand Down Expand Up @@ -188,8 +187,8 @@ class FaceAnalyzer(
*
* 1. Color encoding if necessary;
* 2. Rotate image if necessary;
* 3. Crop image if necessary;
* 4. Mirror image if necessary;
* 3. Mirror image if necessary;
* 4. Crop image if necessary;
* 5. Scale image if necessary;
*
* @param mediaImage The camera frame image;
Expand All @@ -211,8 +210,8 @@ class FaceAnalyzer(

val faceBitmap: Bitmap = colorEncodedBitmap
.rotate(cameraRotation)
.crop(boundingBox)
.mirror(cameraRotation)
.crop(boundingBox)

return Bitmap.createScaledBitmap(
faceBitmap,
Expand Down

0 comments on commit ec28300

Please sign in to comment.