-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
225 lines (201 loc) · 5.67 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import React, { Fragment } from 'react';
import { Button, SafeAreaView, StyleSheet, View, Text, StatusBar } from 'react-native';
import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-react-native';
import { MobilenetDemo } from './components/mobilenet_demo';
import { TestRunner } from './components/tfjs_unit_test_runner';
import { WebcamDemo } from './components/webcam/webcam_demo';
import { RealtimeDemo } from './components/webcam/realtime_demo';
import { DeepLabDemo } from './components/deeplab_demo';
const BACKEND_TO_USE = 'rn-webgl';
export type Screen = 'main' | 'diag' | 'demo' | 'deeplab' | 'test' | 'webcam' | 'realtime';
interface AppState {
isTfReady: boolean;
currentScreen: Screen;
}
export default class App extends React.Component<any,AppState> {
constructor(props: any) {
super(props);
this.state = {
isTfReady: false,
currentScreen: 'main'
};
this.showDemoScreen = this.showDemoScreen.bind(this);
this.showDeepLabScreen = this.showDeepLabScreen.bind(this);
this.showMainScreen = this.showMainScreen.bind(this);
this.showTestScreen = this.showTestScreen.bind(this);
this.showWebcamDemo= this.showWebcamDemo.bind(this);
this.showRealtimeDemo= this.showRealtimeDemo.bind(this);
}
async componentDidMount() {
await tf.setBackend(BACKEND_TO_USE);
// Wait for tf to be ready.
console.log('waiting...')
await tf.ready();
console.log('yeah baby...tf ready!')
// Signal to the app that tensorflow.js can now be used.
this.setState({
isTfReady: true,
});
}
showDiagnosticScreen() {
this.setState({ currentScreen: 'diag' });
}
showDemoScreen() {
this.setState({ currentScreen: 'demo' });
}
showDeepLabScreen() {
this.setState({ currentScreen: 'deeplab' });
}
showMainScreen() {
this.setState({ currentScreen: 'main' });
}
showTestScreen() {
this.setState({ currentScreen: 'test' });
}
showWebcamDemo() {
this.setState({ currentScreen: 'webcam' });
}
showRealtimeDemo() {
this.setState({ currentScreen: 'realtime' });
}
renderDeepLabScreen() {
const image = require('./assets/images/catsmall.jpg');
return <Fragment>
<DeepLabDemo
image={image}
returnToMain={this.showMainScreen} />
</Fragment>;
}
renderDemoScreen() {
const image = require('./assets/images/catsmall.jpg');
return <Fragment>
<MobilenetDemo
image={image}
returnToMain={this.showMainScreen} />
</Fragment>;
}
renderTestScreen() {
return <Fragment>
<TestRunner backend={BACKEND_TO_USE} />
</Fragment>;
}
renderWebcamDemo() {
return <Fragment>
<WebcamDemo returnToMain={this.showMainScreen}/>
</Fragment>;
}
renderMainScreen() {
return <Fragment>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Diagnostic</Text>
<Button
onPress={this.showDiagnosticScreen}
title='Show Diagnostic Screen'
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>DeepLab</Text>
<Button
onPress={this.showDeepLabScreen}
title='Show DeepLab Screen'
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Demo</Text>
<Button
onPress={this.showDemoScreen}
title='Show Demo Screen'
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Unit tests</Text>
<Button
testID='unit-test-btn'
accessibilityLabel='unit-test-btn'
onPress={this.showTestScreen}
title='Show Test Screen'
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Webcam Demo</Text>
<Button
onPress={this.showWebcamDemo}
title='Show Webcam Demo'
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Realtime Webcam Demo</Text>
<Button
onPress={this.showRealtimeDemo}
title='Show Realtime Webcam Demo'
/>
</View>
</Fragment>;
}
renderLoadingTF() {
return <Fragment>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Loading TF</Text>
</View>
</Fragment>;
}
renderRealtimeDemo() {
return <Fragment>
<RealtimeDemo returnToMain={this.showMainScreen}/>
</Fragment>;
}
renderContent() {
const { currentScreen, isTfReady } = this.state;
if (isTfReady) {
switch (currentScreen) {
case 'main':
return this.renderMainScreen();
case 'deeplab':
return this.renderDeepLabScreen();
case 'demo':
return this.renderDemoScreen();
case 'test':
return this.renderTestScreen();
case 'webcam':
return this.renderWebcamDemo();
case 'realtime':
return this.renderRealtimeDemo();
default:
return this.renderMainScreen();
}
} else {
return this.renderLoadingTF();
}
}
render() {
return (
<Fragment>
<StatusBar barStyle='dark-content' />
<SafeAreaView>
<View style={styles.body}>
{this.renderContent()}
</View>
</SafeAreaView>
</Fragment>
);
}
}
const styles = StyleSheet.create({
scrollView: {
backgroundColor: 'white',
},
body: {
backgroundColor: 'white',
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: 'black',
marginBottom: 6,
},
});