1+ import React , { useState } from 'react'
2+ import { RNCamera , TrackedTextFeature } from 'react-native-camera-tflite'
3+ import _ from 'lodash'
4+
5+ import { iPAGES_PROPS } from '~/pages'
6+
7+ import { Translate } from '~/services'
8+
9+ import { Button , ButtonContainer , Result , Title } from '~/components'
10+
11+ import OUTPUTS from './outputs.json'
12+
13+
14+ let _currentInstant = 0
15+
16+
17+ function FruitRecognitionLiveFeed ( { navigate } : iPAGES_PROPS ) {
18+ const [ output , setOutput ] = useState ( '' )
19+
20+ const modelParams = {
21+ file : 'models/fruit_recognitio_live_feed.tflite' ,
22+ inputDimX : 224 ,
23+ inputDimY : 224 ,
24+ outputDim : 1001 ,
25+ freqms : 0
26+ }
27+
28+
29+ function processOutput ( { data } : { data : { textBlocks : TrackedTextFeature [ ] } } ) {
30+ // @ts -ignore
31+ const probs = _ . map ( data , item => _ . round ( item / 255.0 , 0.02 ) )
32+ // @ts -ignore
33+ const orderedData = _ . chain ( data ) . zip ( OUTPUTS ) . orderBy ( 0 , 'desc' ) . map ( item => [ _ . round ( item [ 0 ] / 255.0 , 2 ) , item [ 1 ] ] ) . value ( )
34+ const outputData = _ . chain ( orderedData ) . take ( 3 ) . map ( item => `${ item [ 1 ] } : ${ ( item [ 0 ] * 100 ) . toFixed ( 0 ) } %` ) . join ( '\n' ) . value ( )
35+ const time = Date . now ( ) - ( _currentInstant || Date . now ( ) )
36+ const output = `Guesses:\n\n${ outputData } \n\nTime: ${ time } ms`
37+ setOutput ( output )
38+ _currentInstant = Date . now ( )
39+ }
40+
41+
42+ function NotAuthorizedView ( ) {
43+ return (
44+ < >
45+ < Title text = 'error' />
46+ < Title text = 'needPermissionCamera' />
47+ < ButtonContainer style = { { width : '60%' , maxWidth : 300 } } >
48+ < Button onPress = { ( ) => navigate ( 'Home' ) } text = 'Home' />
49+ </ ButtonContainer >
50+ </ >
51+ )
52+ }
53+
54+
55+ return (
56+ < RNCamera
57+ type = { RNCamera . Constants . Type . back }
58+ flashMode = { RNCamera . Constants . FlashMode . on }
59+ permissionDialogTitle = { Translate ( 'permissionCamera' ) }
60+ permissionDialogMessage = { Translate ( 'needPermissionCamera' ) }
61+ // @ts -ignore
62+ onModelProcessed = { processOutput }
63+ // @ts -ignore
64+ modelParams = { modelParams }
65+ notAuthorizedView = { < NotAuthorizedView /> }
66+ style = { {
67+ width : '100%' , height : '100%' , alignItems : 'center' ,
68+ justifyContent : 'center'
69+ } }
70+ >
71+ < Result result = { output } />
72+ </ RNCamera >
73+ )
74+ }
75+
76+
77+ export default FruitRecognitionLiveFeed
0 commit comments