Skip to content

Commit 2494755

Browse files
committed
COMPONENT/ADD:
CameraNotAuthorized PAGE/ADD: ImageClassificationLiveFeed SERVICE/ADD: ProcessOutputLiveFeed PAGE/UPDATE: FruitRecognitionLiveFeed (refactor moving code to ProcessOutputLiveFeed and CameraNotAuthorized)
1 parent 5cdd76f commit 2494755

File tree

12 files changed

+1123
-41
lines changed

12 files changed

+1123
-41
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
3+
import { NAVIGATE } from '~/pages/Home'
4+
5+
import { Button, ButtonContainer, Title } from '~/components'
6+
7+
8+
interface iCAMERA_NOT_AUTHORIZED_PROPS {
9+
navigate : NAVIGATE
10+
}
11+
12+
13+
function CameraNotAuthorized ({ navigate } : iCAMERA_NOT_AUTHORIZED_PROPS) {
14+
return (
15+
<>
16+
<Title text='error' />
17+
<Title text='needPermissionCamera' />
18+
<ButtonContainer style={{ width: '60%', maxWidth: 300 }}>
19+
<Button onPress={() => navigate('Home')} text='Home' />
20+
</ButtonContainer>
21+
</>
22+
)
23+
}
24+
25+
26+
export default CameraNotAuthorized

src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Background from './Background'
22
import Button from './Button'
33
import ButtonContainer from './ButtonContainer'
4+
import CameraNotAuthorized from './CameraNotAuthorized'
45
import ImagePreview from './ImagePreview'
56
import Result from './Result'
67
import Title from './Title'
@@ -10,6 +11,7 @@ export {
1011
Background,
1112
Button,
1213
ButtonContainer,
14+
CameraNotAuthorized,
1315
ImagePreview,
1416
Result,
1517
Title

src/pages/FruitRecognitionLiveFeed/index.tsx

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import React, { useState } from 'react'
2-
import { RNCamera, TrackedTextFeature } from 'react-native-camera-tflite'
3-
import _ from 'lodash'
2+
import { RNCamera } from 'react-native-camera-tflite'
43

54
import { iPAGES_PROPS } from '~/pages'
65

7-
import { Translate } from '~/services'
6+
import { ProcessOutputLiveFeed, Translate } from '~/services'
87

9-
import { Button, ButtonContainer, Result, Title } from '~/components'
8+
import { CameraNotAuthorized, Result } from '~/components'
109

1110
import OUTPUTS from './outputs.json'
1211

1312

14-
let _currentInstant = 0
15-
16-
1713
function FruitRecognitionLiveFeed ({ navigate } : iPAGES_PROPS) {
1814
const [output, setOutput] = useState('')
1915

@@ -26,43 +22,17 @@ function FruitRecognitionLiveFeed ({ navigate } : iPAGES_PROPS) {
2622
}
2723

2824

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-
5525
return (
5626
<RNCamera
5727
type={RNCamera.Constants.Type.back}
5828
flashMode={RNCamera.Constants.FlashMode.on}
5929
permissionDialogTitle={Translate('permissionCamera')}
6030
permissionDialogMessage={Translate('needPermissionCamera')}
6131
// @ts-ignore
62-
onModelProcessed={processOutput}
32+
onModelProcessed={data => ProcessOutputLiveFeed(data, OUTPUTS, setOutput)}
6333
// @ts-ignore
6434
modelParams={modelParams}
65-
notAuthorizedView={<NotAuthorizedView />}
35+
notAuthorizedView={<CameraNotAuthorized navigate={navigate} />}
6636
style={{
6737
width: '100%', height: '100%', alignItems: 'center',
6838
justifyContent: 'center'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { useState } from 'react'
2+
import { RNCamera } from 'react-native-camera-tflite'
3+
4+
import { iPAGES_PROPS } from '~/pages'
5+
6+
import { ProcessOutputLiveFeed, Translate } from '~/services'
7+
8+
import { CameraNotAuthorized, Result } from '~/components'
9+
10+
import OUTPUTS from './outputs.json'
11+
12+
13+
function ImageClassificationLiveFeed ({ navigate } : iPAGES_PROPS) {
14+
const [output, setOutput] = useState('')
15+
16+
const modelParams = {
17+
file: 'models/mobilenet_v1_1.0_224_quant.tflite',
18+
inputDimX: 224,
19+
inputDimY: 224,
20+
outputDim: 1001,
21+
freqms: 0
22+
}
23+
24+
25+
return (
26+
<RNCamera
27+
type={RNCamera.Constants.Type.back}
28+
flashMode={RNCamera.Constants.FlashMode.on}
29+
permissionDialogTitle={Translate('permissionCamera')}
30+
permissionDialogMessage={Translate('needPermissionCamera')}
31+
// @ts-ignore
32+
onModelProcessed={data => ProcessOutputLiveFeed(data, OUTPUTS, setOutput)}
33+
// @ts-ignore
34+
modelParams={modelParams}
35+
notAuthorizedView={<CameraNotAuthorized navigate={navigate} />}
36+
style={{
37+
width: '100%', height: '100%', alignItems: 'center',
38+
justifyContent: 'center'
39+
}}
40+
>
41+
<Result result={output} />
42+
</RNCamera>
43+
)
44+
}
45+
46+
47+
export default ImageClassificationLiveFeed

0 commit comments

Comments
 (0)