Creating Frame Processor Plugins
Overview​
Frame Processor Plugins are native functions which can be directly called from a JS Frame Processor. (See "Frame Processors")
They receive a frame from the Camera as an input and can return any kind of output. For example, a detectFaces
function returns an array of detected faces in the frame:
function App() {
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
const faces = detectFaces(frame)
console.log(`Faces in Frame: ${faces}`)
}, [])
return (
<Camera frameProcessor={frameProcessor} {...cameraProps} />
)
}
To achieve maximum performance, the detectFaces
function is written in a native language (e.g. Objective-C), but it will be directly called from the VisionCamera Frame Processor JavaScript-Runtime.
Types​
Similar to a TurboModule, the Frame Processor Plugin Registry API automatically manages type conversion from JS to native. They are converted into the most efficient data-structures, as seen here:
JS Type | Objective-C/Swift Type | Java/Kotlin Type |
---|---|---|
number | NSNumber* (double) | Double |
boolean | NSNumber* (boolean) | Boolean |
string | NSString* | String |
[] | NSArray* | List<Object> |
{} | NSDictionary* | Map<String, Object> |
undefined / null | nil | null |
(any, any) => void | RCTResponseSenderBlock | (Object, Object) -> void |
Frame | Frame* | Frame |