-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathApp.tsx
322 lines (309 loc) · 9.19 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import BottomSheet, { BottomSheetScrollView } from "@gorhom/bottom-sheet";
import analytics from "@react-native-firebase/analytics";
import remoteConfig from "@react-native-firebase/remote-config";
import { useState, useEffect, useCallback, useRef, useMemo } from "react";
import {
StyleSheet,
View,
SafeAreaView,
Dimensions,
Alert,
BackHandler,
Image,
TouchableOpacity,
Linking,
StatusBar,
PermissionsAndroid,
} from "react-native";
import RNBootSplash from "react-native-bootsplash";
import { GestureHandlerRootView, FlatList } from "react-native-gesture-handler";
// importing components
import CusCamera from "./components/Camera";
import { Github } from "./components/Icons";
import { H1, H2, H3, Paragraph } from "./components/Typography";
// importing services
import { floatToPercentage } from "./services/math";
import {
isServiceAvailable,
getFlowerImagePrediction,
} from "./services/plantRecog";
const { width } = Dimensions.get("screen");
export default function App() {
const bottomSheetRef = useRef<BottomSheet>(null);
const snapPoints = useMemo(() => ["48%", "100%"], []);
const [appIsReady, setAppIsReady] = useState(false);
const [image, setImage] = useState<string | null>(null);
const [allPredicted, setAllPredicted] = useState<Predictions>([
{
name: "",
score: 0,
},
]);
const [details, setDetails] = useState<PlantDetails>({
images: [],
description: "",
link: "",
loaded: false,
});
const [recognized, setRecognized] = useState<string[]>([]);
// do all permission tasks and initial server requests
useEffect(() => {
(async () => {
await Promise.all([
remoteConfig().setDefaults({
server: "[]",
}),
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, {
title: "Permission to use camera",
message: "We need your permission to use your camera",
buttonPositive: "Ok",
buttonNegative: "Cancel",
buttonNeutral: "Ask Me Later",
}),
]);
await remoteConfig().fetchAndActivate();
setAppIsReady(true);
await analytics().logEvent("app_open", {
time: Date.now(),
});
// Calling the service last to make sure it's not blocking app startup due to Heroku server sleep
const isPlantServiceUp = await isServiceAvailable();
if (!isPlantServiceUp) {
Alert.alert(
"Oh! Snap",
"The service is currently unavailable, please check later!",
[{ text: "Close App", onPress: () => BackHandler.exitApp() }]
);
}
setRecognized(isPlantServiceUp.recognized);
})();
}, []);
const recognizeImage = async (image: string) => {
try {
// Remove old predictions if any
await Promise.all([
analytics().logEvent("predict_image"),
setAllPredicted([
{
name: "Processing",
score: 0,
},
]),
setImage(image),
setDetails({
images: [],
description: "",
link: "",
loaded: false,
}),
]);
// animate bottom sheet to cover whole screen
bottomSheetRef.current?.snapToIndex(1);
// call prediction service with image
const predictPayload = await getFlowerImagePrediction(image);
if (predictPayload !== null) {
setAllPredicted(predictPayload.predictions);
setDetails({
images: [],
description: "",
link: "",
...predictPayload.gyanData,
loaded: true,
});
} else {
return Alert.alert(
"Ops",
"Looks like something bad happened, please try again!"
);
}
} catch (err: any) {
console.log(err.message);
}
};
const renderImages = () => {
return (
<>
<H3 text="Plant Images" />
{!details.loaded ? (
<Paragraph text="Loading..." />
) : (
<FlatList
style={{
borderRadius: 8,
backgroundColor: "#F9F9F9",
marginBottom: 10,
}}
horizontal
showsHorizontalScrollIndicator={false}
data={details.images}
keyExtractor={(_, i) => `${i}`}
renderItem={({ item }) => {
return (
<Image
style={{
width: 100,
height: 160,
resizeMode: "cover",
margin: 4,
borderRadius: 8,
}}
source={{ uri: item }}
/>
);
}}
ListEmptyComponent={
<Paragraph text="Unable to extract images from net!" />
}
/>
)}
</>
);
};
const renderWiki = () => {
return (
<>
<H3 text="Description" />
{!details.loaded ? (
<Paragraph text="Loading..." />
) : (
<>
<Paragraph
text={
details?.description && details?.description?.length === 0
? "Unable to extract details from Wikipedia!"
: details.description
}
/>
{details?.link && details?.link?.length !== 0 && (
<TouchableOpacity
onPress={() => {
if (details.link !== "") {
Linking.openURL(details.link);
}
}}
>
<H3
style={{ color: "blue", marginTop: 0 }}
text="Open in Wikipedia"
/>
</TouchableOpacity>
)}
</>
)}
</>
);
};
const renderOtherPrediction = () => {
if (allPredicted || allPredicted?.length <= 1) {
return null;
}
return (
<>
<H3 text="Other Predictions" />
{allPredicted.slice(1).map((d, i) => {
if (d.score === 0) {
return null;
}
return (
<Paragraph
style={{ marginTop: 0 }}
key={d.name}
text={`Class: ${d.name}, Accuracy: ${floatToPercentage(d.score)}`}
/>
);
})}
</>
);
};
// to avoid flicker remove the splash, when actual app renders after appIsReady changes to "true"
const onLayout = useCallback(async () => {
if (appIsReady) {
RNBootSplash.hide({ fade: true });
}
}, [appIsReady]);
// if app is not ready then render nothing
if (!appIsReady) {
return null;
}
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<SafeAreaView style={styles.container} onLayout={onLayout}>
<StatusBar hidden />
<CusCamera recognizeImage={recognizeImage} />
<BottomSheet ref={bottomSheetRef} index={0} snapPoints={snapPoints}>
<BottomSheetScrollView
contentContainerStyle={styles.scrollViewContainer}
showsVerticalScrollIndicator={false}
>
{image === null ? null : (
<View>
<Image style={styles.plantImage} source={{ uri: image }} />
<H1 text={allPredicted[0].name} />
<Paragraph
text={`Accuracy: ${floatToPercentage(allPredicted[0].score)}`}
/>
{renderImages()}
{renderWiki()}
{renderOtherPrediction()}
</View>
)}
<H2 text="Get Started" />
<Paragraph text="Try taking a photo of your favorite flower, and see what they're called, or Do you already have a flower photo? Open the image gallery to select it." />
<H2 text="About" />
<Paragraph
text={`PlantRecog is an Open Source project, which allows you to know plants with just a click. All the components (service + app + research) used in the project are available on Github. The app can currently recognize ${recognized?.length} plants from there flowers.`}
/>
<TouchableOpacity
style={styles.github}
onPress={async () => {
await analytics().logEvent("github_open");
Linking.openURL("https://github.com/sarthakpranesh/PlantRecog");
}}
>
<Github />
</TouchableOpacity>
</BottomSheetScrollView>
</BottomSheet>
</SafeAreaView>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "black",
display: "flex",
flexDirection: "column",
justifyContent: "flex-start",
alignItems: "center",
},
scrollViewContainer: {
display: "flex",
flexDirection: "column",
justifyContent: "flex-start",
alignContent: "flex-start",
padding: 8,
width,
},
plantImage: {
width: "100%",
height: 200,
resizeMode: "cover",
borderRadius: 8,
},
github: {
width: 40,
height: 40,
borderColor: "black",
borderWidth: 1,
borderRadius: 10,
backgroundColor: "white",
alignSelf: "center",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
margin: 10,
elevation: 4,
},
});