Expo image-manipulator crop - according to facedetector values

1k views Asked by At

Short: How can i use facedetector coordinates and sizes for image manipulator crop? I want to crop face from photo.

Explanation;

I use expo-camera with facedetection. It works so well. Face detector gives output object

 "origin": Object {
    "x": 85.00000000000006,
    "y": 231.35,   },   "size": Object {
    "height": 182,
    "width": 173.33333333333331,   }, }

I can draw a box around face with that coordinates on camera preview mode it fits with screen dimensions. After taking image, output image is much larger than camere preview. So i can’t use that coordinates to crop picture. I updated facedetector coordinates and sizes with respect to image size. For example my if image width 2 times larger than screen width , i multiplied originx, and width with 2. But image manipulator crop didnt give me desired results. Any help would be appreciated.

1

There are 1 answers

2
ertem aktay On

That is my solution. I take the percentage of the screen dimensions and face dimensions.

const growthFactor = {
          w: data.width / width,
          h: data.height / height,
        };
        const cropDimensions = {
          originX: Math.round(
            growthFactor.w * face.origin.x
          ),
          originY: Math.round(
            growthFactor.h * face.origin.y
          ),
          width: Math.round(
            growthFactor.h * face.size.height
          ),
          height: Math.round(
            growthFactor.h * face.size.height
          ),
        };

Ps. I multiplied both height and width with growtfactor.h because i needed square cropped photo.