-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
584 lines (482 loc) · 22.6 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import { StatusBar } from 'expo-status-bar';
import { Alert, Modal,Linking, Pressable, ScrollView, ActivityIndicator, Text, View, SafeAreaView, TouchableOpacity, Image } from 'react-native';
import React, { useEffect, useRef, useState } from 'react';
import { Camera } from 'expo-camera';
import { DataTable } from 'react-native-paper';
import ConfettiCannon from 'react-native-confetti-cannon';
import { FontAwesome } from '@expo/vector-icons';
import * as MediaLibrary from 'expo-media-library';
import styles from './app/config/styles';
import colors from './app/config/colors';
import AsyncStorage from '@react-native-async-storage/async-storage';
import ProgressCircle from 'react-native-progress-circle'
import { FlatGrid } from 'react-native-super-grid';
export default function App() {
//Setting all the states the app can have. Here we store the score, whether we are loading the data from API's and the photo's we take
const [Loading, setLoading] = useState(false);
const [modalVisible, setModalVisible] = useState(false);
const [data, setData] = useState([{'none':'none'}]);
const [assignment, setAssignment] = useState([]);
const [PercentageObjectsSeen, setPercentageObjectsSeen] = useState([]);
const [EmojiSeen, setEmojiSeen] = useState([]);
const [numberrefresh, setNumberrefresh] = useState(-1);//initializing at -1 so that when we get the initial instruction we end up at 0
const [score, setScore] = useState(0); //setting to 1 so on the initial fetch for an instruction we end up at 0 points
const [hasCameraPermission, setHasCameraPermission] = useState();
const [hasMediaLibraryPermission, setHasMediaLibraryPermission] = useState();
const [photo, setPhoto] = useState();
//Extracting some pictures from API response
var emoji = assignment[Object.keys(assignment)[0]];
const DetectorParameter = Object.keys(assignment)[0]
const GetPercentageObjectsSeen = async () => {
//This is an empty list "keys" where we get all the values from Asyncstorage
let keys = []
try {
keys = await AsyncStorage.getAllKeys()
} catch(e) {
// read key error
}
// That list of keys above contains more than just emoji's and also has ugly quotation marks around each key
//THerefor we remove the first and last character of each key.
var justemoji = []
keys.forEach((item) => justemoji.push(item.slice(1, item.length-1)));
for( var i = 0; i < justemoji.length; i++){
// the key "score" is also part of "all keys", so this is a very ugly way to pop that one out the list too.
if ( justemoji[i] === 'cor') {
justemoji.splice(i, 1);
i--;
}
} // finally, if the emoji is empty we remove it from the list as well, so the gridview ends up nice and pretty.
for( var i = 0; i < justemoji.length; i++){
if ( justemoji[i] === '') {
justemoji.splice(i, 1);
i--;
}
}
// We calculate the progress based on the keys list not emoji list. Because some assignments have no emoji but you do want to count them.
setPercentageObjectsSeen(Math.round((((keys.length)/80)*100)))
//The list of emoji's is passed to the function that renders the grid with observed emojis in the settings view.
setEmojiSeen(justemoji)
};
let RenderGrid = () => {
if (EmojiSeen == 0) {
return <View>
<Text style={styles.ProfileSubHeading}>🤳 How to play</Text>
<Text style={styles.modalText}>Photograph objects to earn points. The more things you fit in a picture the more points you score.</Text>
<Text style={styles.modalText}>If you cannot find the object then you can refresh <FontAwesome name="refresh" size={12} color="black" /> your assignment for a 50 point penalty.</Text>
<Text style={styles.modalText}>Good lighting, getting up close and sharp pictures photos greatly improve your chances of detecting the correct object.</Text>
<Pressable
style={[styles.button, styles.TakeAnotherPhotoButton]}
onPress={() => setModalVisible(!modalVisible)}
>
<Text style={styles.textStyle}> Play </Text>
</Pressable>
</View>
} else {
return <View>
<View style={{alignItems: 'center', padding: 20}}>
<ProgressCircle
style= {styles.ProgressBar}
percent={PercentageObjectsSeen}
radius={75}
borderWidth={8}
color={colors.primary}
shadowColor={colors.grey}
bgColor={colors.white}
>
<Text style={{ fontSize: 30,fontWeight: "bold" }}>{PercentageObjectsSeen}%</Text>
<Text style={{ fontSize: 15,fontWeight: "bold" }}>found</Text>
<Text style={{ fontSize: 15,fontWeight: "bold" }}>{score -(numberrefresh*50)} points</Text>
</ProgressCircle>
</View>
<Text style={styles.ProfileSubHeading}> 🏆 Your progress</Text>
<Text>
<Text style={styles.modalText}>You have earned</Text>
<Text style={{fontWeight: "bold"}}> {score -(numberrefresh*50)}</Text>
<Text style={styles.modalText}> points so far and found</Text>
<Text style={{fontWeight: "bold"}}> {PercentageObjectsSeen}%</Text>
<Text style={styles.modalText}> of all objects in the game.</Text>
</Text>
<Text style={styles.ProfileSubHeading}>🤳 Did you know</Text>
<Text style={styles.modalText}>That you get 100 extra points for each additional object in a picture</Text>
<Text style={styles.ProfileSubHeading}>👀 Objects found so far</Text>
<Text style={styles.modalText}>These are the objects you have seen at least once in Photo Scavenger!</Text>
<FlatGrid
itemDimension={50}
data={EmojiSeen}
renderItem={({ item }) => (<Text style={styles.EmojiGrid}> {item} </Text>)}
/>
<Pressable
style={[styles.button, styles.TakeAnotherPhotoButton]}
onPress={() => setModalVisible(!modalVisible)}
>
<Text style={styles.textStyle}>Find {80-EmojiSeen.length} more objects</Text>
</Pressable>
</View>
}
};
const saveScore = async() => {
try{
await AsyncStorage.setItem("score", JSON.stringify(score-(numberrefresh*50)));
console.log("Saved score to device")
console.log(score-(numberrefresh*50))
} catch (err){
alert(err);
}
};
const SaveObjectSeen = async(ObjectSeen) => {
try{
await AsyncStorage.setItem(JSON.stringify(ObjectSeen), JSON.stringify(ObjectSeen));
console.log("Saved Emoji to device")
console.log("Added this emoji to the store")
console.log(ObjectSeen)
GetPercentageObjectsSeen()
} catch (err){
alert(err);
}
};
// retrieving score from device memory
const retrieveSavedScore = async() => {
try{
let storedscore = await AsyncStorage.getItem("score");
if (storedscore !== null) {
setScore(JSON.parse(storedscore));
console.log("retrieved score from storage")
console.log(storedscore)
}
} catch (err){
alert(err);
}
};
function CountRefresh() {
setNumberrefresh(numberrefresh + 1)
}
function NextLevel(){
SaveObjectSeen(emoji)
setScore((score + 250)+ (data.OtherObjectsDetected.length * 100));
setNumberrefresh(0)
console.log("Reset function ran");
// setScore((score - 250-(numberrefresh*35)))
setPhoto(undefined);
FreeCallAssignmentAPI();
}
// Here starts the part where we take the picture
let cameraRef = useRef();
let CallAssignmentAPI = async () => {
fetch('https://photoscavenger.vdotvo9a4e2a6.eu-central-1.cs.amazonlightsail.com/v2/newassignment/'+score)
.then((response) => response.json())
.then((json) => setAssignment(json))
.catch((error) => console.error(error))
CountRefresh();
};
let FreeCallAssignmentAPI = async () => {
saveScore();
fetch('https://photoscavenger.vdotvo9a4e2a6.eu-central-1.cs.amazonlightsail.com/v2/newassignment/'+score)
.then((response) => response.json())
.then((json) => setAssignment(json))
.catch((error) => console.error(error))
};
// This use effect is used 1x on app load, to get the first asignment and fetch camera permissions if we don't have them.
useEffect(() => {
GetPercentageObjectsSeen()
setNumberrefresh(0);
retrieveSavedScore();
CallAssignmentAPI();
setModalVisible(!modalVisible);
(async () => {
const cameraPermission = await Camera.requestCameraPermissionsAsync();
const mediaLibraryPermission = await MediaLibrary.requestPermissionsAsync();
setHasCameraPermission(cameraPermission.status === "granted");
setHasMediaLibraryPermission(mediaLibraryPermission.status === "granted");
})();
}, []);
// IF no permission is given to access the camera this text is shown
if (hasCameraPermission === undefined) {
return <Text>Requesting permissions...</Text>
} else if (!hasCameraPermission) {
return <View style={{alignItems: 'center',flex: 1,justifyContent: 'center'}}>
<Image source={require('./app/assets/AppIcon.png')} style={styles.logo} />
<Text style={styles.ProfileHeading}>Oops</Text>
<Text style={styles.ProfileSubHeading}>⚠️ Permission for camera not granted ⚠️ </Text>
<Text>Please grant permission in the settings</Text></View>
}
let CallDetectionAPI = (image) => {
var formdata = new FormData();
formdata.append('file', {uri: image.uri, name: 'picture.jpg', type: 'image/jpg'});
fetch('https://photoscavenger.vdotvo9a4e2a6.eu-central-1.cs.amazonlightsail.com/v2/uploadfile/'+DetectorParameter, {
method: 'POST',
body: formdata
})
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
};
let takePic = async () => {
let options = {
quality: 1,
base64: true,
exif: false,
};
let newPhoto = await cameraRef.current.takePictureAsync(options);
saveScore();
setPhoto(newPhoto);
setLoading(true);
CallDetectionAPI(newPhoto);
WasAssignmentFound(data);
};
if (photo) {
let savePhoto = () => {
MediaLibrary.saveToLibraryAsync(photo.uri).then(() => {
setPhoto(undefined);
});
};
//This function is a bit of a mess, but it loops over the array of objects that the AI model detected in an image
//The reason we have to try/catch is because when the screen is loaded the 'data' object is not available yet, and thus the
// function fails. after a second the data is available and all is fine. Not quite sure how to clean this up
let GetObjectsDetected = (items_to_render) => {
try {
console.log('Printing the objects found in the picture')
console.log(items_to_render);
return items_to_render.map(x=>
<DataTable.Row>
<DataTable.Cell>- {x}</DataTable.Cell>
<DataTable.Cell numeric>
<Text style={styles.tableGood}>
+ 100
</Text>
</DataTable.Cell>
</DataTable.Row>
);
} catch (error) {
//console.error(error);
}
};
function WasAssignmentFound() {
try {
foundItem = data.OtherObjectsDetected.includes(DetectorParameter)
if (foundItem === true) {
console.log('✅ Photofound is equal to true');
return (
<>
<ConfettiCannon count={200} fallspeed={2000} origin={{x: -50, y: -50}} fadeOut={true} autoStartDelay={500}/>
<ScrollView showsVerticalScrollIndicator={false} showsHorizontalScrollIndicator={false}>
<DataTable>
<DataTable.Header>
<DataTable.Title style={styles.tableBold}></DataTable.Title>
<DataTable.Title> </DataTable.Title>
<DataTable.Title numeric>Points</DataTable.Title>
</DataTable.Header>
<DataTable.Row>
<DataTable.Cell >
<Text>
{emoji} {Object.keys(assignment)[0]} found
</Text>
</DataTable.Cell>
<DataTable.Cell numeric>
<Text style={styles.tableGood}>
+ 250
</Text>
</DataTable.Cell>
</DataTable.Row>
<DataTable.Row>
<DataTable.Cell>
<Text > All items in your picture: </Text>
</DataTable.Cell>
</DataTable.Row>
{GetObjectsDetected(data.OtherObjectsDetected)}
<DataTable.Row>
<DataTable.Cell>{numberrefresh}x refreshed</DataTable.Cell>
<DataTable.Cell numeric>
<Text style={styles.tableBad}>
- {numberrefresh*50}
</Text>
</DataTable.Cell>
</DataTable.Row>
<DataTable.Row>
<DataTable.Cell></DataTable.Cell>
<DataTable.Cell numeric></DataTable.Cell>
</DataTable.Row>
<DataTable.Row style={styles.tableBold}>
<DataTable.Cell>
<Text style={styles.tableBold}>
New Score :
</Text>
</DataTable.Cell>
<DataTable.Cell></DataTable.Cell>
<DataTable.Cell numeric>
<Text style={styles.tableBold}>
{(score + 250 + (data.OtherObjectsDetected.length * 100) - (numberrefresh * 50))}
</Text>
</DataTable.Cell>
</DataTable.Row>
</DataTable>
</ScrollView>
<View style={styles.ButtonAreaScoreView}>
<TouchableOpacity style={styles.SaveOrDiscard} onPress={savePhoto}>
<FontAwesome name="save" size={24} color="black" />
</TouchableOpacity>
<TouchableOpacity style={styles.TakeNextPhotoButton} onPress={() => NextLevel()} >
<Text>Next!</Text>
</TouchableOpacity>
</View>
</>
)
} else {
console.log('⛔️ Photofound is not equal to true');
return (
<>
<ScrollView showsVerticalScrollIndicator={false} showsHorizontalScrollIndicator={false}>
<DataTable>
<DataTable.Header>
<DataTable.Title></DataTable.Title>
<DataTable.Title> </DataTable.Title>
<DataTable.Title numeric></DataTable.Title>
</DataTable.Header>
<DataTable.Row>
<DataTable.Cell >
<Text style={styles.tableBold}>
⛔️ {Object.keys(assignment)[0]} not found
</Text>
</DataTable.Cell>
</DataTable.Row>
<DataTable.Row style={styles.tableBold}>
<DataTable.Cell>
<Text style={styles.tableBold}>
</Text>
</DataTable.Cell>
<DataTable.Cell numeric>
<Text style={styles.tableBold}>
Current Score: {score-(numberrefresh*50)}
</Text></DataTable.Cell>
</DataTable.Row>
</DataTable>
</ScrollView>
<View style={styles.ButtonAreaScoreView}>
<Text style={styles.HelperText} >💡 If you struggle detecting objects. Try to take another picture with good lighting conditions, from close up to the object and holding the camera steady.</Text>
</View>
<View style={styles.ButtonAreaScoreView}>
<TouchableOpacity style={styles.SaveOrDiscard} onPress={savePhoto}>
<FontAwesome name="save" size={24} color="black" />
</TouchableOpacity>
<TouchableOpacity style={styles.TakeAnotherPhotoButton} onPress={() => setPhoto(undefined)} >
<Text>Retry</Text>
</TouchableOpacity>
</View>
</>)
}
} catch (error) {
//console.error(error);
}
};
// This is what is shown after taking a photos
return (
<SafeAreaView style={styles.container}>
{Loading ? ( //Setting a spinner while waiting for the API call to return the results. Read as a IF statement. So if Loading is true, then do this else render template
<View styles={styles.background}>
<ActivityIndicator
//visibility of Overlay Loading Spinner
visible={Loading}
//Text with the Spinner
textContent={'Loading...'}
size='large'
color= {colors.grey}
//Text style of the Spinner Text
//textStyle={styles.spinnerTextStyle}
/>
<Text></Text>
<Text>🔎 Looking for a {Object.keys(assignment)[0]} in your picture... </Text>
</View>
) : ( //this bit we render if the app is not loading
<>
<Image style={styles.preview} source={{ uri: "data:image/jpg;base64," + photo.base64 }} />
<View style={styles.tableview}>
<Text style={styles.ResultsHeading}>Results</Text>
<Text> Your assignment was to photograph a {Object.keys(assignment)[0]}</Text>
{WasAssignmentFound(data)}
</View>
</>
)}
</SafeAreaView>
);
}
// This is the main camera view
return (
<Camera style={styles.container} ref={cameraRef}>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
setModalVisible(!modalVisible);
}}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<TouchableOpacity name="close" onPress={() => setModalVisible(!modalVisible)} style={{
position: 'absolute',
right: -10,
top: -10,
width: 75,
height: 75
}}>
<FontAwesome name="close" size={24} color="black"style={{
position: 'absolute',
right: 25,
top: 20
}}/>
</TouchableOpacity>
<ScrollView showsVerticalScrollIndicator={false} showsHorizontalScrollIndicator={false}>
<View style={{alignItems: 'center'}}>
<Image source={require('./app/assets/AppIcon.png')} style={styles.logo} />
</View>
<Text style={styles.ProfileHeading}>Photo Scavenger</Text>
<Text style={styles.modalText}>Find, Photograph, Score!</Text>
{RenderGrid()}
<Text style={{alignItems: 'center', padding: 20}}>
<Text style={styles.modalTextMuted}>This app is made with by Peter van Doorn. The app is very friendly towards your privacy. There are no ads, there is no tracking and all your photos are deleted directly after processing. The app is open source too, so if you're interested in seeing how the app was built then check out my</Text>
<Text onPress={() => Linking.openURL('https://github.com/two-trick-pony-NL/PhotoScavenger')}> GitHub</Text>
<Text style={styles.modalTextMuted}> page.</Text>
</Text>
</ScrollView>
</View>
</View>
</Modal>
<Text style={styles.CallToAction}> Find a {Object.keys(assignment)[0]} </Text>
<Text style={styles.EmojiAssignment}> {emoji} </Text>
<Text style={styles.HighScore}> {score -(numberrefresh*50)} </Text>
<View style={styles.ProgressScore}>
<ProgressCircle
style= {styles.ProgressBar}
percent={PercentageObjectsSeen}
radius={30}
borderWidth={4}
color={colors.primary}
shadowColor={colors.grey}
bgColor={colors.white}
>
<Text style={{ fontSize: 15, color:colors.black, fontWeight: "bold"}}>{PercentageObjectsSeen}%</Text>
<Text style={{ fontSize: 8, color:colors.black, fontWeight: "bold"}}>found</Text>
</ProgressCircle>
</View>
<View style={styles.NavigationBar}>
<TouchableOpacity
onPress={CallAssignmentAPI}
style={styles.NavigationButton}>
<FontAwesome name="refresh" size={24} color="black" />
</TouchableOpacity>
<TouchableOpacity
onPress={takePic}
style={styles.CameraButton}>
<FontAwesome name="camera" size={44} color="black" />
</TouchableOpacity>
<TouchableOpacity
onPress={() => setModalVisible(!modalVisible)}
style={styles.NavigationButton}>
<FontAwesome name="bars" size={32} color="black" />
</TouchableOpacity>
</View>
<StatusBar style="auto" />
</Camera>
);
}