-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathindex.tsx
More file actions
127 lines (121 loc) · 3.27 KB
/
index.tsx
File metadata and controls
127 lines (121 loc) · 3.27 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
import Spinner from '../../components/Spinner';
import { getImage } from '../../utils';
import {
useClassification,
EFFICIENTNET_V2_S_QUANTIZED,
} from 'react-native-executorch';
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
import { BottomBar } from '../../components/BottomBar';
import React, { useContext, useEffect, useState } from 'react';
import { GeneratingContext } from '../../context';
import ScreenWrapper from '../../ScreenWrapper';
export default function ClassificationScreen() {
const [results, setResults] = useState<{ label: string; score: number }[]>(
[]
);
const [imageUri, setImageUri] = useState('');
const model = useClassification({ model: EFFICIENTNET_V2_S_QUANTIZED });
const { setGlobalGenerating } = useContext(GeneratingContext);
useEffect(() => {
setGlobalGenerating(model.isGenerating);
}, [model.isGenerating, setGlobalGenerating]);
const handleCameraPress = async (isCamera: boolean) => {
const image = await getImage(isCamera);
const uri = image?.uri;
if (typeof uri === 'string') {
setImageUri(uri as string);
setResults([]);
}
};
const runForward = async () => {
if (imageUri) {
try {
const output = await model.forward(imageUri);
const top10 = Object.entries(output)
.sort(([, a], [, b]) => (b as number) - (a as number))
.slice(0, 10)
.map(([label, score]) => ({ label, score: score as number }));
setResults(top10);
} catch (e) {
console.error(e);
}
}
};
if (!model.isReady) {
return (
<Spinner
visible={!model.isReady}
textContent={`Loading the model ${(model.downloadProgress * 100).toFixed(0)} %`}
/>
);
}
return (
<ScreenWrapper>
<View style={styles.imageContainer}>
<Image
style={styles.image}
resizeMode="contain"
source={
imageUri
? { uri: imageUri }
: require('../../assets/icons/executorch_logo.png')
}
/>
{results.length > 0 && (
<View style={styles.results}>
<Text style={styles.resultHeader}>Results Top 10</Text>
<ScrollView style={styles.resultsList}>
{results.map(({ label, score }) => (
<View key={label} style={styles.resultRecord}>
<Text style={styles.resultLabel}>{label}</Text>
<Text>{score.toFixed(3)}</Text>
</View>
))}
</ScrollView>
</View>
)}
</View>
<BottomBar
handleCameraPress={handleCameraPress}
runForward={runForward}
/>
</ScreenWrapper>
);
}
const styles = StyleSheet.create({
imageContainer: {
flex: 6,
width: '100%',
padding: 16,
},
image: {
flex: 2,
borderRadius: 8,
width: '100%',
},
results: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
gap: 4,
padding: 4,
},
resultHeader: {
fontSize: 18,
color: 'navy',
},
resultsList: {
flex: 1,
},
resultRecord: {
flexDirection: 'row',
width: '100%',
justifyContent: 'space-between',
padding: 8,
borderBottomWidth: 1,
},
resultLabel: {
flex: 1,
marginRight: 4,
},
});