-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathindex.tsx
More file actions
140 lines (134 loc) · 3.77 KB
/
index.tsx
File metadata and controls
140 lines (134 loc) · 3.77 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
import Spinner from 'react-native-loading-spinner-overlay';
import { BottomBar } from '../../components/BottomBar';
import { getImage } from '../../utils';
import { useVerticalOCR, VERTICAL_OCR_ENGLISH } 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';
export default function VerticalOCRScree() {
const [imageUri, setImageUri] = useState('');
const [results, setResults] = useState<any[]>([]);
const [imageDimensions, setImageDimensions] = useState<{
width: number;
height: number;
}>();
const model = useVerticalOCR({
model: VERTICAL_OCR_ENGLISH,
independentCharacters: true,
});
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 output = await model.forward(imageUri);
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>
<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,
},
});