Skip to content

Commit f15496b

Browse files
committed
Show or not Result components in pages
PAGE/ADD: ImageSegmentation COMPONENT/UPDATE: ImagePreview (add children and style props, increase min height, add margin bottom, change resize mode to contain) Result (add margin top and margin bottom)
1 parent 2494755 commit f15496b

File tree

10 files changed

+121
-16
lines changed

10 files changed

+121
-16
lines changed

src/components/ImagePreview/index.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
import React from 'react'
2-
import { Image, View } from 'react-native'
2+
import { Image, StyleProp, View, ViewStyle } from 'react-native'
33

44

55
interface iIMAGE_PREVIEW {
6+
children ?: JSX.Element | JSX.Element[] | null,
7+
style ?: StyleProp<ViewStyle>,
68
uri : string
79
}
810

911

10-
function ImagePreview ({ uri } : iIMAGE_PREVIEW) {
12+
function ImagePreview ({ children, style, uri } : iIMAGE_PREVIEW) {
1113
return (
1214
<View
13-
style={{
15+
style={[{
1416
width: '90%', height: '50%', maxWidth: 600,
15-
minHeight: 300, alignItems: 'center', justifyContent: 'center',
16-
backgroundColor: 'rgba(0,0,0,0.2)'
17-
}}
17+
minHeight: 350, alignItems: 'center', justifyContent: 'center',
18+
marginBottom: 20, backgroundColor: 'rgba(0,0,0,0.2)'
19+
}, style]}
1820
>
1921
{ uri && (
2022
<Image
2123
source={{ uri }}
2224
style={{
23-
resizeMode: 'center', width: '100%', height: '100%'
25+
resizeMode: 'contain', width: '96%', height: '96%'
2426
}}
2527
/>
2628
) }
29+
30+
{ children && children }
2731
</View>
2832
)
2933
}

src/components/Result/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Result ({ result, style } : iRESULT_PROPS) {
1313
<Text
1414
style={[{
1515
color: '#fff', fontSize: 18, lineHeight: 26,
16-
textAlign: 'center', marginVertical: 40
16+
textAlign: 'center', marginTop: 20, marginBottom: 40
1717
}, style]}
1818
>
1919
{ result }

src/pages/FruitRecognition/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ function FruitRecognition () {
7979
<>
8080
<Title text='pickImagesCG' />
8181
<ImagePreview uri={fileUri} />
82-
<Result result={result} />
82+
{ result && (
83+
<Result result={result} />
84+
) }
8385
<ButtonContainer style={{ width: '60%', maxWidth: 300 }}>
8486
<Button onPress={() => ChooseFile(setFileUri)} text='chooseFile' />
8587
<Button onPress={() => TakePicture(setFileUri)} text='takePicture' />

src/pages/ImageClassification/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ function ImageClassification () {
7979
<>
8080
<Title text='pickImagesCG' />
8181
<ImagePreview uri={fileUri} />
82-
<Result result={result} />
82+
{ result && (
83+
<Result result={result} />
84+
) }
8385
<ButtonContainer style={{ width: '60%', maxWidth: 300 }}>
8486
<Button onPress={() => ChooseFile(setFileUri)} text='chooseFile' />
8587
<Button onPress={() => TakePicture(setFileUri)} text='takePicture' />
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React, { useState, useEffect } from 'react'
2+
import { Alert, Image } from 'react-native'
3+
// @ts-ignore
4+
import Tflite from 'tflite-react-native'
5+
6+
import { ChooseFile, TakePicture, Translate } from '~/services'
7+
8+
import { Button, ButtonContainer, ImagePreview, Title } from '~/components'
9+
10+
11+
const tflite = new Tflite()
12+
13+
14+
function ImageSegmentation () {
15+
const [fileUri, setFileUri] = useState('')
16+
const [recognitions, setRecognitions] = useState('')
17+
18+
19+
function loadModel () {
20+
tflite.loadModel({
21+
model: 'models/deeplabv3_257_mv_gpu.tflite',
22+
labels: 'models/deeplabv3_257_mv_gpu.txt',
23+
numThreads: 1
24+
},
25+
(error : string) => {
26+
if (error) {
27+
Alert.alert(Translate('error'), Translate('errorLoadModel'))
28+
}
29+
})
30+
}
31+
32+
33+
function performImageSegmentation () {
34+
tflite.runSegmentationOnImage({
35+
path: fileUri,
36+
imageMean: 127.5,
37+
imageStd: 127.5,
38+
outputType: 'png'
39+
},
40+
(error : string, response : string) => {
41+
if (error) {
42+
Alert.alert(Translate('error'), Translate('errorProcessingTflite'))
43+
return
44+
}
45+
setRecognitions(response)
46+
})
47+
}
48+
49+
50+
useEffect(() => {
51+
if (fileUri) {
52+
performImageSegmentation()
53+
}
54+
}, [fileUri])
55+
56+
57+
useEffect(() => {
58+
loadModel()
59+
60+
return () => {
61+
tflite.close()
62+
}
63+
}, [])
64+
65+
66+
return (
67+
<>
68+
<Title text='pickImagesCG' />
69+
<ImagePreview uri={fileUri}>
70+
{ /* @ts-ignore */ }
71+
{ recognitions && (
72+
<Image
73+
source={{ uri: 'data:image/png;base64,' + recognitions }}
74+
style={{
75+
resizeMode: 'contain', width: '96%', height: '96%',
76+
position: 'absolute', opacity: 0.5
77+
}}
78+
/>
79+
) }
80+
</ImagePreview>
81+
<ButtonContainer style={{ width: '60%', maxWidth: 300 }}>
82+
<Button onPress={() => ChooseFile(setFileUri)} text='chooseFile' />
83+
<Button onPress={() => TakePicture(setFileUri)} text='takePicture' />
84+
</ButtonContainer>
85+
</>
86+
)
87+
}
88+
89+
90+
export default ImageSegmentation

src/pages/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import FruitRecognitionLiveFeed from './FruitRecognitionLiveFeed'
33
import { NAVIGATE } from './Home'
44
import ImageClassification from './ImageClassification'
55
import ImageClassificationLiveFeed from './ImageClassificationLiveFeed'
6+
import ImageSegmentation from './ImageSegmentation'
67

78

89
export interface iPAGES_PROPS {
@@ -16,15 +17,17 @@ export interface iPAGES {
1617
FruitRecognition : PAGE,
1718
FruitRecognitionLiveFeed : PAGE,
1819
ImageClassification : PAGE,
19-
ImageClassificationLiveFeed : PAGE
20+
ImageClassificationLiveFeed : PAGE,
21+
ImageSegmentation : PAGE
2022
}
2123

2224

2325
const PAGES : iPAGES = {
2426
FruitRecognition,
2527
FruitRecognitionLiveFeed,
2628
ImageClassification,
27-
ImageClassificationLiveFeed
29+
ImageClassificationLiveFeed,
30+
ImageSegmentation
2831
}
2932

3033

src/services/Translate/en-US.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const EN_US : TRANSLATE = {
2121
FruitRecognitionLiveFeed: 'Fruit Recognition Live',
2222
Home: 'Home',
2323
ImageClassification: 'Image Classification',
24-
ImageClassificationLiveFeed: 'Image Classification Live'
24+
ImageClassificationLiveFeed: 'Image Classification Live',
25+
ImageSegmentation: 'Image Segmentation'
2526
}
2627

2728

src/services/Translate/es-ES.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const ES_ES : TRANSLATE = {
2121
FruitRecognitionLiveFeed: 'Reconocimiento de Frutas En Vivo',
2222
Home: 'Home',
2323
ImageClassification: 'Clasificación de Imagen',
24-
ImageClassificationLiveFeed: 'Clasificación de Imagen En Vivo'
24+
ImageClassificationLiveFeed: 'Clasificación de Imagen En Vivo',
25+
ImageSegmentation: 'Segmentación de Imagen'
2526
}
2627

2728

src/services/Translate/pt-BR.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const PT_BR : TRANSLATE = {
2121
FruitRecognitionLiveFeed: 'Reconhecimento de Frutas Ao Vivo',
2222
Home: 'Home',
2323
ImageClassification: 'Classificação de Imagem',
24-
ImageClassificationLiveFeed: 'Classificação de Imagem Ao Vivo'
24+
ImageClassificationLiveFeed: 'Classificação de Imagem Ao Vivo',
25+
ImageSegmentation: 'Segmentação de Imagem'
2526
}
2627

2728

src/services/Translate/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ export type TRANSLATE = {
1919
FruitRecognitionLiveFeed : string,
2020
Home : string,
2121
ImageClassification : string,
22-
ImageClassificationLiveFeed : string
22+
ImageClassificationLiveFeed : string,
23+
ImageSegmentation : string
2324
}

0 commit comments

Comments
 (0)