Rembg is a tool to remove images background
fix model download
Daniel Gatis39257c450256
diff --git a/requirements.txt b/requirements.txtindex 1b632bf..266f869 100644--- a/requirements.txt+++ b/requirements.txt@@ -6,3 +6,4 @@ torch==1.6.0torchvision==0.7.0waitress==1.4.4tqdm==4.48.2+requests==2.24.0diff --git a/setup.py b/setup.pyindex 843a42b..90fe6ae 100644--- a/setup.py+++ b/setup.py@@ -11,7 +11,7 @@ with open("requirements.txt") as f:setup(name="rembg",- version="1.0.6",+ version="1.0.7",description="Remove image background",long_description=long_description,long_description_content_type="text/markdown",diff --git a/src/rembg/cmd/cli.py b/src/rembg/cmd/cli.pyindex fd3a761..2018f50 100644--- a/src/rembg/cmd/cli.py+++ b/src/rembg/cmd/cli.py@@ -3,7 +3,6 @@ import globimport imghdrimport os-from ..bg import removediff --git a/src/rembg/cmd/server.py b/src/rembg/cmd/server.pyindex 8bdf15d..664892b 100644--- a/src/rembg/cmd/server.py+++ b/src/rembg/cmd/server.py@@ -8,7 +8,6 @@ from waitress import servefrom ..bg import remove-app = Flask(__name__)diff --git a/src/rembg/u2net/detect.py b/src/rembg/u2net/detect.pyindex 9934c6d..ffc247e 100644--- a/src/rembg/u2net/detect.py+++ b/src/rembg/u2net/detect.py@@ -1,10 +1,10 @@import errnoimport os-import urllib.requestimport sys+import urllib.requestimport numpy as np-import pkg_resources+import requestsimport torchimport torch.nn as nnimport torch.nn.functional as F@@ -17,43 +17,35 @@ from tqdm import tqdmfrom . import data_loader, u2net-class DownloadProgressBar(tqdm):- def update_to(self, b=1, bsize=1, tsize=None):- if tsize is not None:- self.total = tsize- self.update(b * bsize - self.n)---def download_url(url, model_name, output_path):- if os.path.exists(output_path):+def download(url, fname, path):+ if os.path.exists(path):return- os.makedirs(os.path.expanduser("~/.u2net"), exist_ok=True)-- print(- f"Downloading model to {output_path}".format(output_path=output_path),- file=sys.stderr,- )-- with DownloadProgressBar(- unit="B", unit_scale=True, miniters=1, desc=url.split("/")[-1]- ) as t:- urllib.request.urlretrieve(url, filename=output_path, reporthook=t.update_to)+ resp = requests.get(url, stream=True)+ total = int(resp.headers.get("content-length", 0))+ with open(path, "wb") as file, tqdm(+ desc=fname, total=total, unit="iB", unit_scale=True, unit_divisor=1024,+ ) as bar:+ for data in resp.iter_content(chunk_size=1024):+ size = file.write(data)+ bar.update(size)def load_model(model_name: str = "u2net"):+ os.makedirs(os.path.expanduser(os.path.join("~", ".u2net")), exist_ok=True)+if model_name == "u2netp":net = u2net.U2NETP(3, 1)- path = os.path.expanduser("~/.u2net/u2netp.pth")- download_url(+ path = os.path.expanduser(os.path.join("~", ".u2net", model_name))+ download("https://www.dropbox.com/s/usb1fyiuh8as5gi/u2netp.pth?dl=1","u2netp.pth",path,)elif model_name == "u2net":net = u2net.U2NET(3, 1)- path = os.path.expanduser("~/.u2net/u2net.pth")- download_url(+ path = os.path.expanduser(os.path.join("~", ".u2net", model_name))+ download("https://www.dropbox.com/s/kdu5mhose1clds0/u2net.pth?dl=1","u2net.pth",path,