const frameProcessor = useSkiaFrameProcessor((frame) => {
'worklet'
const faces = detectFaces(frame);
frame.render();
for (const face of faces) {
if (face?.bounds) {
console.log(face.bounds);
const { x, y, width, height } = face.bounds;
const paint = Skia.Paint();
paint.setColor(Skia.Color('red'));
paint.setStyle(PaintStyle.Stroke);
frame.drawRect(face.bounds, paint);
}
}
}, []);
The bounding box appears offset to the bottom right of faces. I'm aware this is essentially a math problem but haven't been able to find the right solution. The frame processor is passed as a style property for react-native-vision-camera is used for the camera component. and react-native-vision-camera-face-detector is the face detector plugin
const previewWidth = Dimensions.get('window').width;
const previewHeight = Dimensions.get('window').height;
const frameWidth = frame.width;
const frameHeight = frame.height;
// Calculate scaling factors
const scaleX = previewWidth / frameWidth;
const scaleY = previewHeight / frameHeight;
for (const face of faces) {
if (face?.bounds) {
console.log(face.bounds);
const { x, y, width, height } = face.bounds;
const adjustedX = x * scaleX;
const adjustedY = y * scaleY;
const adjustedWidth = width * scaleX;
const adjustedHeight = height * scaleY;
tried these and similar ratios. haven't found a working one yet. Can subtract a cosntant value from x and y to make it work on my specific device but need something that can work on any device
if (aspectRatioFrame > aspectRatioPreview) { scaleX = previewWidth / frameWidth; scaleY = scaleX; } else { scaleY = previewHeight / frameHeight; scaleX = scaleY; }Then, apply these adjusted values when drawing the rectangleframe.drawRect({ x: adjustedX, y: adjustedY, width: adjustedWidth, height: adjustedHeight }, paint);