Skip to content

Commit

Permalink
Dont detect blinks when face is out of frame
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinhenderson committed Jan 19, 2022
1 parent 3c65bf3 commit 38aeff9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/react-app/blink-detection-with-sliders.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { Box } from "@mui/system";
import { SliderWithValue } from "./slider-with-value.jsx";
import { Paper } from "@mui/material";
import { useBlink } from "./hooks/use-blink.js";
import { FACEMESH_FACE_OVAL } from "@mediapipe/face_mesh";

const KEEP_NUMBER_OF_VALUES = 20;
const OVAL_LANDMARKS = [...new Set(FACEMESH_FACE_OVAL.flat())];

export const BlinkDetectionWithSliders = ({
faceInFrame,
Expand Down Expand Up @@ -47,12 +49,26 @@ export const BlinkDetectionWithSliders = ({
const currentTimestamp = performance.now();

if (results.multiFaceLandmarks.length === 1 && faceInFrame) {
faceInFrame(true);
const face = results.multiFaceLandmarks[0];
const flatCoords = [
...OVAL_LANDMARKS.map((x) => face[x].x),
...OVAL_LANDMARKS.map((x) => face[x].y),
];

const biggest = Math.max(...flatCoords);
const smallest = Math.min(...flatCoords);

if (smallest < 0) {
faceInFrame(false);
} else if (biggest > 1) {
faceInFrame(false);
} else {
faceInFrame(true);
detectBlink(results, currentTimestamp, distanceHistory);
}
} else if (faceInFrame) {
faceInFrame(false);
}

detectBlink(results, currentTimestamp, distanceHistory);
},
[throttled, faceInFrame, detectBlink]
);
Expand Down
4 changes: 2 additions & 2 deletions src/react-app/main-screen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export const MainScreen = () => {
marginTop: "1rem",
}}
>
No face detected in frame, make sure your face is in the center of the
screen and well lit.
No face detected in frame, make sure your full face is in the center
of the screen and well lit.
</Typography>
)}
</Box>
Expand Down

0 comments on commit 38aeff9

Please sign in to comment.