-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathindex.tsx
More file actions
182 lines (173 loc) · 4.89 KB
/
index.tsx
File metadata and controls
182 lines (173 loc) · 4.89 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
import Spinner from '../../components/Spinner';
import { BottomBar } from '../../components/BottomBar';
import { ModelPicker, ModelOption } from '../../components/ModelPicker';
import { getImage } from '../../utils';
import {
useOCR,
OCR_ENGLISH,
OCR_GERMAN,
OCR_FRENCH,
OCR_SPANISH,
OCR_ITALIAN,
OCR_JAPANESE,
OCR_KOREAN,
OCRProps,
} from 'react-native-executorch';
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
import ImageWithBboxes2 from '../../components/ImageWithOCRBboxes';
import React, { useContext, useEffect, useState } from 'react';
import { GeneratingContext } from '../../context';
import ScreenWrapper from '../../ScreenWrapper';
import { StatsBar } from '../../components/StatsBar';
type OCRModelSources = OCRProps['model'];
const MODELS: ModelOption<OCRModelSources>[] = [
{ label: 'English', value: OCR_ENGLISH },
{ label: 'German', value: OCR_GERMAN },
{ label: 'French', value: OCR_FRENCH },
{ label: 'Spanish', value: OCR_SPANISH },
{ label: 'Italian', value: OCR_ITALIAN },
{ label: 'Japanese', value: OCR_JAPANESE },
{ label: 'Korean', value: OCR_KOREAN },
];
export default function OCRScreen() {
const [imageUri, setImageUri] = useState('');
const [results, setResults] = useState<any[]>([]);
const [imageDimensions, setImageDimensions] = useState<{
width: number;
height: number;
}>();
const [selectedModel, setSelectedModel] =
useState<OCRModelSources>(OCR_ENGLISH);
const [inferenceTime, setInferenceTime] = useState<number | null>(null);
const model = useOCR({
model: selectedModel,
});
const { setGlobalGenerating } = useContext(GeneratingContext);
useEffect(() => {
setGlobalGenerating(model.isGenerating);
}, [model.isGenerating, setGlobalGenerating]);
const handleCameraPress = async (isCamera: boolean) => {
const image = await getImage(isCamera);
const width = image?.width;
const height = image?.height;
setImageDimensions({ width: width as number, height: height as number });
const uri = image?.uri;
if (typeof uri === 'string') {
setImageUri(uri as string);
setResults([]);
}
};
const runForward = async () => {
try {
const start = Date.now();
const output = await model.forward(imageUri);
setInferenceTime(Date.now() - start);
setResults(output);
} 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.container}>
<View style={styles.imageContainer}>
{imageUri && imageDimensions?.width && imageDimensions?.height ? (
<ImageWithBboxes2
detections={results}
imageWidth={imageDimensions?.width}
imageHeight={imageDimensions?.height}
imageUri={
imageUri || require('../../assets/icons/executorch_logo.png')
}
/>
) : (
<Image
style={styles.image}
resizeMode="contain"
source={require('../../assets/icons/executorch_logo.png')}
/>
)}
</View>
{results.length > 0 && (
<View style={styles.results}>
<Text style={styles.resultHeader}>Results</Text>
<ScrollView style={styles.resultsList}>
{results.map(({ text, score }, index) => (
<View key={index} style={styles.resultRecord}>
<Text style={styles.resultLabel}>{text}</Text>
<Text>{score.toFixed(3)}</Text>
</View>
))}
</ScrollView>
</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}
/>
</ScreenWrapper>
);
}
const styles = StyleSheet.create({
imageContainer: {
flex: 2,
borderRadius: 8,
width: '100%',
},
container: {
flex: 6,
width: '100%',
padding: 16,
},
image: {
width: '100%',
height: '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,
},
});