-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathindex.tsx
More file actions
259 lines (248 loc) · 8.96 KB
/
index.tsx
File metadata and controls
259 lines (248 loc) · 8.96 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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,
},
});