Spaces:
Sleeping
Sleeping
Commit
·
e28e2c8
1
Parent(s):
decc742
Update app.py
Browse files
app.py
CHANGED
@@ -2,34 +2,34 @@ import os
|
|
2 |
import copy
|
3 |
import time
|
4 |
|
5 |
-
import cv2 as cv
|
6 |
import numpy as np
|
7 |
import onnxruntime
|
8 |
|
9 |
-
from PIL import Image
|
10 |
|
11 |
import gradio
|
12 |
|
13 |
def run_inference(onnx_session, input_size, image):
|
14 |
-
#
|
15 |
-
temp_image = copy
|
16 |
-
resize_image =
|
17 |
-
x =
|
|
|
18 |
|
19 |
-
#
|
20 |
-
x =
|
21 |
-
mean = [0.485, 0.456, 0.406]
|
22 |
-
std = [0.229, 0.224, 0.225]
|
23 |
x = (x / 255 - mean) / std
|
24 |
x = x.transpose(2, 0, 1).astype('float32')
|
25 |
x = x.reshape(-1, 3, input_size, input_size)
|
26 |
|
27 |
-
#
|
28 |
input_name = onnx_session.get_inputs()[0].name
|
29 |
output_name = onnx_session.get_outputs()[0].name
|
30 |
onnx_result = onnx_session.run([output_name], {input_name: x})
|
31 |
|
32 |
-
#
|
33 |
onnx_result = np.array(onnx_result).squeeze()
|
34 |
min_value = np.min(onnx_result)
|
35 |
max_value = np.max(onnx_result)
|
@@ -43,20 +43,20 @@ def run_inference(onnx_session, input_size, image):
|
|
43 |
onnx_session = onnxruntime.InferenceSession("u2net.onnx")
|
44 |
|
45 |
def create_rgba(mode, image):
|
|
|
46 |
out = run_inference(
|
47 |
onnx_session,
|
48 |
320,
|
49 |
image,
|
50 |
)
|
51 |
-
resize_image =
|
52 |
|
53 |
if mode == "binary":
|
54 |
-
resize_image
|
55 |
-
resize_image[resize_image < 125] = 0
|
56 |
|
57 |
-
mask =
|
58 |
|
59 |
-
rgba_image =
|
60 |
rgba_image.putalpha(mask)
|
61 |
|
62 |
return rgba_image
|
|
|
2 |
import copy
|
3 |
import time
|
4 |
|
|
|
5 |
import numpy as np
|
6 |
import onnxruntime
|
7 |
|
8 |
+
from PIL import Image, ImageOps
|
9 |
|
10 |
import gradio
|
11 |
|
12 |
def run_inference(onnx_session, input_size, image):
|
13 |
+
# Resize
|
14 |
+
temp_image = image.copy()
|
15 |
+
resize_image = temp_image.resize((input_size, input_size), Image.ANTIALIAS)
|
16 |
+
x = ImageOps.exif_transpose(resize_image)
|
17 |
+
x = np.array(x)
|
18 |
|
19 |
+
# Preprocessing
|
20 |
+
x = x.astype(np.float32)
|
21 |
+
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
22 |
+
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
23 |
x = (x / 255 - mean) / std
|
24 |
x = x.transpose(2, 0, 1).astype('float32')
|
25 |
x = x.reshape(-1, 3, input_size, input_size)
|
26 |
|
27 |
+
# Inference
|
28 |
input_name = onnx_session.get_inputs()[0].name
|
29 |
output_name = onnx_session.get_outputs()[0].name
|
30 |
onnx_result = onnx_session.run([output_name], {input_name: x})
|
31 |
|
32 |
+
# Postprocessing
|
33 |
onnx_result = np.array(onnx_result).squeeze()
|
34 |
min_value = np.min(onnx_result)
|
35 |
max_value = np.max(onnx_result)
|
|
|
43 |
onnx_session = onnxruntime.InferenceSession("u2net.onnx")
|
44 |
|
45 |
def create_rgba(mode, image):
|
46 |
+
image = Image.fromarray(image).convert('RGB')
|
47 |
out = run_inference(
|
48 |
onnx_session,
|
49 |
320,
|
50 |
image,
|
51 |
)
|
52 |
+
resize_image = Image.fromarray(out).resize((image.size[0], image.size[1]), Image.ANTIALIAS)
|
53 |
|
54 |
if mode == "binary":
|
55 |
+
resize_image = resize_image.point(lambda x: 255 if x > 125 else 0)
|
|
|
56 |
|
57 |
+
mask = resize_image
|
58 |
|
59 |
+
rgba_image = image.convert('RGBA')
|
60 |
rgba_image.putalpha(mask)
|
61 |
|
62 |
return rgba_image
|