Skip to content

Commit 23f659e

Browse files
author
A
committed
improve app for image selection
1 parent 6ad2eba commit 23f659e

File tree

1 file changed

+46
-36
lines changed
  • object-detection/example

1 file changed

+46
-36
lines changed

Diff for: object-detection/example/App.js

+46-36
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,80 @@
11
import React, { useState } from 'react';
2-
import { Text, Button, StyleSheet, View, ImageBackground } from 'react-native';
2+
import { Text, Button, StyleSheet, View, ImageBackground, ScrollView, Dimensions, Platform } from 'react-native';
33
import ImagePicker from 'react-native-image-crop-picker';
44
import ObjectDetection from '@react-native-ml-kit/object-detection';
55

66
const App = () => {
77
const [labels, setLabels] = useState([]);
88
const [imagePath, setImagePath] = useState(null);
9+
const [imageHeightWidth, setImageHeightWidth] = useState(null);
910

1011
const handlePress = async () => {
1112
setLabels([]);
13+
1214
const image = await ImagePicker.openPicker({ mediaType: 'photo' });
15+
16+
setImageHeightWidth({
17+
height: image.height,
18+
width: image.width
19+
})
20+
1321
setImagePath(image.path);
1422

15-
console.log(image.path);
1623
const result = await ObjectDetection.detectSingleImage({
17-
url: 'file:' + image.path,
24+
url: (Platform.OS === 'ios' ? 'file:' : '') + image.path,
1825
confidence: 0.8,
1926
shouldEnableMultipleObjects: true,
2027
shouldEnableClassification: true
2128
});
22-
console.log(result);
29+
2330
setLabels(result);
2431
};
2532

26-
// resizeMode="repeat" has to be because any other configuration scales or recenters the image!
27-
2833
return (
2934
<View style={styles.container}>
35+
<ScrollView showsVerticalScrollIndicator={true} vertical={true} style={styles.imageContainer}>
36+
<ScrollView showsHorizontalScrollIndicator={true} horizontal={true}>
37+
{imageHeightWidth && <ImageBackground style={{ width: imageHeightWidth.width, height: imageHeightWidth.height }} source={{ uri: imagePath }} resizeMode="center">
3038

31-
<View style={styles.image}>
32-
<ImageBackground style={styles.image} source={{ uri: imagePath }} resizeMode="repeat">
39+
{labels.map(label => (<View key={label.index} style={{
40+
zIndex: 20,
41+
borderWidth: 1,
42+
borderColor: 'white',
43+
backgroundColor: 'rgba(0, 0, 0, 0.3)',
44+
width: label.frameWidth,
45+
height: label.frameHeight,
46+
left: label.frameX,
47+
top: label.frameY
48+
}} />))
49+
}
3350

34-
{labels.map(label => (<View key={label.index} style={{
35-
zIndex: 20,
36-
borderWidth: 1,
37-
borderColor: 'white',
38-
backgroundColor: 'rgba(0, 0, 0, 0.3)',
39-
width: label.frameWidth,
40-
height: label.frameHeight,
41-
left: label.frameX,
42-
top: label.frameY
43-
}} />))}
51+
</ImageBackground>}
52+
</ScrollView>
53+
</ScrollView>
4454

45-
</ImageBackground>
46-
</View>
47-
48-
<Button title="Choose an Image" onPress={handlePress} />
55+
<View style={styles.controlContainer}>
56+
<Button title="Choose an Image" onPress={handlePress} />
4957

50-
{labels.length > 0 && <Text style={styles.heading}>Labels</Text>}
51-
{labels.map(label => (
52-
<View style={styles.label} key={label}>
53-
<Text>
54-
{label.text} - {label.confidence.toFixed(2)}% (w*h*x*y == {label.frameWidth}x{label.frameHeight}x{label.frameX}x{label.frameY})
55-
</Text>
56-
</View>
57-
))}
58+
{labels.length > 0 && <Text style={styles.heading}>Labels</Text>}
59+
{labels.map(label => (
60+
<View style={styles.label} key={label}>
61+
<Text>
62+
{label.text} - {label.confidence.toFixed(2)}%
63+
w={label.frameWidth} h={label.frameHeight} x={label.frameX} y={label.frameY}
64+
</Text>
65+
</View>
66+
))}
67+
</View>
5868
</View>
5969
);
6070
};
6171

6272
const styles = StyleSheet.create({
6373
container: {
64-
flex: 1,
65-
justifyContent: 'center',
74+
},
75+
controlContainer: {
6676
alignItems: 'center',
77+
justifyContent: 'center'
6778
},
6879
label: {
6980
backgroundColor: '#ffff55',
@@ -78,10 +89,9 @@ const styles = StyleSheet.create({
7889
marginBottom: 10,
7990
marginTop: 20,
8091
},
81-
image: {
82-
overflow: 'hidden',
83-
height: 400,
84-
width: '100%',
92+
imageContainer: {
93+
height: Dimensions.get('window').height / 1.5,
94+
width: Dimensions.get('window').width,
8595
backgroundColor: 'black'
8696
}
8797
});

0 commit comments

Comments
 (0)