-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
89 lines (75 loc) · 2.63 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
import { useState } from 'react';
import { StatusBar } from 'expo-status-bar';
import { Image, Text, View, ActivityIndicator } from 'react-native';
import * as ImagePicker from 'expo-image-picker';
import * as FileSystem from 'expo-file-system';
import * as tensorflow from '@tensorflow/tfjs';
import * as mobilenet from '@tensorflow-models/mobilenet';
import { decodeJpeg } from '@tensorflow/tfjs-react-native';
import { styles } from './styles';
import { Button } from './components/Button';
import { Classification, ClassificationProps } from './components/Classification';
export default function App() {
const [isLoading, setIsLoading] = useState(false);
const [selectedImageUri, setSelectedImageUri] = useState('');
const [results, setResults] = useState<ClassificationProps[]>([]);
async function handleSelectImage() {
setIsLoading(true);
try {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true
});
if(!result.canceled){
const { uri } = result.assets[0];
setSelectedImageUri(uri);
await imageClassification(uri);
}
} catch (error) {
console.log(error);
} finally {
setIsLoading(false);
}
}
async function imageClassification(imageUri: string) {
setResults([]);
await tensorflow.ready();
const model = await mobilenet.load();
const imageBase64 = await FileSystem.readAsStringAsync(imageUri, {
encoding: FileSystem.EncodingType.Base64
});
const imgBuffer = tensorflow.util.encodeString(imageBase64, 'base64').buffer;
const raw = new Uint8Array(imgBuffer);
const imageTensor = decodeJpeg(raw);
const classificationResult = await model.classify(imageTensor);
setResults( classificationResult);
}
return (
<View style={styles.container}>
<StatusBar
style="light"
backgroundColor="transparent"
translucent
/>
<View style={styles.header}>
<Text style={styles.title}>Tensorflow</Text>
</View>
<Image
source={{ uri: selectedImageUri ? selectedImageUri: 'https://teddytennis.com/usa/wp-content/uploads/sites/88/2017/11/placeholder.png' }}
style={styles.image}
/>
<View style={styles.results}>
{
results.map((result) => (
<Classification key={result.className} data={result} />
))
}
</View>
{
isLoading
? <ActivityIndicator color="#f97316" style={{ marginBottom: 18 }} />
: <Button title="Selecionar imagem" onPress={handleSelectImage} />
}
</View>
);
}