-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathindex.tsx
More file actions
86 lines (79 loc) · 2.16 KB
/
index.tsx
File metadata and controls
86 lines (79 loc) · 2.16 KB
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
import Spinner from '../../components/Spinner';
import { BottomBar } from '../../components/BottomBar';
import { getImage } from '../../utils';
import {
useStyleTransfer,
STYLE_TRANSFER_CANDY_QUANTIZED,
} from 'react-native-executorch';
import { View, StyleSheet, Image } from 'react-native';
import React, { useContext, useEffect, useState } from 'react';
import { GeneratingContext } from '../../context';
import ScreenWrapper from '../../ScreenWrapper';
export default function StyleTransferScreen() {
const model = useStyleTransfer({ model: STYLE_TRANSFER_CANDY_QUANTIZED });
const { setGlobalGenerating } = useContext(GeneratingContext);
useEffect(() => {
setGlobalGenerating(model.isGenerating);
}, [model.isGenerating, setGlobalGenerating]);
const [imageUri, setImageUri] = useState('');
const [styledUri, setStyledUri] = useState('');
const handleCameraPress = async (isCamera: boolean) => {
const image = await getImage(isCamera);
const uri = image?.uri;
if (typeof uri === 'string') {
setImageUri(uri);
setStyledUri('');
}
};
const runForward = async () => {
if (imageUri) {
try {
const uri = await model.forward(imageUri, 'url');
setStyledUri(uri);
} catch (e) {
console.error(e);
}
}
};
if (!model.isReady) {
return (
<Spinner
visible={!model.isReady}
textContent={`Loading the model ${(model.downloadProgress * 100).toFixed(0)} %`}
/>
);
}
return (
<ScreenWrapper>
<View style={styles.imageContainer}>
<Image
style={styles.image}
resizeMode="contain"
source={
styledUri
? { uri: styledUri }
: imageUri
? { uri: imageUri }
: require('../../assets/icons/executorch_logo.png')
}
/>
</View>
<BottomBar
handleCameraPress={handleCameraPress}
runForward={runForward}
/>
</ScreenWrapper>
);
}
const styles = StyleSheet.create({
imageContainer: {
flex: 6,
width: '100%',
padding: 16,
},
image: {
flex: 1,
borderRadius: 8,
width: '100%',
},
});