-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
118 lines (101 loc) · 3.63 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View, Dimensions, TouchableOpacity, Image } from 'react-native';
import * as FaceDetector from 'expo-face-detector';
import { Camera } from 'expo-camera';
import { Face, FaceDetectionResult, PermissionResponse } from 'expo-camera/build/Camera.types';
import Ladmarks from './components/Landmarks'
import SmilingText from './components/SmilingText'
import ClownNose from './components/ClownNose'
const { width, height } = Dimensions.get('window')
export default function App() {
const [hasPermission, setHasPermission] = useState<boolean>(false);
const [type, setType] = useState(Camera.Constants.Type.front);
const [faceData, setFaceData] = useState<Face | null>(null)
useEffect(() => {
(async () => {
const { status }: PermissionResponse = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
const handleFacesDetected = (param: FaceDetectionResult) => {
if (param.faces.length) {
setFaceData(param.faces[0])
} else {
setFaceData(null)
}
}
const handleCamRotate = () => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}
return (
<View style={styles.container}>
<Camera style={styles.camera}
type={type}
onFacesDetected={face => handleFacesDetected(face)}
faceDetectorSettings={{
mode: FaceDetector.Constants.Mode.accurate,
detectLandmarks: FaceDetector.Constants.Landmarks.all,
runClassifications: FaceDetector.Constants.Classifications.all,
minDetectionInterval: 100,
tracking: true,
}}
>
{/* Orelha esquerda */}
<Ladmarks x={faceData?.leftEarPosition.x} y={faceData?.leftEarPosition.y} />
{/* Orelha direita */}
<Ladmarks x={faceData?.rightEarPosition.x} y={faceData?.rightEarPosition.y} />
{/* Olho esquerdo */}
<Ladmarks x={faceData?.leftEyePosition.x} y={faceData?.leftEyePosition.y} />
{/* Olho direito */}
<Ladmarks x={faceData?.rightEyePosition.x} y={faceData?.rightEyePosition.y} />
{/* Nariz */}
{(faceData?.smilingProbability || 0) > 0.8 ?
<ClownNose x={faceData?.noseBasePosition.x} y={faceData?.noseBasePosition.y} /> :
<Ladmarks x={faceData?.noseBasePosition.x} y={faceData?.noseBasePosition.y} />
}
{/* Lado esquerdo boca */}
<Ladmarks x={faceData?.leftMouthPosition.x} y={faceData?.leftMouthPosition.y} />
{/* Lado direito boca */}
<Ladmarks x={faceData?.rightMouthPosition.x} y={faceData?.rightMouthPosition.y} />
<SmilingText value={faceData?.smilingProbability} />
<TouchableOpacity style={styles.camRotateContainer} onPress={() => handleCamRotate()}>
<Image style={styles.camRotate} source={require('./assets/reverse-camera.jpeg')} width={width * 0.15} height={width * 0.15} />
</TouchableOpacity>
</Camera>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
camera: {
flex: 1,
width: width,
height: height
},
camRotateContainer: {
alignSelf: 'flex-end',
marginTop: height - (width * 0.15) - 32,
marginHorizontal: 32,
opacity: 0.5
},
camRotate: {
width: width * 0.15,
height: width * 0.15,
backgroundColor: '#fff',
borderRadius: 8
}
});