forked from Mazhar004/MODNet-BGRemover
-
Notifications
You must be signed in to change notification settings - Fork 9
/
predict.py
34 lines (27 loc) · 1.18 KB
/
predict.py
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
import os
from tempfile import TemporaryDirectory as tmpdir
import magic
from bg_remove import BGRemove
from cog import BasePredictor, Input, Path
class Predictor(BasePredictor):
def setup(self):
print("setup")
self.bgremover = BGRemove("/modnet_webcam_portrait_matting.ckpt")
def predict(self,
image: Path = Input(description="input image"),
) -> Path:
image = str(image)
# get mimetype
mime = magic.from_file(image, mime=True)
# copy file to filename with correct extension for any type of image
# we should change the backend to not give filenames without an extension in the future
desired_extension = mime.split("/")[1]
new_filename = image + "." + desired_extension
os.system(f"cp \"{image}\" \"{new_filename}\"")
image = new_filename
print("running bgremover on image ", str(image))
output_path = tmpdir().name
self.bgremover.image(str(image), background=False, output=output_path)
#os.system("ls -l /outputs")
os.system(f"mv {output_path}/*.png {output_path}/output.png")
return Path(f"{output_path}/output.png")