-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathindex.tsx
More file actions
181 lines (171 loc) · 5.32 KB
/
Copy pathindex.tsx
File metadata and controls
181 lines (171 loc) · 5.32 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
import Spinner from '../../components/Spinner';
import { BottomBar } from '../../components/BottomBar';
import { ModelPicker, ModelOption } from '../../components/ModelPicker';
import { getImage } from '../../utils';
import {
models,
Detection,
useObjectDetection,
ObjectDetectionModelSources,
} from 'react-native-executorch';
import { View, StyleSheet, Image, Text } from 'react-native';
import ImageWithBboxes from '../../components/ImageWithBboxes';
import React, { useContext, useEffect, useState } from 'react';
import { GeneratingContext } from '../../context';
import ScreenWrapper from '../../ScreenWrapper';
import { StatsBar } from '../../components/StatsBar';
const objectDetection = models.object_detection;
const MODELS: ModelOption<ObjectDetectionModelSources>[] = [
{
label: 'RF-DeTR Nano',
value: objectDetection.rf_detr_nano(),
},
{
label: 'SSDLite MobileNet',
value: objectDetection.ssdlite_320_mobilenet_v3_large(),
},
{ label: 'YOLO26N', value: objectDetection.yolo26n() },
{ label: 'YOLO26S', value: objectDetection.yolo26s() },
{ label: 'YOLO26M', value: objectDetection.yolo26m() },
{ label: 'YOLO26L', value: objectDetection.yolo26l() },
{ label: 'YOLO26X', value: objectDetection.yolo26x() },
];
import ErrorBanner from '../../components/ErrorBanner';
export default function ObjectDetectionScreen() {
const [imageUri, setImageUri] = useState('');
const [results, setResults] = useState<Detection[]>([]);
const [error, setError] = useState<string | null>(null);
const [imageDimensions, setImageDimensions] = useState<{
width: number;
height: number;
}>();
const [selectedModel, setSelectedModel] =
useState<ObjectDetectionModelSources>(objectDetection.rf_detr_nano());
const [inferenceTime, setInferenceTime] = useState<number | null>(null);
const model = useObjectDetection({ model: selectedModel });
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: width as number, height: height as number });
setResults([]);
setInferenceTime(null);
}
};
const runForward = async () => {
if (imageUri) {
try {
const start = Date.now();
const output = await model.forward(imageUri);
setInferenceTime(Date.now() - start);
setResults(output);
} catch (e) {
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 ? (
<ImageWithBboxes
imageUri={
imageUri || require('../../assets/icons/executorch_logo.png')
}
imageWidth={imageDimensions.width}
imageHeight={imageDimensions.height}
detections={results}
/>
) : (
<Image
style={styles.fullSizeImage}
resizeMode="contain"
source={require('../../assets/icons/executorch_logo.png')}
/>
)}
</View>
{!imageUri && (
<View style={styles.infoContainer}>
<Text style={styles.infoTitle}>Object Detection</Text>
<Text style={styles.infoText}>
This model detects objects in an image and draws bounding boxes
around them with class labels and confidence scores. Pick an image
from your gallery or take one with your camera to get started.
</Text>
</View>
)}
</View>
<ModelPicker
models={MODELS}
selectedModel={selectedModel}
disabled={model.isGenerating}
onSelect={(m) => {
setSelectedModel(m);
setResults([]);
}}
/>
<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%',
},
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,
},
});