Reputation: 27
I'm developing an app in React Native that uses the PoseDetection API by Google ML Kit. It's a project for my university. Here my package.json:
"dependencies": {
"metro-react-native-babel-preset": "^0.77.0",
"react": "18.2.0",
"react-native": "0.73.6",
"react-native-gesture-handler": "^2.14.0",
"react-native-reanimated": "^3.6.2",
"react-native-svg": "^15.3.0",
"react-native-vision-camera": "^4.0.5",
"react-native-worklets-core": "^1.3.3"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-transform-private-methods": "^7.24.7",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@react-native/babel-preset": "0.74.84",
"@react-native/eslint-config": "0.74.84",
"@react-native/metro-config": "0.74.84",
"@react-native/typescript-config": "0.74.84",
"@types/react": "^18.2.6",
"@types/react-test-renderer": "^18.0.0",
"babel-jest": "^29.6.3",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"prettier": "2.8.8",
"react-test-renderer": "18.2.0",
"typescript": "5.0.4"
},
And my App.tsx:
import React, { useEffect } from 'react';
import { Dimensions, Platform, StyleSheet, Text, useWindowDimensions, View } from 'react-native';
import { Camera, useFrameProcessor, useCameraDevice } from 'react-native-vision-camera';
import Animated, { useSharedValue, useAnimatedProps, useAnimatedStyle } from 'react-native-reanimated';
import Svg, { Line } from 'react-native-svg';
import { __poseDetection } from './FrameProcessorPlugin';
import 'react-native-worklets-core';
const AnimatedLine = Animated.createAnimatedComponent(Line);
const usePosition = (pose, valueName1, valueName2) => {
return useAnimatedProps(
() => ({
x1: pose.value[valueName1].x,
y1: pose.value[valueName1].y,
x2: pose.value[valueName2].x,
y2: pose.value[valueName2].y,
}),
[pose],
);
};
export function objectDetect(frame) {
'worklet';
return __poseDetection(frame);
}
const defaultPose = {
leftShoulder: { x: 0, y: 0 },
rightShoulder: { x: 0, y: 0 },
leftElbow: { x: 0, y: 0 },
rightElbow: { x: 0, y: 0 },
leftWrist: { x: 0, y: 0 },
rightWrist: { x: 0, y: 0 },
leftHip: { x: 0, y: 0 },
rightHip: { x: 0, y: 0 },
leftKnee: { x: 0, y: 0 },
rightKnee: { x: 0, y: 0 },
leftAnkle: { x: 0, y: 0 },
rightAnkle: { x: 0, y: 0 },
};
const App = () => {
const pose = useSharedValue(defaultPose);
const leftWristToElbowPosition = usePosition(pose, 'leftWrist', 'leftElbow');
const leftElbowToShoulderPosition = usePosition(pose, 'leftElbow', 'leftShoulder');
const leftShoulderToHipPosition = usePosition(pose, 'leftShoulder', 'leftHip');
const leftHipToKneePosition = usePosition(pose, 'leftHip', 'leftKnee');
const leftKneeToAnklePosition = usePosition(pose, 'leftKnee', 'leftAnkle');
const rightWristToElbowPosition = usePosition(pose, 'rightWrist', 'rightElbow');
const rightElbowToShoulderPosition = usePosition(pose, 'rightElbow', 'rightShoulder');
const rightShoulderToHipPosition = usePosition(pose, 'rightShoulder', 'rightHip');
const rightHipToKneePosition = usePosition(pose, 'rightHip', 'rightKnee');
const rightKneeToAnklePosition = usePosition(pose, 'rightKnee', 'rightAnkle');
const shoulderToShoulderPosition = usePosition(pose, 'leftShoulder', 'rightShoulder');
const hipToHipPosition = usePosition(pose, 'leftHip', 'rightHip');
const dimensions = useWindowDimensions();
const frameProcessor = useFrameProcessor((frame) => {
'worklet';
const poseObject = objectDetect(frame);
const xFactor = dimensions.width / frame.width;
const yFactor = dimensions.height / frame.height;
const poseCopy = {
leftShoulder: { x: 0, y: 0 },
rightShoulder: { x: 0, y: 0 },
leftElbow: { x: 0, y: 0 },
rightElbow: { x: 0, y: 0 },
leftWrist: { x: 0, y: 0 },
rightWrist: { x: 0, y: 0 },
leftHip: { x: 0, y: 0 },
rightHip: { x: 0, y: 0 },
leftKnee: { x: 0, y: 0 },
rightKnee: { x: 0, y: 0 },
leftAnkle: { x: 0, y: 0 },
rightAnkle: { x: 0, y: 0 },
};
Object.keys(poseObject).forEach((v) => {
poseCopy[v] = {
x: poseObject[v].x * xFactor,
y: poseObject[v].y * yFactor,
};
});
pose.value = poseCopy;
}, []);
const device = useCameraDevice('back');
useEffect(() => {
const checkPermissions = async () => {
await Camera.requestCameraPermission();
};
checkPermissions();
}, []);
if (device == null) {
return <Text>Loading...</Text>;
}
return (
<>
<Camera
frameProcessor={frameProcessor}
style={StyleSheet.absoluteFill}
device={device}
isActive={true}
/>
<Svg
height={Dimensions.get('window').height}
width={Dimensions.get('window').width}
style={styles.linesContainer}>
<AnimatedLine animatedProps={leftWristToElbowPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={leftElbowToShoulderPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={leftShoulderToHipPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={leftHipToKneePosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={leftKneeToAnklePosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={rightWristToElbowPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={rightElbowToShoulderPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={rightShoulderToHipPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={rightHipToKneePosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={rightKneeToAnklePosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={shoulderToShoulderPosition} stroke="red" strokeWidth="2" />
<AnimatedLine animatedProps={hipToHipPosition} stroke="red" strokeWidth="2" />
</Svg>
</>
);
};
const styles = StyleSheet.create({
linesContainer: {
position: 'absolute',
top: 0,
left: 0,
height: Dimensions.get('window').height,
width: Dimensions.get('window').width,
},
});
export default App;
Right now I want the App just to start the camera and show me the animated lines so I know that the posedetection works. But I receive the followin error:
I don't understand much of the syntax in React and Reanimated. How do I solve this?
If you need more info please let me know.
I checked similar GitHub and StackOF issues, but all of them were caused due to wrong imports. According to the solutions, mine seems to be fine
Like I said, I don't understand much of the syntax, so I let ChatGPT change the code a few times. But every single version causes the same error.
I checked the Reanimated website in their troubleshooting section. My error isn't listed in there.
Upvotes: 0
Views: 265
Reputation: 27
I found another issue on GitHub where someone mentioned his error and it's kind of similar to mine. Apparently on Reanimated V3 and when you use 'react-native-worklets-core', the import of 'useSharedValue' has to be of 'react-native-worklets-core'. So the solution is:
import React, { useEffect } from 'react';
import { Dimensions, Platform, StyleSheet, Text, useWindowDimensions, View } from 'react-native';
import { Camera, useFrameProcessor, useCameraDevice } from 'react-native-vision-camera';
import Animated, { useAnimatedProps, useAnimatedStyle } from 'react-native-reanimated'; //delete useSharedValue
import Svg, { Line } from 'react-native-svg';
import { poseDetect } from './objectDetect';
import { useSharedValue } from 'react-native-worklets-core'; //add it here
Upvotes: 1