-
Notifications
You must be signed in to change notification settings - Fork 22
/
Camera.tsx
209 lines (192 loc) · 4.96 KB
/
Camera.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import React from 'react'
import {
Camera as VisionCamera,
// runAsync,
useFrameProcessor
} from 'react-native-vision-camera'
import {
Worklets,
// useRunOnJS,
useSharedValue
} from 'react-native-worklets-core'
import { useFaceDetector } from './FaceDetector'
// types
import type {
DependencyList,
ForwardedRef
} from 'react'
import type {
CameraProps,
Frame,
FrameInternal
} from 'react-native-vision-camera'
import type {
Face,
FaceDetectionOptions
} from './FaceDetector'
type UseWorkletType = (
frame: FrameInternal
) => Promise<void>
type UseRunInJSType = (
faces: Face[],
frame: Frame
) => Promise<void | Promise<void>>
type CallbackType = (
faces: Face[],
frame: Frame
) => void | Promise<void>
type ComponentType = {
faceDetectionOptions?: FaceDetectionOptions
faceDetectionCallback: CallbackType
} & CameraProps
/**
* Create a Worklet function that persists between re-renders.
* The returned function can be called from both a Worklet context and the JS context, but will execute on a Worklet context.
*
* @param {function} func The Worklet. Must be marked with the `'worklet'` directive.
* @param {DependencyList} dependencyList The React dependencies of this Worklet.
* @returns {UseWorkletType} A memoized Worklet
*/
function useWorklet(
func: ( frame: FrameInternal ) => void,
dependencyList: DependencyList
): UseWorkletType {
const worklet = React.useMemo( () => {
const context = Worklets.defaultContext
return context.createRunAsync( func )
}, dependencyList )
return worklet
}
/**
* Create a Worklet function that runs the giver function on JS context.
* The returned function can be called from a Worklet to hop back to the JS thread.
*
* @param {function} func The Worklet. Must be marked with the `'worklet'` directive.
* @param {DependencyList} dependencyList The React dependencies of this Worklet.
* @returns {UseRunInJSType} a memoized Worklet
*/
function useRunInJS(
func: CallbackType,
dependencyList: DependencyList
): UseRunInJSType {
return React.useMemo( () => (
Worklets.createRunOnJS( func )
), dependencyList )
}
/**
* Vision camera wrapper
*
* @param {ComponentType} props Camera + face detection props
* @returns
*/
export const Camera = React.forwardRef( ( {
faceDetectionOptions,
faceDetectionCallback,
...props
}: ComponentType,
ref: ForwardedRef<VisionCamera>
) => {
const { detectFaces } = useFaceDetector( faceDetectionOptions )
/**
* Is there an async task already running?
*/
const isAsyncContextBusy = useSharedValue( false )
/**
* Throws logs/errors back on js thread
*/
const logOnJs = Worklets.createRunOnJS( (
log: string,
error?: Error
) => {
if ( error ) {
console.error( log, error.message ?? JSON.stringify( error ) )
} else {
console.log( log )
}
} )
/**
* Runs on detection callback on js thread
*/
const runOnJs = useRunInJS( faceDetectionCallback, [
faceDetectionCallback
] )
/**
* Async context that will handle face detection
*/
const runOnAsyncContext = useWorklet( (
frame: FrameInternal
) => {
'worklet'
try {
const faces = detectFaces( frame )
// increment frame count so we can use frame on
// js side without frame processor getting stuck
frame.incrementRefCount()
runOnJs(
faces,
frame
).finally( () => {
'worklet'
// finally decrement frame count so it can be dropped
frame.decrementRefCount()
} )
} catch ( error: any ) {
logOnJs( 'Execution error:', error )
} finally {
frame.decrementRefCount()
isAsyncContextBusy.value = false
}
}, [
detectFaces,
runOnJs
] )
/**
* Detect faces on frame on an async context without blocking camera preview
*
* @param {Frame} frame Current frame
*/
function runAsync( frame: Frame ) {
'worklet'
if ( isAsyncContextBusy.value ) return
// set async context as busy
isAsyncContextBusy.value = true
// cast to internal frame and increment ref count
const internal = frame as FrameInternal
internal.incrementRefCount()
// detect faces in async context
runOnAsyncContext( internal )
}
/**
* Camera frame processor
*/
const cameraFrameProcessor = useFrameProcessor( ( frame ) => {
'worklet'
runAsync( frame )
}, [ runOnAsyncContext ] )
//
// use bellow when vision-camera's
// context creation issue is solved
//
// /**
// * Runs on detection callback on js thread
// */
// const runOnJs = useRunOnJS( faceDetectionCallback, [
// faceDetectionCallback
// ] )
// const cameraFrameProcessor = useFrameProcessor( ( frame ) => {
// 'worklet'
// runAsync( frame, () => {
// 'worklet'
// runOnJs(
// detectFaces( frame ),
// frame
// )
// } )
// }, [ runOnJs ] )
return <VisionCamera
{ ...props }
ref={ ref }
frameProcessor={ cameraFrameProcessor }
pixelFormat='yuv'
/>
} )