Spaces:
Sleeping
Sleeping
Update example_inference.py
Browse files- example_inference.py +11 -4
example_inference.py
CHANGED
@@ -4,6 +4,8 @@ from PIL import Image
|
|
4 |
from briarmbg import BriaRMBG
|
5 |
from utilities import preprocess_image, postprocess_image
|
6 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
7 |
|
8 |
def example_inference(im_path, transprent_bg=False):
|
9 |
|
@@ -15,7 +17,7 @@ def example_inference(im_path, transprent_bg=False):
|
|
15 |
|
16 |
# prepare input
|
17 |
model_input_size = [1024,1024]
|
18 |
-
orig_im = io.imread(im_path)
|
19 |
orig_im_size = orig_im.shape[0:2]
|
20 |
image = preprocess_image(orig_im, model_input_size).to(device)
|
21 |
|
@@ -28,7 +30,12 @@ def example_inference(im_path, transprent_bg=False):
|
|
28 |
# save result
|
29 |
pil_im = Image.fromarray(result_image)
|
30 |
no_bg_image = Image.new("RGBA", pil_im.size, bgColor)
|
31 |
-
orig_image = Image.open(im_path)
|
32 |
no_bg_image.paste(orig_image, mask=pil_im)
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from briarmbg import BriaRMBG
|
5 |
from utilities import preprocess_image, postprocess_image
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
+
import io as IO
|
8 |
+
import base64
|
9 |
|
10 |
def example_inference(im_path, transprent_bg=False):
|
11 |
|
|
|
17 |
|
18 |
# prepare input
|
19 |
model_input_size = [1024,1024]
|
20 |
+
orig_im = io.imread(im_path, plugin='imageio')
|
21 |
orig_im_size = orig_im.shape[0:2]
|
22 |
image = preprocess_image(orig_im, model_input_size).to(device)
|
23 |
|
|
|
30 |
# save result
|
31 |
pil_im = Image.fromarray(result_image)
|
32 |
no_bg_image = Image.new("RGBA", pil_im.size, bgColor)
|
33 |
+
orig_image = Image.open(IO.BytesIO(im_path))
|
34 |
no_bg_image.paste(orig_image, mask=pil_im)
|
35 |
+
|
36 |
+
# Convert image to bytes and then to base64
|
37 |
+
buffered = IO.BytesIO()
|
38 |
+
no_bg_image.save(buffered, format="PNG")
|
39 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
40 |
+
|
41 |
+
return img_str
|