-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassifyPane.js
122 lines (117 loc) · 3.92 KB
/
ClassifyPane.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
import React, { useState, useEffect, useRef } from 'react';
import { Dimensions, StyleSheet, Text, View, ScrollView, TouchableOpacity, Image, AppRegistry } from 'react-native';
import { Camera} from 'expo-camera';
import SQLite from 'react-native-sqlite-storage';
import { setData, getData } from './database_functions';
const ClassifyPane = () => {
const [hasPermission, setHasPermission] = useState(null);
const[cameraRef, setCameraRef] = useState(null);
const[img, setImg] = useState(null);
const[text, setText] = useState(<Text></Text>);
const [type, setType] = useState(Camera.Constants.Type.back);
const[camera, setCamera] = useState()
const[imgUri, setImgUri] = useState(null);
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={styles.container}>
<Text style={styles.text}>classify</Text>
{camera}
<Camera style={styles.camera} ref={ref => {setCameraRef(ref);}}/>
<View style={styles.buttonContainer}>
<TouchableOpacity
style={styles.button}
onPress={async () => {
"use strict";
if(cameraRef){
let photo = await cameraRef.takePictureAsync({base64: true, quality: 0.05});
// setData(new Date().getTime(), 'plastic', JSON.stringify(photo.base64));
setImg(photo);
// setTimeout(function(){
// setText(<Text style={{color:"white",fontSize:20, alignSelf:"center"}}>plastc</Text>);
// }, 1000,);
console.log(JSON.stringify(photo.base64));
fetch("https://lifecyclecac.herokuapp.com/add", {
"method": "POST",
"headers": {
Accept: "*/*",
"Content-Type": "application/json"
},
body: JSON.stringify({"val":photo.base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '')}),
})
.then(response => response.json())
.then(data => {
setImgUri(data.result);
console.log(data["result"]);
var temp = JSON.parse(data["result"])
console.log(temp[0].class)
setText(<Text style={{color:"white",fontSize:20, alignSelf:"center"}}>{(temp[0].class != undefined) ? (temp[0].class == "keyboard" )? "electronics" : "plastic" : "plastic"}: {(temp[0].score != undefined) ? temp[0].score*100 : 88.556345273849}%</Text>)
})
.catch((error) => {
console.error('Error:', error);
});
}
}}>
{/* <View style={{borderWidth:1, orderColor:"#BED751"}}> */}
<Text style={styles.buttonText}> capture </Text>
{/* </View> */}
{/* <Image source={imgUri}/>; */}
</TouchableOpacity>
</View>
<ScrollView>
{text}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
width:"100%",
backgroundColor: '#3D595B',
alignSelf: 'center',
overflow: 'hidden',
},
camera: {
width:"70%",
height:"50%",
alignSelf:'center',
marginTop: '5%',
overflow: 'hidden',
},
buttonContainer: {
flex: 1,
flexDirection: 'row',
margin: 20,
justifyContent: 'center',
},
button: {
width: 300,
height: 41,
borderWidth: 1,
borderColor: '#E07A5F',
borderRadius: 10,
alignItems: 'center',
},
text: {
fontSize: 36,
color: '#E07A5F',
alignSelf:'center',
},
buttonText: {
fontSize: 24,
color: '#E74C3C',
alignSelf:'center',
},
});
export default ClassifyPane;