Spaces:
Sleeping
Sleeping
First commit
Browse files- app.py +67 -0
- model.onnx +3 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import onnxruntime
|
5 |
+
|
6 |
+
def preprocess_image(directory):
|
7 |
+
inputs = Image.open(directory)
|
8 |
+
new_img = np.array(inputs)
|
9 |
+
new_img = np.expand_dims(new_img.transpose((2, 0, 1)), 0)
|
10 |
+
new_img = ((new_img - 127.5) / 127.5).astype(np.float32)
|
11 |
+
return new_img
|
12 |
+
|
13 |
+
def color_map(N=256):
|
14 |
+
def bitget(byteval, idx):
|
15 |
+
return ((byteval & (1 << idx)) != 0)
|
16 |
+
|
17 |
+
cmap = np.zeros((N, 3), dtype=np.uint8)
|
18 |
+
for i in range(N):
|
19 |
+
r = g = b = 0
|
20 |
+
c = i
|
21 |
+
for j in range(8):
|
22 |
+
r = r | (bitget(c, 0) << 7-j)
|
23 |
+
g = g | (bitget(c, 1) << 7-j)
|
24 |
+
b = b | (bitget(c, 2) << 7-j)
|
25 |
+
c = c >> 3
|
26 |
+
|
27 |
+
cmap[i] = np.array([r, g, b])
|
28 |
+
return cmap
|
29 |
+
|
30 |
+
def decode_segmap(temp):
|
31 |
+
cmap = color_map()
|
32 |
+
r = temp.copy()
|
33 |
+
g = temp.copy()
|
34 |
+
b = temp.copy()
|
35 |
+
for l in range(0, 21):
|
36 |
+
r[temp == l] = cmap[l][0]
|
37 |
+
g[temp == l] = cmap[l][1]
|
38 |
+
b[temp == l] = cmap[l][2]
|
39 |
+
|
40 |
+
rgb = np.zeros((temp.shape[0], temp.shape[1], 3)).astype(np.uint8)
|
41 |
+
rgb[:, :, 0] = r
|
42 |
+
rgb[:, :, 1] = g
|
43 |
+
rgb[:, :, 2] = b
|
44 |
+
return rgb
|
45 |
+
|
46 |
+
def inference(image_dir):
|
47 |
+
ort_session = onnxruntime.InferenceSession("model.onnx", providers=["CPUExecutionProvider"])
|
48 |
+
image = preprocess_image(image_dir)
|
49 |
+
outputs = ort_session.run(
|
50 |
+
None,
|
51 |
+
{"pixel_values": image})
|
52 |
+
outputs = outputs[0].squeeze().argmax(axis=0)
|
53 |
+
cmap = decode_segmap(outputs)
|
54 |
+
return cmap
|
55 |
+
|
56 |
+
inputs_images = gr.components.Image(type="filepath", label="Input Image")
|
57 |
+
outputs_images = gr.components.Image(type="numpy", label="Output Image")
|
58 |
+
|
59 |
+
app = gr.Interface(
|
60 |
+
fn=inference,
|
61 |
+
inputs=inputs_images,
|
62 |
+
outputs=outputs_images,
|
63 |
+
title="Semantic segmentation",
|
64 |
+
cache_examples=False,)
|
65 |
+
|
66 |
+
app.launch()
|
67 |
+
|
model.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:97e7c41a88d3f89da8307d17fc0eda4815b7418e6459ac214a1600a4748794df
|
3 |
+
size 110048518
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
Pillow
|
4 |
+
onnxruntime
|