-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
149 lines (125 loc) · 3.56 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
import React, { useState } from 'react';
import { View, Text, StyleSheet, Image, Button } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import axios from 'axios';
function App() {
const [pickedImagePath, setPickedImagePath] = useState('');
const [prediction, setPrediction] = useState(null);
const showImagePicker = async () => {
const permissionResult = await ImagePicker.requestMediaLibraryPermissionsAsync();
if (permissionResult.granted === false) {
alert("You've refused to allow this app to access your photos!");
return;
}
const result = await ImagePicker.launchImageLibraryAsync();
console.log(result);
if (!result.cancelled) {
setPickedImagePath(result.uri);
console.log(result.uri);
}
}
const openCamera = async () => {
const permissionResult = await ImagePicker.requestCameraPermissionsAsync();
if (permissionResult.granted === false) {
alert("You've refused to allow this app to access your camera!");
return;
}
const result = await ImagePicker.launchCameraAsync();
console.log(result);
if (!result.cancelled) {
setPickedImagePath(result.uri);
console.log(result.uri);
}
}
const removeImage = () => {
setPickedImagePath('');
setPrediction(null);
}
const sendImageToServer = async (imageUri) => {
try {
const formData = new FormData();
const image = {
uri: imageUri,
type: 'image/jpeg',
name: 'file',
};
formData.append('file', image);
const response = await axios.post(
'https://us-central1-comp700.cloudfunctions.net/predict',
formData,
{
headers: {
'Content-Type': 'multipart/form-data',
},
}
);
setPrediction(response.data);
console.log(response.data);
} catch (error) {
console.error('Error sending image:', error);
}
}
const sendToServerButtonPressed = () => {
if (pickedImagePath !== '') {
sendImageToServer(pickedImagePath);
} else {
alert('No image selected.');
}
}
return (
<View style={styles.screen}>
{/* Title */}
<Text style={styles.title}>MY PSORIASIS DETECTION APP</Text>
<View style={styles.buttonContainer}>
<Button onPress={showImagePicker} title="Select an image" />
<Button onPress={openCamera} title="Open camera" />
</View>
<View style={styles.imageContainer}>
{pickedImagePath !== '' && (
<>
<Image source={{ uri: pickedImagePath }} style={styles.image} />
<Button onPress={removeImage} title="Remove Image" color="red" />
<Button
onPress={sendToServerButtonPressed}
title="Predict"
color="green"
/>
</>
)}
</View>
{prediction && (
<View style={styles.predictionContainer}>
<Text>Class: {prediction.Class}</Text>
<Text>Confidence: {prediction.Confidence}%</Text>
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: '#87CEEB',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginTop: 20, // Add margin to push the title down a bit
},
buttonContainer: {
width: 400,
flexDirection: 'row',
justifyContent: 'space-around',
},
imageContainer: {
padding: 30,
},
image: {
width: 400,
height: 300,
resizeMode: 'cover',
},
});
export default App;