volfiros commited on
Commit
78b3bce
·
1 Parent(s): a17bbb0

fix: move ROI from buggy forehead to cheeks with correct centering

Browse files
frontend/src/components/WebcamFeed.tsx CHANGED
@@ -1,6 +1,6 @@
1
  import { useRef, useEffect } from "react";
2
  import { useFaceDetection } from "../hooks/useFaceDetection";
3
- import { computeForeheadROI, extractRGBMeans } from "../lib/extractROI";
4
  import { FPS } from "../lib/constants";
5
 
6
  interface WebcamFeedProps {
@@ -94,7 +94,7 @@ export default function WebcamFeed({ onFrame, onFaceDetected, onCameraError, onV
94
  const face = detectFace(video, now);
95
 
96
  if (face) {
97
- const roi = computeForeheadROI(
98
  face.leftEye,
99
  face.rightEye,
100
  canvas.width,
 
1
  import { useRef, useEffect } from "react";
2
  import { useFaceDetection } from "../hooks/useFaceDetection";
3
+ import { computeFaceROI, extractRGBMeans } from "../lib/extractROI";
4
  import { FPS } from "../lib/constants";
5
 
6
  interface WebcamFeedProps {
 
94
  const face = detectFace(video, now);
95
 
96
  if (face) {
97
+ const roi = computeFaceROI(
98
  face.leftEye,
99
  face.rightEye,
100
  canvas.width,
frontend/src/lib/constants.ts CHANGED
@@ -7,9 +7,6 @@ export const HEALTH_CHECK_URL = `${window.location.protocol}//${window.location.
7
  export const WS_URL = `${window.location.protocol === "https:" ? "wss:" : "ws:"}//${window.location.host}/ws`;
8
  export const MAX_WS_RETRIES = 5;
9
  export const WS_RETRY_BASE_MS = 2000;
10
- export const ROI_WIDTH_RATIO = 0.8;
11
- export const ROI_HEIGHT_RATIO = 0.4;
12
- export const ROI_OFFSET_UP_RATIO = 0.4;
13
 
14
  export type Phase =
15
  | "idle"
 
7
  export const WS_URL = `${window.location.protocol === "https:" ? "wss:" : "ws:"}//${window.location.host}/ws`;
8
  export const MAX_WS_RETRIES = 5;
9
  export const WS_RETRY_BASE_MS = 2000;
 
 
 
10
 
11
  export type Phase =
12
  | "idle"
frontend/src/lib/extractROI.ts CHANGED
@@ -1,11 +1,9 @@
1
- import { ROI_WIDTH_RATIO, ROI_HEIGHT_RATIO, ROI_OFFSET_UP_RATIO } from "./constants";
2
-
3
  interface Keypoint {
4
  x: number;
5
  y: number;
6
  }
7
 
8
- export function computeForeheadROI(
9
  leftEye: Keypoint,
10
  rightEye: Keypoint,
11
  canvasWidth: number,
@@ -13,25 +11,24 @@ export function computeForeheadROI(
13
  ): { x: number; y: number; width: number; height: number } {
14
  const midX = (leftEye.x + rightEye.x) / 2;
15
  const midY = (leftEye.y + rightEye.y) / 2;
16
- const interEyeDist = Math.sqrt(
17
- (rightEye.x - leftEye.x) ** 2 + (rightEye.y - leftEye.y) ** 2
18
- );
 
 
 
19
 
20
- const width = interEyeDist * ROI_WIDTH_RATIO;
21
- const height = interEyeDist * ROI_HEIGHT_RATIO;
22
- const offsetX = midX * canvasWidth;
23
- const offsetY = midY * canvasHeight - interEyeDist * ROI_OFFSET_UP_RATIO * canvasHeight;
24
 
25
- const x = Math.max(0, offsetX - width * canvasWidth / 2);
26
- const y = Math.max(0, offsetY - height * canvasHeight);
27
- const w = width * canvasWidth;
28
- const h = height * canvasHeight;
29
 
30
  return {
31
  x,
32
  y,
33
- width: Math.min(w, canvasWidth - x),
34
- height: Math.min(h, canvasHeight - y),
35
  };
36
  }
37
 
 
 
 
1
  interface Keypoint {
2
  x: number;
3
  y: number;
4
  }
5
 
6
+ export function computeFaceROI(
7
  leftEye: Keypoint,
8
  rightEye: Keypoint,
9
  canvasWidth: number,
 
11
  ): { x: number; y: number; width: number; height: number } {
12
  const midX = (leftEye.x + rightEye.x) / 2;
13
  const midY = (leftEye.y + rightEye.y) / 2;
14
+ const dx = rightEye.x - leftEye.x;
15
+ const dy = rightEye.y - leftEye.y;
16
+ const interEyeDist = Math.sqrt(dx * dx + dy * dy);
17
+
18
+ const roiCenterX = midX * canvasWidth;
19
+ const roiCenterY = (midY + interEyeDist * 0.55) * canvasHeight;
20
 
21
+ const roiWidth = interEyeDist * 1.3 * canvasWidth;
22
+ const roiHeight = interEyeDist * 0.7 * canvasHeight;
 
 
23
 
24
+ const x = Math.max(0, roiCenterX - roiWidth / 2);
25
+ const y = Math.max(0, roiCenterY - roiHeight / 2);
 
 
26
 
27
  return {
28
  x,
29
  y,
30
+ width: Math.min(roiWidth, canvasWidth - x),
31
+ height: Math.min(roiHeight, canvasHeight - y),
32
  };
33
  }
34