Skip to content

Commit 49763fb

Browse files
design: change design of displaying ocr results
1 parent 765305a commit 49763fb

File tree

2 files changed

+78
-18
lines changed

2 files changed

+78
-18
lines changed

examples/computer-vision/screens/OCRScreen.tsx

+39-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
RECOGNIZER_EN_CRNN_512,
99
useOCR,
1010
} from 'react-native-executorch';
11-
import { View, StyleSheet, Image, Text } from 'react-native';
11+
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
1212
import { useState } from 'react';
1313
import ImageWithBboxes2 from '../components/ImageWithOCRBboxes';
1414

@@ -24,7 +24,6 @@ export const OCRScreen = ({
2424
width: number;
2525
height: number;
2626
}>();
27-
const [detectedText, setDetectedText] = useState<string>('');
2827

2928
const model = useOCR({
3029
detectorSource: DETECTOR_CRAFT_800,
@@ -45,7 +44,6 @@ export const OCRScreen = ({
4544
if (typeof uri === 'string') {
4645
setImageUri(uri as string);
4746
setResults([]);
48-
setDetectedText('');
4947
}
5048
};
5149

@@ -54,11 +52,6 @@ export const OCRScreen = ({
5452
const output = await model.forward(imageUri);
5553
setResults(output);
5654
console.log(output);
57-
let txt = '';
58-
output.forEach((detection: any) => {
59-
txt += detection.text + ' ';
60-
});
61-
setDetectedText(txt);
6255
} catch (e) {
6356
console.error(e);
6457
}
@@ -94,7 +87,19 @@ export const OCRScreen = ({
9487
/>
9588
)}
9689
</View>
97-
<Text>{detectedText}</Text>
90+
{results.length > 0 && (
91+
<View style={styles.results}>
92+
<Text style={styles.resultHeader}>Results</Text>
93+
<ScrollView style={styles.resultsList}>
94+
{results.map(({ text, score }) => (
95+
<View key={text} style={styles.resultRecord}>
96+
<Text style={styles.resultLabel}>{text}</Text>
97+
<Text>{score.toFixed(3)}</Text>
98+
</View>
99+
))}
100+
</ScrollView>
101+
</View>
102+
)}
98103
</View>
99104
<BottomBar
100105
handleCameraPress={handleCameraPress}
@@ -115,4 +120,29 @@ const styles = StyleSheet.create({
115120
width: '100%',
116121
padding: 16,
117122
},
123+
results: {
124+
flex: 1,
125+
alignItems: 'center',
126+
justifyContent: 'center',
127+
gap: 4,
128+
padding: 4,
129+
},
130+
resultHeader: {
131+
fontSize: 18,
132+
color: 'navy',
133+
},
134+
resultsList: {
135+
flex: 1,
136+
},
137+
resultRecord: {
138+
flexDirection: 'row',
139+
width: '100%',
140+
justifyContent: 'space-between',
141+
padding: 8,
142+
borderBottomWidth: 1,
143+
},
144+
resultLabel: {
145+
flex: 1,
146+
marginRight: 4,
147+
},
118148
});

examples/computer-vision/screens/VerticalOCRScreen.tsx

+39-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
RECOGNIZER_EN_CRNN_64,
99
useVerticalOCR,
1010
} from 'react-native-executorch';
11-
import { View, StyleSheet, Image, Text } from 'react-native';
11+
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
1212
import { useState } from 'react';
1313
import ImageWithBboxes2 from '../components/ImageWithOCRBboxes';
1414

@@ -24,7 +24,6 @@ export const VerticalOCRScreen = ({
2424
width: number;
2525
height: number;
2626
}>();
27-
const [detectedText, setDetectedText] = useState<string>('');
2827
const model = useVerticalOCR({
2928
detectorSources: {
3029
detectorLarge: DETECTOR_CRAFT_1280,
@@ -47,7 +46,6 @@ export const VerticalOCRScreen = ({
4746
if (typeof uri === 'string') {
4847
setImageUri(uri as string);
4948
setResults([]);
50-
setDetectedText('');
5149
}
5250
};
5351

@@ -56,11 +54,6 @@ export const VerticalOCRScreen = ({
5654
const output = await model.forward(imageUri);
5755
setResults(output);
5856
console.log(output);
59-
let txt = '';
60-
output.forEach((detection: any) => {
61-
txt += detection.text + ' ';
62-
});
63-
setDetectedText(txt);
6457
} catch (e) {
6558
console.error(e);
6659
}
@@ -96,7 +89,19 @@ export const VerticalOCRScreen = ({
9689
/>
9790
)}
9891
</View>
99-
<Text>{detectedText}</Text>
92+
{results.length > 0 && (
93+
<View style={styles.results}>
94+
<Text style={styles.resultHeader}>Results</Text>
95+
<ScrollView style={styles.resultsList}>
96+
{results.map(({ text, score }) => (
97+
<View key={text} style={styles.resultRecord}>
98+
<Text style={styles.resultLabel}>{text}</Text>
99+
<Text>{score.toFixed(3)}</Text>
100+
</View>
101+
))}
102+
</ScrollView>
103+
</View>
104+
)}
100105
</View>
101106
<BottomBar
102107
handleCameraPress={handleCameraPress}
@@ -117,4 +122,29 @@ const styles = StyleSheet.create({
117122
width: '100%',
118123
padding: 16,
119124
},
125+
results: {
126+
flex: 1,
127+
alignItems: 'center',
128+
justifyContent: 'center',
129+
gap: 4,
130+
padding: 4,
131+
},
132+
resultHeader: {
133+
fontSize: 18,
134+
color: 'navy',
135+
},
136+
resultsList: {
137+
flex: 1,
138+
},
139+
resultRecord: {
140+
flexDirection: 'row',
141+
width: '100%',
142+
justifyContent: 'space-between',
143+
padding: 8,
144+
borderBottomWidth: 1,
145+
},
146+
resultLabel: {
147+
flex: 1,
148+
marginRight: 4,
149+
},
120150
});

0 commit comments

Comments
 (0)