-
Notifications
You must be signed in to change notification settings - Fork 74
feat: pose estimation #1100
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
Merged
Merged
feat: pose estimation #1100
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
941c8b2
feat: add native impl of pose estimation
chmjkb 072584a
chore: add cspell words and eslint rules
chmjkb 2c2887e
feat: add TS api
chmjkb 5b85c48
chore: type adjustment
chmjkb 7bab9cd
chore: another refactor
chmjkb 061c2c8
docs: add docs
chmjkb b885e75
docs: add usePoseEstimation to vc integration docs
chmjkb a440d61
chore: move CocoKeypoints to Types category
chmjkb 6a606ee
docs: custom model clarifiction
chmjkb dd7a745
docs: custom model clarificaiton
chmjkb e700880
chore: add example app screen
chmjkb f07608d
fix: handle orientation
chmjkb 88e70d2
chore: add model url
chmjkb 728f415
tests: vibe tests
chmjkb cc525a9
chore: review suggestions
chmjkb 56c3b50
chore: remove model from config~
chmjkb 8a79109
fix: add pose estimation task
chmjkb 0043051
chore: don't static cast numDetections
chmjkb 2778d2f
chore: review suggestions
chmjkb 62fc0c3
docs: update docs
chmjkb 1dc017c
chore: type fix, review changes
chmjkb c8869ca
fix(tests): update a new image for test_img
chmjkb 54fff29
chore: review changes
chmjkb ab50eac
chore: add cspell word
chmjkb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -193,3 +193,8 @@ BIOES | |
| viterbi | ||
| argmaxes | ||
| unpadded | ||
| keypoint | ||
| keypoints | ||
| Keypoint | ||
| Keypoints | ||
| letterboxing | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| import Spinner from '../../components/Spinner'; | ||
| import { BottomBar } from '../../components/BottomBar'; | ||
| import { getImage } from '../../utils'; | ||
| import { | ||
| usePoseEstimation, | ||
| PoseDetections, | ||
| RnExecutorchError, | ||
| RnExecutorchErrorCode, | ||
| YOLO26N_POSE, | ||
| } from 'react-native-executorch'; | ||
| import { View, StyleSheet, Image, Text } from 'react-native'; | ||
| import React, { useContext, useEffect, useState } from 'react'; | ||
| import { GeneratingContext } from '../../context'; | ||
| import ScreenWrapper from '../../ScreenWrapper'; | ||
| import { StatsBar } from '../../components/StatsBar'; | ||
| import Svg, { Circle, Line } from 'react-native-svg'; | ||
| import ErrorBanner from '../../components/ErrorBanner'; | ||
| import { COCO_SKELETON_CONNECTIONS } from '../../components/utils/cocoSkeleton'; | ||
|
|
||
| // Colors for different people | ||
| const PERSON_COLORS = ['lime', 'cyan', 'magenta', 'yellow', 'orange', 'pink']; | ||
|
|
||
| export default function PoseEstimationScreen() { | ||
| const [imageUri, setImageUri] = useState(''); | ||
| const [results, setResults] = useState<PoseDetections>([]); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [imageDimensions, setImageDimensions] = useState<{ | ||
| width: number; | ||
| height: number; | ||
| }>(); | ||
| const [inferenceTime, setInferenceTime] = useState<number | null>(null); | ||
| const [layout, setLayout] = useState({ width: 0, height: 0 }); | ||
|
|
||
| const model = usePoseEstimation({ model: YOLO26N_POSE }); | ||
| const { setGlobalGenerating } = useContext(GeneratingContext); | ||
|
|
||
| useEffect(() => { | ||
| setGlobalGenerating(model.isGenerating); | ||
| }, [model.isGenerating, setGlobalGenerating]); | ||
|
|
||
| useEffect(() => { | ||
| if (model.error) setError(String(model.error)); | ||
| }, [model.error]); | ||
|
|
||
| const handleCameraPress = async (isCamera: boolean) => { | ||
| const image = await getImage(isCamera); | ||
| const uri = image?.uri; | ||
| const width = image?.width; | ||
| const height = image?.height; | ||
|
|
||
| if (uri && width && height) { | ||
| setImageUri(image.uri as string); | ||
| setImageDimensions({ width, height }); | ||
| setResults([]); | ||
| setInferenceTime(null); | ||
| } | ||
| }; | ||
|
|
||
| const runForward = async () => { | ||
| if (imageUri) { | ||
| try { | ||
| const start = Date.now(); | ||
| const output = await model.forward(imageUri, { inputSize: 384 }); | ||
| setInferenceTime(Date.now() - start); | ||
| setResults(output); | ||
| } catch (e) { | ||
| if (e instanceof RnExecutorchError) { | ||
| switch (e.code) { | ||
| case RnExecutorchErrorCode.FileReadFailed: | ||
| setError('Could not read the selected image.'); | ||
| break; | ||
| case RnExecutorchErrorCode.ModelGenerating: | ||
| setError('Model is busy — wait for the current run to finish.'); | ||
| break; | ||
| case RnExecutorchErrorCode.InvalidUserInput: | ||
| case RnExecutorchErrorCode.InvalidArgument: | ||
| setError(`Invalid input: ${e.message}`); | ||
| break; | ||
| default: | ||
| setError(e.message); | ||
| } | ||
| } else { | ||
| setError(e instanceof Error ? e.message : String(e)); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (!model.isReady) { | ||
| return ( | ||
| <Spinner | ||
| visible={!model.isReady} | ||
| textContent={`Loading the model ${(model.downloadProgress * 100).toFixed(0)} %`} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <ScreenWrapper> | ||
| <ErrorBanner message={error} onDismiss={() => setError(null)} /> | ||
| <View style={styles.imageContainer}> | ||
| <View style={styles.image}> | ||
| {imageUri && imageDimensions?.width && imageDimensions?.height ? ( | ||
| <View | ||
| style={styles.imageWrapper} | ||
| onLayout={(e) => | ||
| setLayout({ | ||
| width: e.nativeEvent.layout.width, | ||
| height: e.nativeEvent.layout.height, | ||
| }) | ||
| } | ||
| > | ||
| <Image | ||
| source={{ uri: imageUri }} | ||
| style={styles.fullSizeImage} | ||
| resizeMode="contain" | ||
| /> | ||
| {results.length > 0 && | ||
| layout.width > 0 && | ||
| layout.height > 0 && | ||
| (() => { | ||
| // Account for resizeMode="contain" letterboxing: the image's | ||
| // displayed area is smaller than the container in one axis. | ||
| const imageRatio = | ||
| imageDimensions.width / imageDimensions.height; | ||
| const layoutRatio = layout.width / layout.height; | ||
| let scaleX: number, scaleY: number; | ||
| if (imageRatio > layoutRatio) { | ||
| scaleX = layout.width / imageDimensions.width; | ||
| scaleY = layout.width / imageRatio / imageDimensions.height; | ||
| } else { | ||
| scaleY = layout.height / imageDimensions.height; | ||
| scaleX = | ||
| (layout.height * imageRatio) / imageDimensions.width; | ||
| } | ||
| const offsetX = | ||
| (layout.width - imageDimensions.width * scaleX) / 2; | ||
| const offsetY = | ||
| (layout.height - imageDimensions.height * scaleY) / 2; | ||
| const isInBounds = (kp: { x: number; y: number }) => | ||
| kp.x >= 0 && | ||
| kp.y >= 0 && | ||
| kp.x <= imageDimensions.width && | ||
| kp.y <= imageDimensions.height; | ||
| return ( | ||
| <Svg style={StyleSheet.absoluteFill}> | ||
| {results.map((personKeypoints, personIdx) => { | ||
| const color = | ||
| PERSON_COLORS[personIdx % PERSON_COLORS.length]; | ||
| return ( | ||
| <React.Fragment key={`person-${personIdx}`}> | ||
| {COCO_SKELETON_CONNECTIONS.map( | ||
| ([from, to], lineIdx) => { | ||
| const kp1 = personKeypoints[from]; | ||
| const kp2 = personKeypoints[to]; | ||
| if (!kp1 || !kp2) return null; | ||
| if (!isInBounds(kp1) || !isInBounds(kp2)) | ||
| return null; | ||
| return ( | ||
| <Line | ||
| key={`person-${personIdx}-line-${lineIdx}`} | ||
| x1={kp1.x * scaleX + offsetX} | ||
| y1={kp1.y * scaleY + offsetY} | ||
| x2={kp2.x * scaleX + offsetX} | ||
| y2={kp2.y * scaleY + offsetY} | ||
| stroke={color} | ||
| strokeWidth="2" | ||
| /> | ||
| ); | ||
| } | ||
| )} | ||
| {Object.entries(personKeypoints) | ||
| .filter(([, kp]) => isInBounds(kp)) | ||
| .map(([name, kp]) => ( | ||
| <Circle | ||
| key={`person-${personIdx}-kp-${name}`} | ||
| cx={kp.x * scaleX + offsetX} | ||
| cy={kp.y * scaleY + offsetY} | ||
| r="4" | ||
| fill="red" | ||
| /> | ||
| ))} | ||
| </React.Fragment> | ||
| ); | ||
| })} | ||
| </Svg> | ||
| ); | ||
| })()} | ||
| </View> | ||
| ) : ( | ||
| <Image | ||
| style={styles.fullSizeImage} | ||
| resizeMode="contain" | ||
| source={require('../../assets/icons/executorch_logo.png')} | ||
| /> | ||
| )} | ||
| </View> | ||
| {!imageUri && ( | ||
| <View style={styles.infoContainer}> | ||
| <Text style={styles.infoTitle}>Pose Estimation</Text> | ||
| <Text style={styles.infoText}> | ||
| This model detects human body keypoints (17 COCO keypoints) and | ||
| draws a skeleton overlay. Pick an image from your gallery or take | ||
| one with your camera to get started. | ||
| </Text> | ||
| </View> | ||
| )} | ||
| </View> | ||
| <StatsBar | ||
| inferenceTime={inferenceTime} | ||
| detectionCount={results.length > 0 ? results.length : null} | ||
| /> | ||
| <BottomBar | ||
| handleCameraPress={handleCameraPress} | ||
| runForward={runForward} | ||
| hasImage={!!imageUri} | ||
| isGenerating={model.isGenerating} | ||
| /> | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| imageContainer: { | ||
| flex: 6, | ||
| width: '100%', | ||
| padding: 16, | ||
| }, | ||
| image: { | ||
| flex: 2, | ||
| borderRadius: 8, | ||
| width: '100%', | ||
| }, | ||
| imageWrapper: { | ||
| flex: 1, | ||
| width: '100%', | ||
| height: '100%', | ||
| }, | ||
| fullSizeImage: { | ||
| width: '100%', | ||
| height: '100%', | ||
| }, | ||
| infoContainer: { | ||
| alignItems: 'center', | ||
| padding: 16, | ||
| gap: 8, | ||
| }, | ||
| infoTitle: { | ||
| fontSize: 18, | ||
| fontWeight: '600', | ||
| color: 'navy', | ||
| }, | ||
| infoText: { | ||
| fontSize: 14, | ||
| color: '#555', | ||
| textAlign: 'center', | ||
| lineHeight: 20, | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export const COCO_SKELETON_CONNECTIONS = [ | ||
| ['NOSE', 'LEFT_EYE'], | ||
| ['NOSE', 'RIGHT_EYE'], | ||
| ['LEFT_EYE', 'LEFT_EAR'], | ||
| ['RIGHT_EYE', 'RIGHT_EAR'], | ||
| ['LEFT_SHOULDER', 'RIGHT_SHOULDER'], | ||
| ['LEFT_SHOULDER', 'LEFT_ELBOW'], | ||
| ['LEFT_ELBOW', 'LEFT_WRIST'], | ||
| ['RIGHT_SHOULDER', 'RIGHT_ELBOW'], | ||
| ['RIGHT_ELBOW', 'RIGHT_WRIST'], | ||
| ['LEFT_SHOULDER', 'LEFT_HIP'], | ||
| ['RIGHT_SHOULDER', 'RIGHT_HIP'], | ||
| ['LEFT_HIP', 'RIGHT_HIP'], | ||
| ['LEFT_HIP', 'LEFT_KNEE'], | ||
| ['LEFT_KNEE', 'LEFT_ANKLE'], | ||
| ['RIGHT_HIP', 'RIGHT_KNEE'], | ||
| ['RIGHT_KNEE', 'RIGHT_ANKLE'], | ||
| ] as const; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.