Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions python-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ You have to install ``onnxruntime-gpu`` manually to enable GPU inference, or ins
## Change Log

### [0.7.1] - 2022-12-14

#### Changed

- Change model downloading provider to cloudfront.

### [0.7] - 2022-11-28

#### Added

- Add face swapping model and example.

#### Changed

- Set default ORT provider to CUDA and CPU.

### [0.6] - 2022-01-29

#### Added

- Add pose estimation in face-analysis app.

#### Changed

- Change model automated downloading url, to ucloud.


## Quick Example

Expand Down Expand Up @@ -150,4 +150,6 @@ handler.prepare(ctx_id=0)

```

### Enable debugging

To enable the debug logger, export the environment variable DEBUG (set to any value) prior to starting your application.
6 changes: 6 additions & 0 deletions python-package/insightface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# pylint: disable=wrong-import-position
"""InsightFace: A Face Analysis Toolkit."""
from __future__ import absolute_import
from loguru import logger
import os

try:
#import mxnet as mx
Expand All @@ -11,6 +13,10 @@
"Unable to import dependency onnxruntime. "
)

if os.getenv("DEBUG") is None:
logger.remove()
logger.add(sys.stderr, level="INFO")

__version__ = '0.7.3'

from . import model_zoo
Expand Down
13 changes: 7 additions & 6 deletions python-package/insightface/app/face_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Organization : insightface.ai
# @Author : Jia Guo
# @Time : 2021-05-04
# @Function :
# @Function :


from __future__ import division
Expand All @@ -12,6 +12,7 @@

import numpy as np
import onnxruntime
from loguru import logger
from numpy.linalg import norm

from ..model_zoo import model_zoo
Expand All @@ -30,15 +31,15 @@ def __init__(self, name=DEFAULT_MP_NAME, root='~/.insightface', allowed_modules=
for onnx_file in onnx_files:
model = model_zoo.get_model(onnx_file, **kwargs)
if model is None:
print('model not recognized:', onnx_file)
logger.debug('model not recognized:', onnx_file)
elif allowed_modules is not None and model.taskname not in allowed_modules:
print('model ignore:', onnx_file, model.taskname)
logger.debug('model ignore:', onnx_file, model.taskname)
del model
elif model.taskname not in self.models and (allowed_modules is None or model.taskname in allowed_modules):
print('find model:', onnx_file, model.taskname, model.input_shape, model.input_mean, model.input_std)
logger.debug('find model:', onnx_file, model.taskname, model.input_shape, model.input_mean, model.input_std)
self.models[model.taskname] = model
else:
print('duplicated model task type, ignore:', onnx_file, model.taskname)
logger.debug('duplicated model task type, ignore:', onnx_file, model.taskname)
del model
assert 'detection' in self.models
self.det_model = self.models['detection']
Expand All @@ -47,7 +48,7 @@ def __init__(self, name=DEFAULT_MP_NAME, root='~/.insightface', allowed_modules=
def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640)):
self.det_thresh = det_thresh
assert det_size is not None
print('set det-size:', det_size)
logger.debug('set det-size:', det_size)
self.det_size = det_size
for taskname, model in self.models.items():
if taskname=='detection':
Expand Down
5 changes: 3 additions & 2 deletions python-package/insightface/data/pickle_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import os.path as osp
from pathlib import Path
from loguru import logger
import pickle

def get_object(name):
Expand All @@ -16,9 +17,9 @@ def get_object(name):
name = name + ".pkl"

filepath = osp.join(objects_dir, name)

if not osp.exists(filepath):
print(f"[Error] File not found: {filepath}")
logger.error(f"[Error] File not found: {filepath}")
return None

with open(filepath, 'rb') as f:
Expand Down
5 changes: 3 additions & 2 deletions python-package/insightface/data/rec_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path as osp
import sys
import mxnet as mx
from loguru import logger


class RecBuilder():
Expand All @@ -15,7 +16,7 @@ def __init__(self, path, image_size=(112, 112)):
self.max_label = -1
assert not osp.exists(path), '%s exists' % path
os.makedirs(path)
self.writer = mx.recordio.MXIndexedRecordIO(os.path.join(path, 'train.idx'),
self.writer = mx.recordio.MXIndexedRecordIO(os.path.join(path, 'train.idx'),
os.path.join(path, 'train.rec'),
'w')
self.meta = []
Expand Down Expand Up @@ -64,7 +65,7 @@ def add_image(self, img, label):
def close(self):
with open(osp.join(self.path, 'train.meta'), 'wb') as pfile:
pickle.dump(self.meta, pfile, protocol=pickle.HIGHEST_PROTOCOL)
print('stat:', self.widx, self.wlabel)
logger.debug('stat:', self.widx, self.wlabel)
with open(os.path.join(self.path, 'property'), 'w') as f:
f.write("%d,%d,%d\n" % (self.max_label+1, self.image_size[0], self.image_size[1]))
f.write("%d\n" % (self.widx))
Expand Down
3 changes: 2 additions & 1 deletion python-package/insightface/model_zoo/inswapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import onnxruntime
import cv2
import onnx
from loguru import logger
from onnx import numpy_helper
from ..utils import face_align

Expand Down Expand Up @@ -35,7 +36,7 @@ def __init__(self, model_file=None, session=None):
input_cfg = inputs[0]
input_shape = input_cfg.shape
self.input_shape = input_shape
print('inswapper-shape:', self.input_shape)
logger.debug('inswapper-shape:', self.input_shape)
self.input_size = tuple(input_shape[2:4][::-1])

def forward(self, img, latent):
Expand Down
5 changes: 3 additions & 2 deletions python-package/insightface/model_zoo/model_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import zipfile
import glob

from loguru import logger
from ..utils import download, check_sha1

_model_sha1 = {
Expand Down Expand Up @@ -72,11 +73,11 @@ def get_model_file(name, root=os.path.join('~', '.insightface', 'models')):
if check_sha1(file_path, sha1_hash):
return file_path
else:
print(
logger.debug(
'Mismatch in the content of model file detected. Downloading again.'
)
else:
print('Model file is not found. Downloading.')
logger.debug('Model file is not found. Downloading.')

if not os.path.exists(root):
os.makedirs(root)
Expand Down
7 changes: 4 additions & 3 deletions python-package/insightface/model_zoo/model_zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# @Organization : insightface.ai
# @Author : Jia Guo
# @Time : 2021-05-04
# @Function :
# @Function :

import os
import os.path as osp
import glob
import onnxruntime
from loguru import logger
from .arcface_onnx import *
from .retinaface import *
#from .scrfd import *
Expand All @@ -19,7 +20,7 @@
__all__ = ['get_model']


class PickableInferenceSession(onnxruntime.InferenceSession):
class PickableInferenceSession(onnxruntime.InferenceSession):
# This is a wrapper to make the current InferenceSession class pickable.
def __init__(self, model_path, **kwargs):
super().__init__(model_path, **kwargs)
Expand All @@ -38,7 +39,7 @@ def __init__(self, onnx_file):

def get_model(self, **kwargs):
session = PickableInferenceSession(self.onnx_file, **kwargs)
print(f'Applied providers: {session._providers}, with options: {session._provider_options}')
logger.debug(f'Applied providers: {session._providers}, with options: {session._provider_options}')
inputs = session.get_inputs()
input_cfg = inputs[0]
input_shape = input_cfg.shape
Expand Down
7 changes: 4 additions & 3 deletions python-package/insightface/model_zoo/retinaface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Organization : insightface.ai
# @Author : Jia Guo
# @Time : 2021-09-18
# @Function :
# @Function :

from __future__ import division
import datetime
Expand All @@ -13,6 +13,7 @@
import os.path as osp
import cv2
import sys
from loguru import logger

def softmax(z):
assert len(z.shape) == 2
Expand Down Expand Up @@ -139,7 +140,7 @@ def prepare(self, ctx_id, **kwargs):
input_size = kwargs.get('input_size', None)
if input_size is not None:
if self.input_size is not None:
print('warning: det_size is already set in detection model, ignore')
logger.warning('warning: det_size is already set in detection model, ignore')
else:
self.input_size = input_size

Expand Down Expand Up @@ -207,7 +208,7 @@ def forward(self, img, threshold):
def detect(self, img, input_size = None, max_num=0, metric='default'):
assert input_size is not None or self.input_size is not None
input_size = self.input_size if input_size is None else input_size

im_ratio = float(img.shape[0]) / img.shape[1]
model_ratio = float(input_size[1]) / input_size[0]
if im_ratio>model_ratio:
Expand Down
15 changes: 8 additions & 7 deletions python-package/insightface/model_zoo/scrfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Organization : insightface.ai
# @Author : Jia Guo
# @Time : 2021-05-04
# @Function :
# @Function :

from __future__ import division
import datetime
Expand All @@ -13,6 +13,7 @@
import os.path as osp
import cv2
import sys
from loguru import logger

def softmax(z):
assert len(z.shape) == 2
Expand Down Expand Up @@ -142,7 +143,7 @@ def prepare(self, ctx_id, **kwargs):
input_size = kwargs.get('input_size', None)
if input_size is not None:
if self.input_size is not None:
print('warning: det_size is already set in scrfd model, ignore')
logger.warning('warning: det_size is already set in scrfd model, ignore')
else:
self.input_size = input_size

Expand Down Expand Up @@ -220,7 +221,7 @@ def forward(self, img, threshold):
def detect(self, img, input_size = None, max_num=0, metric='default'):
assert input_size is not None or self.input_size is not None
input_size = self.input_size if input_size is None else input_size

im_ratio = float(img.shape[0]) / img.shape[1]
model_ratio = float(input_size[1]) / input_size[0]
if im_ratio>model_ratio:
Expand Down Expand Up @@ -329,10 +330,10 @@ def scrfd_2p5gkps(**kwargs):
#bboxes, kpss = detector.detect(img, 0.5, input_size = (640, 640))
bboxes, kpss = detector.detect(img, 0.5)
tb = datetime.datetime.now()
print('all cost:', (tb-ta).total_seconds()*1000)
print(img_path, bboxes.shape)
logger.debug('all cost:', (tb-ta).total_seconds()*1000)
logger.debug(img_path, bboxes.shape)
if kpss is not None:
print(kpss.shape)
logger.debug(kpss.shape)
for i in range(bboxes.shape[0]):
bbox = bboxes[i]
x1,y1,x2,y2,score = bbox.astype(np.int)
Expand All @@ -343,6 +344,6 @@ def scrfd_2p5gkps(**kwargs):
kp = kp.astype(np.int)
cv2.circle(img, tuple(kp) , 1, (0,0,255) , 2)
filename = img_path.split('/')[-1]
print('output:', filename)
logger.debug('output:', filename)
cv2.imwrite('./outputs/%s'%filename, img)

3 changes: 2 additions & 1 deletion python-package/insightface/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import hashlib
import requests
from loguru import logger
from tqdm import tqdm


Expand Down Expand Up @@ -67,7 +68,7 @@ def download_file(url, path=None, overwrite=False, sha1_hash=None):
if not os.path.exists(dirname):
os.makedirs(dirname)

print('Downloading %s from %s...' % (fname, url))
logger.debug('Downloading %s from %s...' % (fname, url))
r = requests.get(url, stream=True)
if r.status_code != 200:
raise RuntimeError("Failed downloading url %s" % url)
Expand Down
5 changes: 3 additions & 2 deletions python-package/insightface/utils/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import os.path as osp
import zipfile
from loguru import logger
from .download import download_file

BASE_REPO_URL = 'https://github.com/deepinsight/insightface/releases/download/v0.7'
Expand All @@ -11,7 +12,7 @@ def download(sub_dir, name, force=False, root='~/.insightface'):
dir_path = os.path.join(_root, sub_dir, name)
if osp.exists(dir_path) and not force:
return dir_path
print('download_path:', dir_path)
logger.debug('download_path:', dir_path)
zip_file_path = os.path.join(_root, sub_dir, name + '.zip')
model_url = "%s/%s.zip"%(BASE_REPO_URL, name)
download_file(model_url,
Expand All @@ -35,7 +36,7 @@ def download_onnx(sub_dir, model_file, force=False, root='~/.insightface', downl
return new_model_file
if not osp.exists(model_root):
os.makedirs(model_root)
print('download_path:', new_model_file)
logger.debug('download_path:', new_model_file)
if not download_zip:
model_url = "%s/%s"%(BASE_REPO_URL, model_file)
download_file(model_url,
Expand Down
5 changes: 3 additions & 2 deletions python-package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ def find_version(*file_paths):
'cython',
'albumentations',
'prettytable',
'loguru',
]

extensions = [
Extension("insightface.thirdparty.face3d.mesh.cython.mesh_core_cython",
Extension("insightface.thirdparty.face3d.mesh.cython.mesh_core_cython",
["insightface/thirdparty/face3d/mesh/cython/mesh_core_cython.pyx", "insightface/thirdparty/face3d/mesh/cython/mesh_core.cpp"], language='c++'),
]
data_images = list(glob.glob('insightface/data/images/*.jpg'))
Expand Down Expand Up @@ -91,7 +92,7 @@ def find_version(*file_paths):
logging.warning(" Install Homebrew: https://brew.sh/")
logging.warning(" Then, run: brew install llvm libomp")
logging.info("Proceeding without setting the compiler.")

else:
# Check if LLVM is installed
llvm_check = subprocess.run(["brew", "--prefix", "llvm"], capture_output=True, text=True)
Expand Down