diff --git a/python-package/README.md b/python-package/README.md index cc85387..10777df 100644 --- a/python-package/README.md +++ b/python-package/README.md @@ -5,7 +5,8 @@ For insightface pip-package <= 0.1.5, we use MXNet as inference backend, please Starting from insightface>=0.2, we use onnxruntime as inference backend. -Please first download our **antelope** model release by command: +For insightface>=0.3.3, model package will be downloaded automatically. +For insightface==0.3.2, you must first download our **antelope** model release by command: ``` insightface-cli model.download antelope diff --git a/python-package/insightface/__init__.py b/python-package/insightface/__init__.py index 335cfb1..8af9c90 100644 --- a/python-package/insightface/__init__.py +++ b/python-package/insightface/__init__.py @@ -11,7 +11,7 @@ except ImportError: "Unable to import dependency onnxruntime. " ) -__version__ = '0.3.2' +__version__ = '0.3.3' from . import model_zoo from . import utils diff --git a/python-package/insightface/app/face_analysis.py b/python-package/insightface/app/face_analysis.py index f176958..04d9e07 100644 --- a/python-package/insightface/app/face_analysis.py +++ b/python-package/insightface/app/face_analysis.py @@ -14,7 +14,7 @@ import os.path as osp from numpy.linalg import norm from ..model_zoo import model_zoo from ..utils import face_align -from ..utils import get_model_dir +from ..utils import ensure_available from .common import Face from ..utils import DEFAULT_MP_NAME @@ -25,7 +25,8 @@ class FaceAnalysis: self.models = {} #root = os.path.expanduser(root) #self.model_dir = osp.join(root, name) - self.model_dir = get_model_dir(name, root) + #self.model_dir = get_model_dir(name, root) + self.model_dir = ensure_available('models', name) onnx_files = glob.glob(osp.join(self.model_dir, '*.onnx')) onnx_files = sorted(onnx_files) for onnx_file in onnx_files: diff --git a/python-package/insightface/commands/model_download.py b/python-package/insightface/commands/model_download.py index 6b0de8a..c5a14a3 100644 --- a/python-package/insightface/commands/model_download.py +++ b/python-package/insightface/commands/model_download.py @@ -5,7 +5,7 @@ import os import os.path as osp import zipfile import glob -from ..utils import download, check_sha1 +from ..utils import download def model_download_command_factory(args): @@ -27,27 +27,10 @@ class ModelDownloadCommand(BaseInsightFaceCLICommand): download_parser.set_defaults(func=model_download_command_factory) def __init__(self, model: str, root: str, force: bool): - self.base_repo_url = 'http://storage.insightface.ai/files/' self._model = model - self._root = os.path.expanduser(root) + self._root = root self._force = force def run(self): - if not os.path.exists(self._root): - os.makedirs(self._root) - dir_path = os.path.join(self._root, 'models', self._model) - if osp.exists(dir_path) and not self._force: - return - if not os.path.exists(dir_path): - os.makedirs(dir_path) - - print('dir_path:', dir_path) - zip_file_path = os.path.join(self._root, 'models', self._model + '.zip') - model_url = "%s/models/%s.zip"%(self.base_repo_url, self._model) - download(model_url, - path=zip_file_path, - overwrite=True) - with zipfile.ZipFile(zip_file_path) as zf: - zf.extractall(dir_path) - os.remove(zip_file_path) + download('models', self._model, force=self._force, root=self._root) diff --git a/python-package/insightface/utils/__init__.py b/python-package/insightface/utils/__init__.py index f14ff94..be02bc9 100644 --- a/python-package/insightface/utils/__init__.py +++ b/python-package/insightface/utils/__init__.py @@ -6,7 +6,7 @@ from __future__ import absolute_import #from . import metrics #from . import parallel -from .download import download, check_sha1 +from .storage import download, ensure_available from .filesystem import get_model_dir from .filesystem import makedirs, try_import_dali from .constant import * diff --git a/python-package/insightface/utils/download.py b/python-package/insightface/utils/download.py index d7b4051..5cda84d 100644 --- a/python-package/insightface/utils/download.py +++ b/python-package/insightface/utils/download.py @@ -33,7 +33,7 @@ def check_sha1(filename, sha1_hash): return sha1.hexdigest()[0:l] == sha1_hash[0:l] -def download(url, path=None, overwrite=False, sha1_hash=None): +def download_file(url, path=None, overwrite=False, sha1_hash=None): """Download an given URL Parameters ---------- diff --git a/python-package/insightface/utils/storage.py b/python-package/insightface/utils/storage.py new file mode 100644 index 0000000..578d311 --- /dev/null +++ b/python-package/insightface/utils/storage.py @@ -0,0 +1,33 @@ + +import os +import os.path as osp +import zipfile +from .download import download_file + +BASE_REPO_URL='http://storage.insightface.ai/files' + +def download(sub_dir, name, force=False, root='~/.insightface'): + _root = os.path.expanduser(root) + dir_path = os.path.join(_root, sub_dir, name) + if osp.exists(dir_path) and not force: + return dir_path + if not os.path.exists(dir_path): + os.makedirs(dir_path) + print('download_path:', dir_path) + zip_file_path = os.path.join(_root, sub_dir, name + '.zip') + model_url = "%s/%s/%s.zip"%(BASE_REPO_URL, sub_dir, name) + download_file(model_url, + path=zip_file_path, + overwrite=True) + with zipfile.ZipFile(zip_file_path) as zf: + zf.extractall(dir_path) + os.remove(zip_file_path) + return dir_path + +def ensure_available(sub_dir, name, root='~/.insightface'): + _root = os.path.expanduser(root) + dir_path = os.path.join(_root, sub_dir, name) + if osp.exists(dir_path): + return dir_path + return download(sub_dir, name, force=False, root=root) +