Skip to content

Commit

Permalink
Merge pull request #64 from AceCentre/track-one-eye
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinhenderson authored Mar 11, 2022
2 parents 7513b2a + 81aadb4 commit 8c6d91b
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/react-app/hooks/use-blink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const BLINK_MODES = [
},
];

const BIGGER_VIEWS = ["DIRECTION_HOLD", "HOLD"];

export const useBlink = (...params) => {
const { loading: blinkModeLoading, value: blinkMode } = useStoreValue(
BLINK_MODE,
Expand All @@ -48,7 +50,7 @@ export const useBlink = (...params) => {

useResizer({
width: 900,
height: !blinkModeLoading && blinkMode === "DIRECTION_HOLD" ? 523 : 440,
height: !blinkModeLoading && BIGGER_VIEWS.includes(blinkMode) ? 523 : 440,
});

const [displayOnSlider, setDisplayOnSlider] = useState({
Expand Down
69 changes: 64 additions & 5 deletions src/react-app/hooks/use-blink/use-basic-blink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useCallback } from "react";
import { CHANGE_THRESHOLD_BASIC_KEY } from "../../lib/store-consts";
import {
CHANGE_THRESHOLD_BASIC_KEY,
BASIC_BLINK_EYE_TO_TRACK,
} from "../../lib/store-consts";
import { useStoreValue } from "../use-store";

const euclaideanDistance = (point, point1) => {
Expand All @@ -16,9 +19,16 @@ export const useBasicBlink = (onBlink, setDisplayOnSlider) => {
update: updateBlinkThreshold,
} = useStoreValue(CHANGE_THRESHOLD_BASIC_KEY, 5);

const {
loading: loadingEyeToTrack,
value: eyeToTrack,
update: updateEyeToTrack,
} = useStoreValue(BASIC_BLINK_EYE_TO_TRACK, "both");

const noop = useCallback(
(results, currentTimestamp, distanceHistory) => {
if (loadingBlinkThreshold) return;
if (loadingEyeToTrack) return;

if (results.multiFaceLandmarks) {
for (const landmarks of results.multiFaceLandmarks) {
Expand All @@ -45,7 +55,17 @@ export const useBasicBlink = (onBlink, setDisplayOnSlider) => {
const reRatio = rhDistance / rvDistance;
const leRatio = lhDistance / lvDistance;

const ratio = (reRatio + leRatio) / 2;
let ratio = null;

if (eyeToTrack === "both") {
ratio = (reRatio + leRatio) / 2;
} else if (eyeToTrack === "right") {
ratio = reRatio;
} else if (eyeToTrack === "left") {
ratio = leRatio;
} else {
throw new Error(`Invalid eyeToTrack: ${eyeToTrack}`);
}

const currentFrame = {
time: currentTimestamp,
Expand All @@ -66,14 +86,26 @@ export const useBasicBlink = (onBlink, setDisplayOnSlider) => {
}
}
},
[blinkThreshold, loadingBlinkThreshold, onBlink, setDisplayOnSlider]
[
blinkThreshold,
loadingBlinkThreshold,
onBlink,
setDisplayOnSlider,
loadingEyeToTrack,
eyeToTrack,
]
);

const eyesToTrackHighlights = {
both: { leftEye: true, rightEye: true },
left: { leftEye: true, rightEye: false },
right: { leftEye: false, rightEye: true },
};

return {
detectBlink: noop,
highlights: {
leftEye: true,
rightEye: true,
...eyesToTrackHighlights[eyeToTrack],
face: true,
leftPupil: false,
rightPupil: false,
Expand All @@ -94,6 +126,33 @@ export const useBasicBlink = (onBlink, setDisplayOnSlider) => {
updateBlinkThreshold(newValue / 10);
},
},
{
loadingOption: loadingEyeToTrack,
type: "radio",
options: [
{
value: "both",
name: "Both",
tooltip: "Detects both eyes",
},
{
value: "left",
name: "Left",
tooltip: "Detects only the left eye",
},
{
value: "right",
name: "Right",
tooltip: "Detects only the right eye",
},
],
defaultValue: loadingEyeToTrack ? "both" : eyeToTrack,
label: "Eyes to track",
tooltip: "Decide which eyes you want to track",
onChange: (newValue) => {
updateEyeToTrack(newValue);
},
},
],
};
};
26 changes: 22 additions & 4 deletions src/react-app/hooks/use-blink/use-direction-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,34 @@ export const useDirectionBasic = (onBlink, setDisplayOnSlider) => {
]
);

const eyesToTrackHighlights = {
both: {
leftPupil: true,
rightPupil: true,
leftEyeEdgePoints: true,
rightEyeEdgePoints: true,
},
left: {
leftPupil: true,
rightPupil: false,
leftEyeEdgePoints: true,
rightEyeEdgePoints: false,
},
right: {
leftPupil: false,
rightPupil: true,
leftEyeEdgePoints: false,
rightEyeEdgePoints: true,
},
};

return {
detectBlink: noop,
highlights: {
leftEye: false,
rightEye: false,
face: true,
leftPupil: true,
rightPupil: true,
leftEyeEdgePoints: true,
rightEyeEdgePoints: true,
...eyesToTrackHighlights[whichEyes],
},
options: [
{
Expand Down
26 changes: 22 additions & 4 deletions src/react-app/hooks/use-blink/use-direction-hold.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,34 @@ export const useDirectionHold = (onBlink, setDisplayOnSlider) => {
]
);

const eyesToTrackHighlights = {
both: {
leftPupil: true,
rightPupil: true,
leftEyeEdgePoints: true,
rightEyeEdgePoints: true,
},
left: {
leftPupil: true,
rightPupil: false,
leftEyeEdgePoints: true,
rightEyeEdgePoints: false,
},
right: {
leftPupil: false,
rightPupil: true,
leftEyeEdgePoints: false,
rightEyeEdgePoints: true,
},
};

return {
detectBlink: noop,
highlights: {
leftEye: false,
rightEye: false,
face: true,
leftPupil: true,
rightPupil: true,
leftEyeEdgePoints: true,
rightEyeEdgePoints: true,
...eyesToTrackHighlights[whichEyes],
},
options: [
{
Expand Down
58 changes: 55 additions & 3 deletions src/react-app/hooks/use-blink/use-hold-blink.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from "react";
import {
BLINK_LENGTH_KEY,
HOLD_BLINK_EYE_TO_TRACK,
CHANGE_THRESHOLD_HOLD_KEY,
} from "../../lib/store-consts";
import { useStoreValue } from "../use-store";
Expand All @@ -25,9 +26,16 @@ export const useHoldBlink = (onBlink, setDisplayOnSlider) => {
update: updateBlinkLength,
} = useStoreValue(BLINK_LENGTH_KEY, 500);

const {
loading: loadingEyeToTrack,
value: eyeToTrack,
update: updateEyeToTrack,
} = useStoreValue(HOLD_BLINK_EYE_TO_TRACK, "both");

const noop = useCallback(
(results, currentTimestamp, distanceHistory) => {
if (loadingBlinkThreshold) return;
if (loadingEyeToTrack) return;

if (results.multiFaceLandmarks) {
for (const landmarks of results.multiFaceLandmarks) {
Expand All @@ -54,7 +62,17 @@ export const useHoldBlink = (onBlink, setDisplayOnSlider) => {
const reRatio = rhDistance / rvDistance;
const leRatio = lhDistance / lvDistance;

const ratio = (reRatio + leRatio) / 2;
let ratio = null;

if (eyeToTrack === "both") {
ratio = (reRatio + leRatio) / 2;
} else if (eyeToTrack === "right") {
ratio = reRatio;
} else if (eyeToTrack === "left") {
ratio = leRatio;
} else {
throw new Error(`Invalid eyeToTrack: ${eyeToTrack}`);
}

const currentFrame = {
time: currentTimestamp,
Expand Down Expand Up @@ -95,14 +113,21 @@ export const useHoldBlink = (onBlink, setDisplayOnSlider) => {
blinkLength,
onBlink,
setDisplayOnSlider,
loadingEyeToTrack,
eyeToTrack,
]
);

const eyesToTrackHighlights = {
both: { leftEye: true, rightEye: true },
left: { leftEye: true, rightEye: false },
right: { leftEye: false, rightEye: true },
};

return {
detectBlink: noop,
highlights: {
leftEye: true,
rightEye: true,
...eyesToTrackHighlights[eyeToTrack],
face: true,
leftPupil: false,
rightPupil: false,
Expand Down Expand Up @@ -136,6 +161,33 @@ export const useHoldBlink = (onBlink, setDisplayOnSlider) => {
updateBlinkLength(newValue);
},
},
{
loadingOption: loadingEyeToTrack,
type: "radio",
options: [
{
value: "both",
name: "Both",
tooltip: "Detects both eyes",
},
{
value: "left",
name: "Left",
tooltip: "Detects only the left eye",
},
{
value: "right",
name: "Right",
tooltip: "Detects only the right eye",
},
],
defaultValue: loadingEyeToTrack ? "both" : eyeToTrack,
label: "Eyes to track",
tooltip: "Decide which eyes you want to track",
onChange: (newValue) => {
updateEyeToTrack(newValue);
},
},
],
};
};
Loading

0 comments on commit 8c6d91b

Please sign in to comment.