|
| 1 | +import { useState } from 'react'; |
| 2 | +import Spinner from 'react-native-loading-spinner-overlay'; |
| 3 | +import { BottomBar } from '../components/BottomBar'; |
| 4 | +import { efficientnet_v2_s } from '../models/classification'; |
| 5 | +import { getImageUri } from '../utils'; |
| 6 | +import { useClassification } from 'react-native-executorch'; |
| 7 | +import { View, StyleSheet, Image, Text, ScrollView } from 'react-native'; |
| 8 | + |
| 9 | +export const ClassificationScreen = ({ |
| 10 | + imageUri, |
| 11 | + setImageUri, |
| 12 | +}: { |
| 13 | + imageUri: string; |
| 14 | + setImageUri: (imageUri: string) => void; |
| 15 | +}) => { |
| 16 | + const [results, setResults] = useState<{ label: string; score: number }[]>( |
| 17 | + [] |
| 18 | + ); |
| 19 | + |
| 20 | + const model = useClassification({ |
| 21 | + modulePath: efficientnet_v2_s, |
| 22 | + }); |
| 23 | + |
| 24 | + const handleCameraPress = async (isCamera: boolean) => { |
| 25 | + const uri = await getImageUri(isCamera); |
| 26 | + if (typeof uri === 'string') { |
| 27 | + setImageUri(uri as string); |
| 28 | + setResults([]); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + const runForward = async () => { |
| 33 | + if (imageUri) { |
| 34 | + try { |
| 35 | + const output = await model.forward(imageUri); |
| 36 | + const top10 = Object.entries(output) |
| 37 | + .sort(([, a], [, b]) => b - a) |
| 38 | + .slice(0, 10) |
| 39 | + .map(([label, score]) => ({ label, score })); |
| 40 | + setResults(top10); |
| 41 | + } catch (e) { |
| 42 | + console.error(e); |
| 43 | + } |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + if (!model.isModelReady) { |
| 48 | + return ( |
| 49 | + <Spinner |
| 50 | + visible={!model.isModelReady} |
| 51 | + textContent={`Loading the model...`} |
| 52 | + /> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <> |
| 58 | + <View style={styles.imageContainer}> |
| 59 | + <Image |
| 60 | + style={styles.image} |
| 61 | + resizeMode="contain" |
| 62 | + source={ |
| 63 | + imageUri |
| 64 | + ? { uri: imageUri } |
| 65 | + : require('../assets/icons/executorch_logo.png') |
| 66 | + } |
| 67 | + /> |
| 68 | + {results.length > 0 && ( |
| 69 | + <View style={styles.results}> |
| 70 | + <Text style={styles.resultHeader}>Results Top 10</Text> |
| 71 | + <ScrollView style={styles.resultsList}> |
| 72 | + {results.map(({ label, score }) => ( |
| 73 | + <View key={label} style={styles.resultRecord}> |
| 74 | + <Text style={styles.resultLabel}>{label}</Text> |
| 75 | + <Text>{score.toFixed(3)}</Text> |
| 76 | + </View> |
| 77 | + ))} |
| 78 | + </ScrollView> |
| 79 | + </View> |
| 80 | + )} |
| 81 | + </View> |
| 82 | + <BottomBar |
| 83 | + handleCameraPress={handleCameraPress} |
| 84 | + runForward={runForward} |
| 85 | + /> |
| 86 | + </> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +const styles = StyleSheet.create({ |
| 91 | + imageContainer: { |
| 92 | + flex: 6, |
| 93 | + width: '100%', |
| 94 | + padding: 16, |
| 95 | + }, |
| 96 | + image: { |
| 97 | + flex: 2, |
| 98 | + borderRadius: 8, |
| 99 | + width: '100%', |
| 100 | + }, |
| 101 | + results: { |
| 102 | + flex: 1, |
| 103 | + alignItems: 'center', |
| 104 | + justifyContent: 'center', |
| 105 | + gap: 4, |
| 106 | + padding: 4, |
| 107 | + }, |
| 108 | + resultHeader: { |
| 109 | + fontSize: 18, |
| 110 | + color: 'navy', |
| 111 | + }, |
| 112 | + resultsList: { |
| 113 | + flex: 1, |
| 114 | + }, |
| 115 | + resultRecord: { |
| 116 | + flexDirection: 'row', |
| 117 | + width: '100%', |
| 118 | + justifyContent: 'space-between', |
| 119 | + padding: 8, |
| 120 | + borderBottomWidth: 1, |
| 121 | + }, |
| 122 | + resultLabel: { |
| 123 | + flex: 1, |
| 124 | + marginRight: 4, |
| 125 | + }, |
| 126 | +}); |
0 commit comments