-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathImageSegmentationScreen.tsx
More file actions
188 lines (175 loc) · 4.33 KB
/
ImageSegmentationScreen.tsx
File metadata and controls
188 lines (175 loc) · 4.33 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
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
import Spinner from 'react-native-loading-spinner-overlay';
import { BottomBar } from '../components/BottomBar';
import { getImage } from '../utils';
import {
useImageSegmentation,
DEEPLAB_V3_RESNET50,
DeeplabLabel,
} from 'react-native-executorch';
import {
Canvas,
Image as SkiaImage,
Skia,
AlphaType,
ColorType,
} from '@shopify/react-native-skia';
import { View, StyleSheet, Image } from 'react-native';
import { useState } from 'react';
const width = 224;
const height = 224;
let pixels = new Uint8Array(width * height * 4);
pixels.fill(255);
let data = Skia.Data.fromBytes(pixels);
let img = Skia.Image.MakeImage(
{
width: width,
height: height,
alphaType: AlphaType.Opaque,
colorType: ColorType.RGBA_8888,
},
data,
width * 4
);
const numberToColor: number[][] = [
[255, 87, 51], // 0 Red
[51, 255, 87], // 1 Green
[51, 87, 255], // 2 Blue
[255, 51, 246], // 3 Magenta
[51, 255, 246], // 4 Cyan
[243, 255, 51], // 5 Yellow
[141, 51, 255], // 6 Purple
[255, 131, 51], // 7 Orange
[51, 255, 131], // 8 Spring Green
[131, 51, 255], // 9 Violet
[255, 255, 51], // 10 Bright Yellow
[51, 255, 255], // 11 Aqua
[255, 51, 143], // 12 Deep Pink
[127, 51, 255], // 13 Dark Orchid
[51, 255, 175], // 14 Medium Spring Green
[255, 175, 51], // 15 Sandy Brown
[179, 255, 51], // 16 Chartreuse
[255, 87, 51], // 17 Red (darker shade)
[255, 51, 162], // 18 Hot Pink
[51, 162, 255], // 19 Sky Blue
[162, 51, 255], // 20 Amethyst
];
export const ImageSegmentationScreen = ({
imageUri,
setImageUri,
}: {
imageUri: string;
setImageUri: (imageUri: string) => void;
}) => {
const model = useImageSegmentation({
modelSource: DEEPLAB_V3_RESNET50,
});
const handleCameraPress = async (isCamera: boolean) => {
const image = await getImage(isCamera);
const uri = image?.uri;
setImageUri(uri as string);
};
const [resultPresent, setResultPresent] = useState(false);
const runForward = async () => {
if (imageUri) {
try {
const output = await model.forward(imageUri);
pixels = new Uint8Array(width * height * 4);
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
for (let i = 0; i < 3; i++) {
pixels[(x * height + y) * 4 + i] =
numberToColor[
(output[DeeplabLabel.ARGMAX] || [])[x * height + y]
][i];
}
pixels[(x * height + y) * 4 + 3] = 255;
}
}
data = Skia.Data.fromBytes(pixels);
img = Skia.Image.MakeImage(
{
width: width,
height: height,
alphaType: AlphaType.Opaque,
colorType: ColorType.RGBA_8888,
},
data,
width * 4
);
setResultPresent(true);
} catch (e) {
console.error(e);
}
}
};
if (!model.isReady) {
return (
<Spinner
visible={!model.isReady}
textContent={`Loading the model ${(model.downloadProgress * 100).toFixed(0)} %`}
/>
);
}
return (
<>
<View style={styles.imageCanvasContainer}>
<View style={styles.imageContainer}>
<Image
style={styles.image}
resizeMode="contain"
source={
imageUri
? { uri: imageUri }
: require('../assets/icons/executorch_logo.png')
}
/>
</View>
{resultPresent && (
<View style={styles.canvasContainer}>
<Canvas style={styles.canvas}>
<SkiaImage
image={img}
fit="contain"
x={0}
y={0}
width={width}
height={height}
/>
</Canvas>
</View>
)}
</View>
<BottomBar
handleCameraPress={handleCameraPress}
runForward={runForward}
/>
</>
);
};
const styles = StyleSheet.create({
imageCanvasContainer: {
flex: 6,
width: '100%',
padding: 16,
},
imageContainer: {
flex: 1,
width: '100%',
},
image: {
flex: 1,
borderRadius: 8,
width: '100%',
},
canvasContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
gap: 4,
padding: 4,
},
canvas: {
width: width,
height: height,
},
});