Skip to content

Commit 4302b33

Browse files
committed
COMPONENT/ADD:
PoseEstimationRecognition PAGE/ADD: PoseEstimation COMPONENT/UPDATE: Recognition (change name to ObjectDetectionRecognition, RECOGNITION type to OBJECT_DETECTED)
1 parent 8bf4bd4 commit 4302b33

File tree

10 files changed

+178
-19
lines changed

10 files changed

+178
-19
lines changed

src/components/Recognition/index.tsx renamed to src/components/ObjectDetectionRecognition/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { Text, useWindowDimensions, View } from 'react-native'
33

44

5-
export type RECOGNITION = {
5+
export type OBJECT_DETECTED = {
66
detectedClass : string,
77
confidenceInClass : number,
88
rect : {
@@ -13,12 +13,12 @@ export type RECOGNITION = {
1313
}
1414
}
1515

16-
interface iRECOGNITION_PROPS {
17-
recognition : RECOGNITION[]
16+
interface iOBJECT_DETECTION_PROPS {
17+
recognition : OBJECT_DETECTED[]
1818
}
1919

2020

21-
function Recognition ({ recognition } : iRECOGNITION_PROPS) {
21+
function ObjectDetectionRecognition ({ recognition } : iOBJECT_DETECTION_PROPS) {
2222
const { width, height } = useWindowDimensions()
2323
const isPortrait = height >= width
2424

@@ -60,4 +60,4 @@ function Recognition ({ recognition } : iRECOGNITION_PROPS) {
6060
}
6161

6262

63-
export default Recognition
63+
export default ObjectDetectionRecognition
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react'
2+
import { Text, useWindowDimensions, View } from 'react-native'
3+
4+
5+
export type POSE = {
6+
score : number,
7+
keypoints : {
8+
[key : number] : {
9+
x : number,
10+
y : number,
11+
part : string,
12+
score : number
13+
}
14+
}
15+
}
16+
17+
interface iPOSE_ESTIMATION_RECOGNITION_PROPS {
18+
recognition : POSE[]
19+
}
20+
21+
22+
function PoseEstimationRecognition ({ recognition } : iPOSE_ESTIMATION_RECOGNITION_PROPS) {
23+
const { width, height } = useWindowDimensions()
24+
const isPortrait = height >= width
25+
26+
27+
return recognition.map(response => {
28+
return Object.values(response.keypoints).map(({ x, y, part }, key) => {
29+
let left, top
30+
top = y * 100 + '%'
31+
if (isPortrait) {
32+
left = x * 90 + '%'
33+
}
34+
else {
35+
left = x * 93 + '%'
36+
}
37+
38+
39+
return (
40+
<View
41+
key={key}
42+
style={{
43+
position: 'absolute', left, top
44+
}}
45+
>
46+
<Text
47+
style={{
48+
color: '#ff033e', fontSize: 14, fontWeight: 'bold'
49+
}}
50+
>
51+
{ '.' + part }
52+
</Text>
53+
</View>
54+
)
55+
})
56+
})
57+
}
58+
59+
60+
export default PoseEstimationRecognition

src/components/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import Button from './Button'
33
import ButtonContainer from './ButtonContainer'
44
import CameraNotAuthorized from './CameraNotAuthorized'
55
import ImagePreview from './ImagePreview'
6-
import Recognition from './Recognition'
6+
import ObjectDetectionRecognition from './ObjectDetectionRecognition'
7+
import PoseEstimationRecognition from './PoseEstimationRecognition'
78
import Result from './Result'
89
import Title from './Title'
910

@@ -14,7 +15,8 @@ export {
1415
ButtonContainer,
1516
CameraNotAuthorized,
1617
ImagePreview,
17-
Recognition,
18+
ObjectDetectionRecognition,
19+
PoseEstimationRecognition,
1820
Result,
1921
Title
2022
}

src/pages/ObjectDetection/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import Tflite from 'tflite-react-native'
55

66
import { ChooseFile, TakePicture, Translate } from '~/services'
77

8-
import { Button, ButtonContainer, ImagePreview, Recognition, Title } from '~/components'
8+
import {
9+
Button, ButtonContainer, ImagePreview, ObjectDetectionRecognition, Title
10+
} from '~/components'
911

10-
import { RECOGNITION } from '~/components/Recognition'
12+
import { OBJECT_DETECTED } from '~/components/ObjectDetectionRecognition'
1113

1214

1315
type MODELS = {
@@ -22,7 +24,7 @@ const tflite = new Tflite()
2224
function ObjectDetection () {
2325
const [chosenModel, setChosenModel] = useState<keyof MODELS | null>(null)
2426
const [fileUri, setFileUri] = useState('')
25-
const [recognition, setRecognition] = useState<RECOGNITION[]>([])
27+
const [recognition, setRecognition] = useState<OBJECT_DETECTED[] | null>(null)
2628

2729

2830
const loadModel : MODELS = {
@@ -58,7 +60,7 @@ function ObjectDetection () {
5860
threshold: 0.3,
5961
numResultsPerClass: 2
6062
},
61-
(error : string, response : RECOGNITION[]) => {
63+
(error : string, response : OBJECT_DETECTED[]) => {
6264
if (error) {
6365
Alert.alert(Translate('error'), Translate('errorProcessingTflite'))
6466
return
@@ -73,7 +75,7 @@ function ObjectDetection () {
7375
threshold: 0.3,
7476
numResultsPerClass: 2
7577
},
76-
(error : string, response : RECOGNITION[]) => {
78+
(error : string, response : OBJECT_DETECTED[]) => {
7779
if (error) {
7880
Alert.alert(Translate('error'), Translate('errorProcessingTflite'))
7981
return
@@ -134,7 +136,7 @@ function ObjectDetection () {
134136
<Title text='pickImagesCG' />
135137
<ImagePreview uri={fileUri}>
136138
{ /* @ts-ignore */ recognition && (
137-
<Recognition recognition={recognition} />
139+
<ObjectDetectionRecognition recognition={recognition} />
138140
) }
139141
</ImagePreview>
140142
<ButtonContainer style={{ width: '60%', maxWidth: 300 }}>

src/pages/PoseEstimation/index.tsx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React, { useState, useEffect } from 'react'
2+
import { Alert } from 'react-native'
3+
// @ts-ignore
4+
import Tflite from 'tflite-react-native'
5+
6+
import { ChooseFile, TakePicture, Translate } from '~/services'
7+
8+
import {
9+
Button, ButtonContainer, ImagePreview, PoseEstimationRecognition, Title
10+
} from '~/components'
11+
12+
import { POSE } from '~/components/PoseEstimationRecognition'
13+
14+
15+
const tflite = new Tflite()
16+
17+
18+
function PoseEstimation () {
19+
const [fileUri, setFileUri] = useState('')
20+
const [recognition, setRecognition] = useState<POSE[] | null>(null)
21+
22+
23+
function loadModel () {
24+
tflite.loadModel({
25+
model: 'models/posenet.tflite',
26+
numThreads: 1
27+
},
28+
(error : string) => {
29+
if (error) {
30+
Alert.alert(Translate('error'), Translate('errorLoadModel'))
31+
}
32+
})
33+
}
34+
35+
36+
function performPoseEstimation () {
37+
tflite.runPoseNetOnImage({
38+
path: fileUri,
39+
imageMean: 127.5,
40+
imageStd: 127.5,
41+
numResults: 3,
42+
threshold: 0.8,
43+
nmsRadius: 20
44+
},
45+
(error : string, response : POSE[]) => {
46+
if (error) {
47+
Alert.alert(Translate('error'), Translate('errorProcessingTflite'))
48+
return
49+
}
50+
setRecognition(response)
51+
})
52+
}
53+
54+
55+
useEffect(() => {
56+
if (fileUri) {
57+
performPoseEstimation()
58+
}
59+
}, [fileUri])
60+
61+
62+
useEffect(() => {
63+
loadModel()
64+
65+
return () => {
66+
tflite.close()
67+
}
68+
}, [])
69+
70+
71+
return (
72+
<>
73+
<Title text='pickImagesCG' />
74+
<ImagePreview uri={fileUri}>
75+
{ /* @ts-ignore */ recognition && (
76+
<PoseEstimationRecognition recognition={recognition} />
77+
) }
78+
</ImagePreview>
79+
<ButtonContainer style={{ width: '60%', maxWidth: 300 }}>
80+
<Button onPress={() => ChooseFile(setFileUri)} text='chooseFile' />
81+
<Button onPress={() => TakePicture(setFileUri)} text='takePicture' />
82+
</ButtonContainer>
83+
</>
84+
)
85+
}
86+
87+
88+
export default PoseEstimation

src/pages/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import ImageClassification from './ImageClassification'
55
import ImageClassificationLiveFeed from './ImageClassificationLiveFeed'
66
import ImageSegmentation from './ImageSegmentation'
77
import ObjectDetection from './ObjectDetection'
8+
import PoseEstimation from './PoseEstimation'
89

910

1011
export interface iPAGES_PROPS {
@@ -20,7 +21,8 @@ export interface iPAGES {
2021
ImageClassification : PAGE,
2122
ImageClassificationLiveFeed : PAGE,
2223
ImageSegmentation : PAGE,
23-
ObjectDetection : PAGE
24+
ObjectDetection : PAGE,
25+
PoseEstimation : PAGE
2426
}
2527

2628

@@ -30,7 +32,8 @@ const PAGES : iPAGES = {
3032
ImageClassification,
3133
ImageClassificationLiveFeed,
3234
ImageSegmentation,
33-
ObjectDetection
35+
ObjectDetection,
36+
PoseEstimation
3437
}
3538

3639

src/services/Translate/en-US.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const EN_US : TRANSLATE = {
2424
ImageClassification: 'Image Classification',
2525
ImageClassificationLiveFeed: 'Image Classification Live',
2626
ImageSegmentation: 'Image Segmentation',
27-
ObjectDetection: 'Object Detection'
27+
ObjectDetection: 'Object Detection',
28+
PoseEstimation: 'Pose Estimation'
2829
}
2930

3031

src/services/Translate/es-ES.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const ES_ES : TRANSLATE = {
2424
ImageClassification: 'Clasificación de Imagen',
2525
ImageClassificationLiveFeed: 'Clasificación de Imagen En Vivo',
2626
ImageSegmentation: 'Segmentación de Imagen',
27-
ObjectDetection: 'Detección de Objetos'
27+
ObjectDetection: 'Detección de Objetos',
28+
PoseEstimation: 'Estimación de Pose'
2829
}
2930

3031

src/services/Translate/pt-BR.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const PT_BR : TRANSLATE = {
2424
ImageClassification: 'Classificação de Imagem',
2525
ImageClassificationLiveFeed: 'Classificação de Imagem Ao Vivo',
2626
ImageSegmentation: 'Segmentação de Imagem',
27-
ObjectDetection: 'Detecção de Objetos'
27+
ObjectDetection: 'Detecção de Objetos',
28+
PoseEstimation: 'Estimativa de Pose'
2829
}
2930

3031

src/services/Translate/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ export type TRANSLATE = {
2222
ImageClassification : string,
2323
ImageClassificationLiveFeed : string,
2424
ImageSegmentation : string,
25-
ObjectDetection : string
25+
ObjectDetection : string,
26+
PoseEstimation : string
2627
}

0 commit comments

Comments
 (0)