Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
161 lines (152 loc) · 5.28 KB
/
Copy pathApp.js
File metadata and controls
161 lines (152 loc) · 5.28 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, { useState, useEffect, useRef } from 'react';
import { Text, View, StyleSheet, Platform, Image, Button } from 'react-native';
import * as Permissions from 'expo-permissions';
import { Camera, Constants } from 'expo-camera';
import * as tf from '@tensorflow/tfjs';
import * as handpose from '@tensorflow-models/handpose';
import * as fp from 'fingerpose';
import { cameraWithTensors } from '@tensorflow/tfjs-react-native';
//import '@tensorflow/tfjs-backend-cpu';
import '@tensorflow/tfjs-react-native';
//import '@tensorflow/tfjs-backend-webgl';
//disable yellow warnings on EXPO client!
//console.disableYellowBox = true;
//YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader', 'RNDeviceInfo', 'Warning: An update']);
//LogBox.ignoreWarnings = true;
export default function App() {
//RAF ID
let requestAnimationFrameId = 0;
const [tfReady, setTfReady] = useState(false);
const [hpmReady, setHpmReady] = useState(false);
const [handposeModel, setHandposeModel] = useState(null);
//performance hacks (Platform dependent)
const textureDims =
Platform.OS === 'ios'
? { width: 1080, height: 1920 }
: { width: 1600, height: 1200 };
const tensorDims = { width: 152, height: 200 };
const [emoji, setEmoji] = useState(null);
//TF Camera Decorator
const TensorCamera = cameraWithTensors(Camera);
useEffect(() => {
const init = async () => {
if (!hpmReady && !tfReady) {
console.log("Initializing...");
tf.device_util.isMobile = () => true;
tf.device_util.isBrowser = () => false;
tf.ready()
.then(() => {
setTfReady(true);
console.log(tf.getBackend());
handpose.load()
.then(model => {
console.log("hand pose model: ", model)
setHandposeModel(model)
setHpmReady(true)
console.log("Tf and handpose model initialized");
})
.catch(e2 => {
console.error("Hand pose model load error", e2.message, e2.stack);
})
})
.catch(e1 => {
console.error("TF init error", e1.message, e1.stack);
})
}
}
init();
},[])
useEffect(() => {
return () => {
console.log("Cancelling animation frame: ", requestAnimationFrameId);
cancelAnimationFrame(requestAnimationFrameId);
};
}, [requestAnimationFrameId]);
function handleCameraStreamV2(images, updatePreview, gl) {
const loop = async () => {
if (tfReady && hpmReady && handposeModel){
const nextImageTensor = images.next().value
console.log("imageAsTensors: ", JSON.stringify(nextImageTensor))
if (nextImageTensor){
console.log("Calling estimateHands...")
handposeModel.estimateHands(nextImageTensor)
.then(prediction => {
console.log("prediction: ", JSON.stringify(prediction));
setEmoji(null);
if (prediction && prediction.length > 0) {
const GE = new fp.GestureEstimator([fp.Gestures.ThumbsUpGesture]);
const gesture = GE.estimate(prediction[0].landmarks, 7.5);
console.log("Gesture: ", gesture);
if (gesture.gestures !== undefined && gesture.gestures.length > 0) {
const confidence = gesture.gestures.map(
(prediction) => prediction.confidence
);
const mxConfidence = confidence.indexOf(
Math.max.apply(null, confidence)
);
const emojiName = gesture.gestures[mxConfidence].name;
console.log("emoji name: ", emojiName)
setEmoji(emojiName);
}
}
console.log('Looping...')
requestAnimationFrameId = requestAnimationFrame(loop);
})
.catch(e1 => {
console.error("Prediction error", e1.message, e1.stack);
})
}
}
}
loop();
}
return (
<View style={styles.container}>
{hpmReady && tfReady ?
(<TensorCamera
style={styles.camera}
type={Camera.Constants.Type.back}
zoom={0}
cameraTextureHeight={textureDims.height}
cameraTextureWidth={textureDims.width}
resizeHeight={tensorDims.height}
resizeWidth={tensorDims.width}
resizeDepth={3}
onReady={handleCameraStreamV2}
autorender={true}
/>
) : (<Text style={styles.camera}>{"Initializing..."}</Text>)}
{emoji !== null ? (
<Image
style={styles.tinyLogo}
source={require('./assets/thumbs_up.png')}
/>
) : (
<Text>{""}</Text>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
camera: {
position: 'absolute',
left: 90,
top: 100,
width: 500 / 2,
height: 800 / 2,
zIndex: 1,
borderWidth: 2,
borderColor: 'black',
borderRadius: 0,
},
tinyLogo: {
width: 50,
height: 50,
}
});