Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

design: change design of displaying ocr results #112

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions examples/computer-vision/screens/OCRScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RECOGNIZER_EN_CRNN_512,
useOCR,
} from 'react-native-executorch';
import { View, StyleSheet, Image, Text } from 'react-native';
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
import { useState } from 'react';
import ImageWithBboxes2 from '../components/ImageWithOCRBboxes';

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

const model = useOCR({
detectorSource: DETECTOR_CRAFT_800,
Expand All @@ -45,7 +44,6 @@ export const OCRScreen = ({
if (typeof uri === 'string') {
setImageUri(uri as string);
setResults([]);
setDetectedText('');
}
};

Expand All @@ -54,11 +52,6 @@ export const OCRScreen = ({
const output = await model.forward(imageUri);
setResults(output);
console.log(output);
let txt = '';
output.forEach((detection: any) => {
txt += detection.text + ' ';
});
setDetectedText(txt);
} catch (e) {
console.error(e);
}
Expand All @@ -75,8 +68,8 @@ export const OCRScreen = ({

return (
<>
<View style={styles.imageContainer}>
<View style={styles.image}>
<View style={styles.container}>
<View style={styles.imageContainer}>
{imageUri && imageDimensions?.width && imageDimensions?.height ? (
<ImageWithBboxes2
detections={results}
Expand All @@ -88,13 +81,25 @@ export const OCRScreen = ({
/>
) : (
<Image
style={{ width: '100%', height: '100%' }}
style={styles.image}
resizeMode="contain"
source={require('../assets/icons/executorch_logo.png')}
/>
)}
</View>
<Text>{detectedText}</Text>
{results.length > 0 && (
<View style={styles.results}>
<Text style={styles.resultHeader}>Results</Text>
<ScrollView style={styles.resultsList}>
{results.map(({ text, score }) => (
<View key={text} style={styles.resultRecord}>
<Text style={styles.resultLabel}>{text}</Text>
<Text>{score.toFixed(3)}</Text>
</View>
))}
</ScrollView>
</View>
)}
</View>
<BottomBar
handleCameraPress={handleCameraPress}
Expand All @@ -105,14 +110,43 @@ export const OCRScreen = ({
};

const styles = StyleSheet.create({
image: {
imageContainer: {
flex: 2,
borderRadius: 8,
width: '100%',
},
imageContainer: {
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,
},
});
62 changes: 48 additions & 14 deletions examples/computer-vision/screens/VerticalOCRScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RECOGNIZER_EN_CRNN_64,
useVerticalOCR,
} from 'react-native-executorch';
import { View, StyleSheet, Image, Text } from 'react-native';
import { View, StyleSheet, Image, Text, ScrollView } from 'react-native';
import { useState } from 'react';
import ImageWithBboxes2 from '../components/ImageWithOCRBboxes';

Expand All @@ -24,7 +24,6 @@ export const VerticalOCRScreen = ({
width: number;
height: number;
}>();
const [detectedText, setDetectedText] = useState<string>('');
const model = useVerticalOCR({
detectorSources: {
detectorLarge: DETECTOR_CRAFT_1280,
Expand All @@ -47,7 +46,6 @@ export const VerticalOCRScreen = ({
if (typeof uri === 'string') {
setImageUri(uri as string);
setResults([]);
setDetectedText('');
}
};

Expand All @@ -56,11 +54,6 @@ export const VerticalOCRScreen = ({
const output = await model.forward(imageUri);
setResults(output);
console.log(output);
let txt = '';
output.forEach((detection: any) => {
txt += detection.text + ' ';
});
setDetectedText(txt);
} catch (e) {
console.error(e);
}
Expand All @@ -77,8 +70,8 @@ export const VerticalOCRScreen = ({

return (
<>
<View style={styles.imageContainer}>
<View style={styles.image}>
<View style={styles.container}>
<View style={styles.imageContainer}>
{imageUri && imageDimensions?.width && imageDimensions?.height ? (
<ImageWithBboxes2
detections={results}
Expand All @@ -90,13 +83,25 @@ export const VerticalOCRScreen = ({
/>
) : (
<Image
style={{ width: '100%', height: '100%' }}
style={styles.image}
resizeMode="contain"
source={require('../assets/icons/executorch_logo.png')}
/>
)}
</View>
<Text>{detectedText}</Text>
{results.length > 0 && (
<View style={styles.results}>
<Text style={styles.resultHeader}>Results</Text>
<ScrollView style={styles.resultsList}>
{results.map(({ text, score }) => (
<View key={text} style={styles.resultRecord}>
<Text style={styles.resultLabel}>{text}</Text>
<Text>{score.toFixed(3)}</Text>
</View>
))}
</ScrollView>
</View>
)}
</View>
<BottomBar
handleCameraPress={handleCameraPress}
Expand All @@ -107,14 +112,43 @@ export const VerticalOCRScreen = ({
};

const styles = StyleSheet.create({
image: {
imageContainer: {
flex: 2,
borderRadius: 8,
width: '100%',
},
imageContainer: {
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,
},
});