-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
199 lines (181 loc) · 5.47 KB
/
App.js
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
import React from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
Image,
CameraRoll,
Dimensions,
Button,
} from 'react-native';
import {
StackNavigator,
} from 'react-navigation';
import { Constants } from 'expo';
import {
BruiseScreen,
BurnScreen,
CutScreen,
} from './InfoScreen';
class HomeScreen extends React.Component {
state = {
image: null,
uploading: false,
injury: null,
isLoading: false
};
render() {
const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<Image source={require('./assets/icons/name-icon.png')}
style={{ width: 300, height: 200 }}/>
<View style={{ flexDirection: 'row' }}>
<Button
title="Take a pic"
onPress={this._onTake}
/>
<Text style={styles.space}>lo</Text>
<Button
title="Pick a pic"
onPress={this._onPick}
/>
</View>
<View>
{this.state.injury==="No Injury Found"&&!this.state.isLoading ? <Text>{this.state.injury}</Text> : null}
{this.state.isLoading ? <Image source={require('./assets/icons/loading-small-circle.gif')}/> : null}
</View>
</View>
);
}
_onPick = async () => {
const {
cancelled,
uri,
} = await Expo.ImagePicker.launchImageLibraryAsync();
if (!cancelled) {
this._onSend(uri);
}
}
_onTake = async () => {
const {
cancelled,
uri,
} = await Expo.ImagePicker.launchCameraAsync();
if (!cancelled) {
this._onSend(uri);
}
}
_onSend = async (uri) => {
const { navigate } = this.props.navigation;
await this.setState({isLoading: true});
uploadResponse = await uploadImageAsync(uri);
uploadResult = await uploadResponse.json();
await this.setState({ image: uploadResult.location });
await this.getJSON(this.state.image);
await this.setState({isLoading: false});
await navigate(this.state.injury, { navigate });
}
getJSON = async (uri) => {
var object = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
requests: [
{
image: {
source: {
imageUri: uri
}
},
features: [
{
type: "WEB_DETECTION",
maxResults: 5
}
]
}
]
})
};
var _desc = '';
const request = await fetch('https://vision.googleapis.com/v1/images:annotate?key=AIzaSyB97a-l-FZyGXWiOBwze-EhGHIPxQazeNc', object);
const result = await request.json();
const info = result.responses[0].webDetection.webEntities;
this.filter(info);
}
//takes in unfiltered json array
filter = async(json) => {
var _desc = new Array(json.length);
for (var i = 0; i < json.length; i++) {
if (json[i].description === "Abrasion" || json[i].description === "Bruise") {
this.setState({ injury: "Bruise" });
return;
}
else if (json[i].description === "Burn") {
this.setState({ injury: "Burn" });
return;
}
else if (json[i].description === "Blood" || json[i].description === "Bleeding" || json[i].description === "Cut") {
this.setState({ injury: "Cut" });
return;
}
}
this.setState({ injury: "No Injury Found"});
}
}
const MainNavigator = StackNavigator({
Home: { screen: HomeScreen },
Burn: { screen: BurnScreen },
Cut: { screen: CutScreen },
Bruise: { screen: BruiseScreen },
// Bleeding: { screen: BleedingScreen },
});
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<MainNavigator style={{ width: Dimensions.get('window').width }} />
</View>
);
}
}
async function uploadImageAsync(uri) {
let apiUrl = 'https://file-upload-example-backend-dkhqoilqqn.now.sh/upload';
let uriParts = uri.split('.');
let fileType = uri[uri.length - 1];
let formData = new FormData();
formData.append('photo', {
uri,
name: `photo.${fileType}`,
type: `image/${fileType}`,
});
let options = {
method: 'POST',
body: formData,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
};
return fetch(apiUrl, options);
}
const styles = StyleSheet.create({
button: {
padding: '5px',
margin: '20px',
backgroundColor: '#ddd',
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-around',
},
space: {
color: 'white',
}
});