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
0 commit comments