1
1
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' ;
3
3
import ImagePicker from 'react-native-image-crop-picker' ;
4
4
import ObjectDetection from '@react-native-ml-kit/object-detection' ;
5
5
6
6
const App = ( ) => {
7
7
const [ labels , setLabels ] = useState ( [ ] ) ;
8
8
const [ imagePath , setImagePath ] = useState ( null ) ;
9
+ const [ imageHeightWidth , setImageHeightWidth ] = useState ( null ) ;
9
10
10
11
const handlePress = async ( ) => {
11
12
setLabels ( [ ] ) ;
13
+
12
14
const image = await ImagePicker . openPicker ( { mediaType : 'photo' } ) ;
15
+
16
+ setImageHeightWidth ( {
17
+ height : image . height ,
18
+ width : image . width
19
+ } )
20
+
13
21
setImagePath ( image . path ) ;
14
22
15
- console . log ( image . path ) ;
16
23
const result = await ObjectDetection . detectSingleImage ( {
17
- url : ' file:' + image . path ,
24
+ url : ( Platform . OS === 'ios' ? ' file:' : '' ) + image . path ,
18
25
confidence : 0.8 ,
19
26
shouldEnableMultipleObjects : true ,
20
27
shouldEnableClassification : true
21
28
} ) ;
22
- console . log ( result ) ;
29
+
23
30
setLabels ( result ) ;
24
31
} ;
25
32
26
- // resizeMode="repeat" has to be because any other configuration scales or recenters the image!
27
-
28
33
return (
29
34
< 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" >
30
38
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
+ }
33
50
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 >
44
54
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 } />
49
57
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 >
58
68
</ View >
59
69
) ;
60
70
} ;
61
71
62
72
const styles = StyleSheet . create ( {
63
73
container : {
64
- flex : 1 ,
65
- justifyContent : 'center' ,
74
+ } ,
75
+ controlContainer : {
66
76
alignItems : 'center' ,
77
+ justifyContent : 'center'
67
78
} ,
68
79
label : {
69
80
backgroundColor : '#ffff55' ,
@@ -78,10 +89,9 @@ const styles = StyleSheet.create({
78
89
marginBottom : 10 ,
79
90
marginTop : 20 ,
80
91
} ,
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 ,
85
95
backgroundColor : 'black'
86
96
}
87
97
} ) ;
0 commit comments