react-native-mediapipe-vision
Version:
MediaPipe frame processor for VisionCamera in React Native
84 lines (74 loc) • 2.39 kB
JavaScript
// Main entry point for react-native-mediapipe-vision
import { NativeModules } from 'react-native';
import { VisionCameraProxy, useFrameProcessor } from 'react-native-vision-camera';
const { MediaPipeVision } = NativeModules;
// Register the frame processor plugin with VisionCamera
// 'mediaPipeVision' must match the name registered in Objective-C code
const plugin = VisionCameraProxy.initFrameProcessorPlugin('mediaPipeVision');
// Export frame processor functions
export const MediaPipeProcessors = {
/**
* Process segmentation on a frame
* @returns Object with segmentation results
*/
segmentation: (frame) => {
'worklet';
if (!plugin) {
throw new Error('Failed to load MediaPipe Vision plugin');
}
return plugin.call(frame, { processorType: 'segmentation' });
},
/**
* Process pose detection on a frame
* @returns Object with pose detection results
*/
pose: (frame) => {
'worklet';
if (!plugin) {
throw new Error('Failed to load MediaPipe Vision plugin');
}
return plugin.call(frame, { processorType: 'pose' });
},
/**
* Process face detection on a frame
* @returns Object with face detection results
*/
face: (frame) => {
'worklet';
if (!plugin) {
throw new Error('Failed to load MediaPipe Vision plugin');
}
return plugin.call(frame, { processorType: 'face' });
},
/**
* Process hand landmarks on a frame
* @returns Object with hand detection results
*/
hands: (frame) => {
'worklet';
if (!plugin) {
throw new Error('Failed to load MediaPipe Vision plugin');
}
return plugin.call(frame, { processorType: 'hands' });
}
};
// Export a custom hook for easy use with VisionCamera
export const useMediaPipeProcessor = (processorType) => {
return useFrameProcessor((frame) => {
'worklet';
switch(processorType) {
case 'segmentation':
return MediaPipeProcessors.segmentation(frame);
case 'pose':
return MediaPipeProcessors.pose(frame);
case 'face':
return MediaPipeProcessors.face(frame);
case 'hands':
return MediaPipeProcessors.hands(frame);
default:
throw new Error(`Unsupported processor type: ${processorType}`);
}
}, [processorType]);
};
// Export the native module for any additional functionality
export default MediaPipeVision;