|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import React, { Fragment } from 'react'; |
| 19 | +import { Button, SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar } from 'react-native'; |
| 20 | + |
| 21 | +import * as tf from '@tensorflow/tfjs'; |
| 22 | +import '@tensorflow/tfjs-react-native'; |
| 23 | + |
| 24 | +import { Diagnostic } from './components/diagnostic'; |
| 25 | +import { MobilenetDemo } from './components/mobilenet_demo'; |
| 26 | + |
| 27 | +export type Screen = 'main' | 'diag' | 'demo'; |
| 28 | + |
| 29 | +interface AppState { |
| 30 | + isTfReady: boolean; |
| 31 | + currentScreen: Screen; |
| 32 | +} |
| 33 | + |
| 34 | +export class App extends React.Component<{}, AppState> { |
| 35 | + constructor(props: {}) { |
| 36 | + super(props); |
| 37 | + this.state = { |
| 38 | + isTfReady: false, |
| 39 | + currentScreen: 'main' |
| 40 | + }; |
| 41 | + |
| 42 | + this.showDiagnosticScreen = this.showDiagnosticScreen.bind(this); |
| 43 | + this.showDemoScreen = this.showDemoScreen.bind(this); |
| 44 | + this.showMainScreen = this.showMainScreen.bind(this); |
| 45 | + } |
| 46 | + |
| 47 | + async componentDidMount() { |
| 48 | + await tf.setBackend('rn-webgl'); |
| 49 | + await tf.ready(); |
| 50 | + this.setState({ |
| 51 | + isTfReady: true, |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + showDiagnosticScreen() { |
| 56 | + this.setState({ currentScreen: 'diag' }); |
| 57 | + } |
| 58 | + |
| 59 | + showDemoScreen() { |
| 60 | + this.setState({ currentScreen: 'demo' }); |
| 61 | + } |
| 62 | + |
| 63 | + showMainScreen() { |
| 64 | + this.setState({ currentScreen: 'main' }); |
| 65 | + } |
| 66 | + |
| 67 | + renderMainScreen() { |
| 68 | + return <Fragment> |
| 69 | + <View style={styles.sectionContainer}> |
| 70 | + <Text style={styles.sectionTitle}>Diagnostic</Text> |
| 71 | + <Button |
| 72 | + onPress={this.showDiagnosticScreen} |
| 73 | + title='Show Diagnostic Screen' |
| 74 | + /> |
| 75 | + </View> |
| 76 | + <View style={styles.sectionContainer}> |
| 77 | + <Text style={styles.sectionTitle}>Demo</Text> |
| 78 | + <Button |
| 79 | + onPress={this.showDemoScreen} |
| 80 | + title='Show Demo Screen' |
| 81 | + /> |
| 82 | + </View> |
| 83 | + </Fragment>; |
| 84 | + } |
| 85 | + |
| 86 | + renderDiagnosticScreen() { |
| 87 | + return <Fragment> |
| 88 | + <Diagnostic returnToMain={this.showMainScreen} /> |
| 89 | + </Fragment>; |
| 90 | + } |
| 91 | + |
| 92 | + renderDemoScreen() { |
| 93 | + const image = require('./assets/images/catsmall.jpg'); |
| 94 | + return <Fragment> |
| 95 | + <MobilenetDemo |
| 96 | + image={image} |
| 97 | + returnToMain={this.showMainScreen} /> |
| 98 | + </Fragment>; |
| 99 | + } |
| 100 | + |
| 101 | + renderLoadingTF() { |
| 102 | + return <Fragment> |
| 103 | + <View style={styles.sectionContainer}> |
| 104 | + <Text style={styles.sectionTitle}>Loading TF</Text> |
| 105 | + </View> |
| 106 | + </Fragment>; |
| 107 | + } |
| 108 | + |
| 109 | + renderContent() { |
| 110 | + const { currentScreen, isTfReady } = this.state; |
| 111 | + if (isTfReady) { |
| 112 | + switch (currentScreen) { |
| 113 | + case 'main': |
| 114 | + return this.renderMainScreen(); |
| 115 | + case 'diag': |
| 116 | + return this.renderDiagnosticScreen(); |
| 117 | + case 'demo': |
| 118 | + return this.renderDemoScreen(); |
| 119 | + default: |
| 120 | + return this.renderMainScreen(); |
| 121 | + } |
| 122 | + } else { |
| 123 | + return this.renderLoadingTF(); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + render() { |
| 129 | + return ( |
| 130 | + <Fragment> |
| 131 | + <StatusBar barStyle='dark-content' /> |
| 132 | + <SafeAreaView> |
| 133 | + <ScrollView |
| 134 | + contentInsetAdjustmentBehavior='automatic' |
| 135 | + style={styles.scrollView}> |
| 136 | + <View style={styles.body}> |
| 137 | + {this.renderContent()} |
| 138 | + </View> |
| 139 | + </ScrollView> |
| 140 | + </SafeAreaView> |
| 141 | + </Fragment> |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +const styles = StyleSheet.create({ |
| 147 | + scrollView: { |
| 148 | + backgroundColor: 'white', |
| 149 | + }, |
| 150 | + body: { |
| 151 | + backgroundColor: 'white', |
| 152 | + }, |
| 153 | + sectionContainer: { |
| 154 | + marginTop: 32, |
| 155 | + paddingHorizontal: 24, |
| 156 | + }, |
| 157 | + sectionTitle: { |
| 158 | + fontSize: 24, |
| 159 | + fontWeight: '600', |
| 160 | + color: 'black', |
| 161 | + marginBottom: 6, |
| 162 | + }, |
| 163 | +}); |
0 commit comments