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

Update Bounding Box and MLkit dependency #36

Open
mat2718 opened this issue Jan 3, 2023 · 1 comment
Open

Update Bounding Box and MLkit dependency #36

mat2718 opened this issue Jan 3, 2023 · 1 comment

Comments

@mat2718
Copy link

mat2718 commented Jan 3, 2023

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

fixes

  • return normal bounding box parameters
  • upgrade MLkit to work with other plugins such as OCR

i can also include the function im using to adjust the bounding box based on on the screen size the camera takes up. i use that function when using a header since the bounding box can get a little skewed.

Here is the diff that solved my problem:

diff --git a/node_modules/vision-camera-face-detector/android/build.gradle b/node_modules/vision-camera-face-detector/android/build.gradle
index 55bfaac..8ced7fb 100644
--- a/node_modules/vision-camera-face-detector/android/build.gradle
+++ b/node_modules/vision-camera-face-detector/android/build.gradle
@@ -56,6 +56,6 @@ dependencies {
     implementation "com.facebook.react:react-native:+"  // From node_modules
     implementation 'androidx.annotation:annotation:1.1.0'
     api project(":react-native-vision-camera")
-    implementation "androidx.camera:camera-core:1.1.0-alpha06"
-    implementation 'com.google.mlkit:face-detection:16.1.2'
+    implementation "androidx.camera:camera-core:1.1.0-alpha08"
+    implementation 'com.google.mlkit:face-detection:16.1.5'
 }
diff --git a/node_modules/vision-camera-face-detector/android/src/main/java/com/visioncamerafacedetector/VisionCameraFaceDetectorPlugin.java b/node_modules/vision-camera-face-detector/android/src/main/java/com/visioncamerafacedetector/VisionCameraFaceDetectorPlugin.java
index 3e4b3ff..18e0371 100644
--- a/node_modules/vision-camera-face-detector/android/src/main/java/com/visioncamerafacedetector/VisionCameraFaceDetectorPlugin.java
+++ b/node_modules/vision-camera-face-detector/android/src/main/java/com/visioncamerafacedetector/VisionCameraFaceDetectorPlugin.java
@@ -1,4 +1,4 @@
 
 import static java.lang.Math.ceil;
@@ -47,26 +47,13 @@ public class VisionCameraFaceDetectorPlugin extends FrameProcessorPlugin {
   private WritableMap processBoundingBox(Rect boundingBox) {
     WritableMap bounds = Arguments.createMap();
 
-    // Calculate offset (we need to center the overlay on the target)
-    Double offsetX =  (boundingBox.exactCenterX() - ceil(boundingBox.width())) / 2.0f;
-    Double offsetY =  (boundingBox.exactCenterY() - ceil(boundingBox.height())) / 2.0f;
-
-    Double x = boundingBox.right + offsetX;
-    Double y = boundingBox.top + offsetY;
-
-
-
-    bounds.putDouble("x", boundingBox.centerX() + (boundingBox.centerX() - x));
-    bounds.putDouble("y", boundingBox.centerY() + (y - boundingBox.centerY()));
+    bounds.putDouble("x", boundingBox.centerX());
+    bounds.putDouble("y", boundingBox.centerY());
+    bounds.putDouble("top", boundingBox.top);
+    bounds.putDouble("left", boundingBox.left);
     bounds.putDouble("width", boundingBox.width());
     bounds.putDouble("height", boundingBox.height());
 
-
-    bounds.putDouble("boundingCenterX", boundingBox.centerX());
-    bounds.putDouble("boundingCenterY", boundingBox.centerY());
-    bounds.putDouble("boundingExactCenterX", boundingBox.exactCenterX());
-    bounds.putDouble("boundingExactCenterY", boundingBox.exactCenterY());
-
     return bounds;
   }
 
diff --git a/node_modules/vision-camera-face-detector/src/index.ts b/node_modules/vision-camera-face-detector/src/index.ts
index 8ca44fb..abe1fd7 100644
--- a/node_modules/vision-camera-face-detector/src/index.ts
+++ b/node_modules/vision-camera-face-detector/src/index.ts
@@ -13,11 +13,14 @@ export interface Face {
   rightEyeOpenProbability: number;
   smilingProbability: number;
   bounds: {
-    y: number;
     x: number;
+    y: number;
+    top: number;
+    left: number;
     height: number;
     width: number;
   };
+
   contours: {
     FACE: Point[];
     NOSE_BOTTOM: Point[];

This issue body was partially generated by patch-package.

vision-camera-face-detector+0.1.8.patch

ts file to adjust bounding box based on view and frame

adjustToView.ts

export type Dimensions = {width: number; height: number};

// matches return bounds from custom face detection module
export type Rect = {
  top: number;
  left: number;
  height: number;
  width: number;
};

/**
 * It takes a frame and a view, and returns an object with two functions: adjustPoint and adjustRect
 * @param {Dimensions} frame - Dimensions - the dimensions of the video frame
 * @param {Dimensions} view - Dimensions
 * @returns An object with two functions.
 * @resource https://github.com/bglgwyng/FrameProcessorExample/blob/e8e99d58c878d4dce9a8adf74a7447d253be93ab/adjustToView.ts#L21
 */
const adjustToView = (
  frame: Dimensions,
  view: Dimensions,
  landscape: boolean,
  verticalCropPadding: number,
  horizontalCropPadding: number,
) => {
  'worklet';
  const {width, height} = view;
  /* Calculating the aspect ratio of the view. */
  const aspectRatio = width / height;

  const frameWidth = frame.width;
  const frameHeight = frame.height;

  /* Setting the widthRatio, heightRatio, offsetX, and offsetY to 0. */
  let widthRatio: number;
  let heightRatio: number;
  let offsetX = 0;
  let offsetY = 0;

  /* Calculating the ratio of the frame to the view. */

  const croppedFrameWidth = aspectRatio * frameHeight;
  const croppedFrameHeight = aspectRatio * frameWidth;
  if (!landscape) {
    offsetX = (frameWidth - croppedFrameWidth) / 2;
  } else {
    offsetY = (frameHeight - croppedFrameHeight) / 2;
  }
  heightRatio = height / frameHeight;
  widthRatio = width / croppedFrameWidth;

  /* Returning an object with two functions. */
  return {
    adjustPoint: (point: {x: number; y: number}) => ({
      x: (point.x - offsetX) * widthRatio,
      y: (point.y - offsetY) * heightRatio,
    }),
    adjustRect: (rect: Rect) => ({
      top: (rect.top - offsetY - verticalCropPadding) * heightRatio,
      left: (rect.left - offsetX - horizontalCropPadding) * widthRatio,
      height: (rect.height + verticalCropPadding) * heightRatio,
      width: (rect.width + horizontalCropPadding) * widthRatio,
    }),
  };
};

export default adjustToView;
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

No branches or pull requests

2 participants
@mat2718 and others