Skip to content

Commit 5cdd76f

Browse files
committed
Add iPAGES_PROPS interface in pages
LIB/ADD: react-native-camera-tflite @types/lodash deprecated-react-native-prop-types PAGE/ADD: FruitRecognitionLiveFeed PAGE/UPDATE: Home (add NAVIGATE type and navigate prop in HandlePage)
1 parent 07f1cd7 commit 5cdd76f

File tree

11 files changed

+145
-5
lines changed

11 files changed

+145
-5
lines changed

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ buildscript {
88
minSdkVersion = 21
99
compileSdkVersion = 31
1010
targetSdkVersion = 31
11+
googlePlayServicesVersion = "12.0.1"
12+
supportLibVersion = "27.1.0"
1113

1214
if (System.properties['os.arch'] == "aarch64") {
1315
// For M1 Users we need to use the NDK 24 which added support for aarch64

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"i18n-js": "^3.9.2",
1818
"react": "18.0.0",
1919
"react-native": "0.69.3",
20+
"react-native-camera-tflite": "^0.0.3",
2021
"react-native-image-picker": "^4.8.4",
2122
"tflite-react-native": "^0.0.5"
2223
},
@@ -27,12 +28,14 @@
2728
"@tsconfig/react-native": "^2.0.2",
2829
"@types/i18n-js": "^3.8.2",
2930
"@types/jest": "^26.0.23",
31+
"@types/lodash": "^4.14.182",
3032
"@types/react-native": "^0.69.3",
3133
"@types/react-test-renderer": "^18.0.0",
3234
"@typescript-eslint/eslint-plugin": "^5.29.0",
3335
"@typescript-eslint/parser": "^5.29.0",
3436
"babel-jest": "^26.6.3",
3537
"babel-plugin-root-import": "^6.6.0",
38+
"deprecated-react-native-prop-types": "^2.3.0",
3639
"eslint": "^7.32.0",
3740
"jest": "^26.6.3",
3841
"metro-react-native-babel-preset": "^0.70.3",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
"apples",
3+
"avocados",
4+
"bananas",
5+
"grapes",
6+
"guava",
7+
"limes",
8+
"mangos",
9+
"oranges",
10+
"pineapples",
11+
"watermelons"
12+
]

src/pages/Home/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Background, Button, ButtonContainer, Title } from '~/components'
88

99

1010
type PAGE = keyof iPAGES | null
11+
export type NAVIGATE = (to : PAGE) => void
1112

1213

1314
function Home () {
@@ -51,7 +52,7 @@ function Home () {
5152
function HandlePage () {
5253
if (page) {
5354
if (PAGES[page]) {
54-
return PAGES[page]()
55+
return PAGES[page]({ navigate })
5556
}
5657
else {
5758
return PickPage()

src/pages/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import FruitRecognition from './FruitRecognition'
2+
import FruitRecognitionLiveFeed from './FruitRecognitionLiveFeed'
3+
import { NAVIGATE } from './Home'
24
import ImageClassification from './ImageClassification'
35

46

5-
type PAGE = () => JSX.Element
7+
export interface iPAGES_PROPS {
8+
navigate : NAVIGATE
9+
}
10+
11+
type PAGE = ({ navigate } : iPAGES_PROPS) => JSX.Element
612

713
export interface iPAGES {
814
[key : string] : PAGE,
915
FruitRecognition : PAGE,
16+
FruitRecognitionLiveFeed : PAGE,
1017
ImageClassification : PAGE
1118
}
1219

1320

1421
const PAGES : iPAGES = {
1522
FruitRecognition,
23+
FruitRecognitionLiveFeed,
1624
ImageClassification
1725
}
1826

src/services/Translate/en-US.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const EN_US : TRANSLATE = {
88
error: 'Error',
99
errorLoadModel: 'Error trying to load TensorFlow Lite model.',
1010
errorProcessingTflite: 'Something went wrong while processing in TensorFlow Lite.',
11+
needPermissionCamera: 'We need your permission to use your camera phone',
12+
permissionCamera: 'Permission to use camera',
1113
pickImagesCG: 'Pick Images from Camera & Gallery',
1214
selectImage: 'Select Image',
1315
takePicture: 'Take Picture',
@@ -16,6 +18,8 @@ const EN_US : TRANSLATE = {
1618
// PAGES
1719

1820
FruitRecognition: 'Fruit Recognition',
21+
FruitRecognitionLiveFeed: 'Fruit Recognition Live',
22+
Home: 'Home',
1923
ImageClassification: 'Image Classification'
2024
}
2125

src/services/Translate/es-ES.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const ES_ES : TRANSLATE = {
88
error: 'Error',
99
errorLoadModel: 'Error al intentar cargar modelo de TensorFlow Lite.',
1010
errorProcessingTflite: 'Algo salió mal durante el procesamiento en TensorFlow Lite.',
11+
needPermissionCamera: 'Necesitamos su permiso para usar la cámara de su teléfono',
12+
permissionCamera: 'Permiso para usar la cámara',
1113
pickImagesCG: 'Elija Imágenes de la Cámara y la Galería',
1214
selectImage: 'Seleccionar Imagen',
1315
takePicture: 'Tomar Foto',
@@ -16,6 +18,8 @@ const ES_ES : TRANSLATE = {
1618
// PAGES
1719

1820
FruitRecognition: 'Reconocimiento de Frutas',
21+
FruitRecognitionLiveFeed: 'Reconocimiento de Frutas En Vivo',
22+
Home: 'Home',
1923
ImageClassification: 'Clasificación de Imagen'
2024
}
2125

src/services/Translate/pt-BR.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const PT_BR : TRANSLATE = {
88
error: 'Erro',
99
errorLoadModel: 'Erro ao tentar carregar modelo do TensorFlow Lite.',
1010
errorProcessingTflite: 'Algo deu errado durante o processamento no TensorFlow Lite.',
11+
needPermissionCamera: 'Precisamos da sua permissão para usar a câmera do seu telefone',
12+
permissionCamera: 'Permissão para usar a câmera',
1113
pickImagesCG: 'Escolha Imagens da Câmera e da Galeria',
1214
selectImage: 'Selecionar Imagem',
1315
takePicture: 'Tirar Foto',
@@ -16,6 +18,8 @@ const PT_BR : TRANSLATE = {
1618
// PAGES
1719

1820
FruitRecognition: 'Reconhecimento de Frutas',
21+
FruitRecognitionLiveFeed: 'Reconhecimento de Frutas Ao Vivo',
22+
Home: 'Home',
1923
ImageClassification: 'Classificação de Imagem'
2024
}
2125

src/services/Translate/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type TRANSLATE = {
66
error : string,
77
errorLoadModel : string,
88
errorProcessingTflite : string,
9+
needPermissionCamera : string,
10+
permissionCamera : string,
911
pickImagesCG : string,
1012
selectImage : string,
1113
takePicture : string,
@@ -14,5 +16,7 @@ export type TRANSLATE = {
1416
// PAGES
1517

1618
FruitRecognition : string,
19+
FruitRecognitionLiveFeed : string,
20+
Home : string,
1721
ImageClassification : string
1822
}

0 commit comments

Comments
 (0)