Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FaceControls #1461

Merged
merged 16 commits into from
Jun 5, 2023
33 changes: 20 additions & 13 deletions .storybook/stories/FaceControls.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint react-hooks/exhaustive-deps: 1 */
import * as THREE from 'three'
import * as React from 'react'
import { withKnobs, number } from '@storybook/addon-knobs'
Expand All @@ -13,20 +14,26 @@ export default {
decorators: [withKnobs, (storyFn) => <Setup cameraFov={60}>{storyFn()}</Setup>],
}

export const FaceControlsSt = ({ eyes }) => (
<>
<color attach="background" args={['#303030']} />
<axesHelper />

<FaceLandmarker>
<FaceControls eyes={eyes} />
</FaceLandmarker>
function FaceControlsScene({ eyes }) {
return (
<>
<color attach="background" args={['#303030']} />
<axesHelper />

<React.Suspense fallback={null}>
Copy link
Member Author

@abernier abernier Jun 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess React.Suspense is now required here, since <FaceLandmarker> provider is now suspended?

I'm not sure

<FaceLandmarker>
<FaceControls eyes={eyes} />
</FaceLandmarker>
</React.Suspense>

<Box args={[0.1, 0.1, 0.1]}>
<meshStandardMaterial />
</Box>
</>
)
}

<Box args={[0.1, 0.1, 0.1]}>
<meshStandardMaterial />
</Box>
</>
)
export const FaceControlsSt = ({ eyes }) => <FaceControlsScene eyes={eyes} />
FaceControlsSt.args = {
eyes: undefined,
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/FaceControls.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint react-hooks/exhaustive-deps: 1 */
import * as THREE from 'three'
import * as React from 'react'
import {
Expand Down Expand Up @@ -300,7 +301,7 @@ export const FaceControls = forwardRef<FaceControlsApi, FaceControlsProps>(
rotation-z={Math.PI}
visible={debug}
>
<meshStandardMaterial flatShading={true} side={THREE.DoubleSide} />
<meshBasicMaterial side={THREE.DoubleSide} />
</Facemesh>
</FaceControlsContext.Provider>
)
Expand Down
25 changes: 15 additions & 10 deletions src/core/FaceLandmarker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint react-hooks/exhaustive-deps: 1 */
import * as React from 'react'
import { createContext, ReactNode, useContext, useEffect, useState } from 'react'
import { createContext, ReactNode, useContext, useEffect } from 'react'
import { FilesetResolver, FaceLandmarker as FaceLandmarkerImpl, FaceLandmarkerOptions } from '@mediapipe/tasks-vision'
import { clear, suspend } from 'suspend-react'

const FaceLandmarkerContext = createContext({} as FaceLandmarkerImpl | undefined)

Expand Down Expand Up @@ -29,17 +31,20 @@ export function FaceLandmarker({
options = FaceLandmarkerDefaults.options,
children,
}: FaceLandmarkerProps) {
const [faceLandmarker, setFaceLandmarker] = useState<FaceLandmarkerImpl>()
useEffect(() => {
let ret: FaceLandmarkerImpl
const opts = JSON.stringify(options)

FilesetResolver.forVisionTasks(basePath)
.then((vision) => FaceLandmarkerImpl.createFromOptions(vision, options))
.then((faceLandmarker) => setFaceLandmarker(faceLandmarker))
.catch((err) => console.error('error while creating faceLandmarker', err))
const faceLandmarker = suspend(async () => {
return await FilesetResolver.forVisionTasks(basePath).then((vision) =>
FaceLandmarkerImpl.createFromOptions(vision, options)
)
}, [basePath, opts])

return () => void ret?.close()
}, [basePath, options])
useEffect(() => {
return () => {
faceLandmarker?.close()
clear([basePath, opts])
}
}, [faceLandmarker, basePath, opts])

return <FaceLandmarkerContext.Provider value={faceLandmarker}>{children}</FaceLandmarkerContext.Provider>
}
Expand Down