本文整理汇总了Python中dlib.cnn_face_detection_model_v1方法的典型用法代码示例。如果您正苦于以下问题:Python dlib.cnn_face_detection_model_v1方法的具体用法?Python dlib.cnn_face_detection_model_v1怎么用?Python dlib.cnn_face_detection_model_v1使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dlib
的用法示例。
在下文中一共展示了dlib.cnn_face_detection_model_v1方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, device, path_to_detector=None, verbose=False):
super().__init__(device, verbose)
base_path = os.path.join(appdata_dir('face_alignment'), "data")
# Initialise the face detector
if 'cuda' in device:
if path_to_detector is None:
path_to_detector = os.path.join(
base_path, "mmod_human_face_detector.dat")
if not os.path.isfile(path_to_detector):
print("Downloading the face detection CNN. Please wait...")
path_to_temp_detector = os.path.join(
base_path, "mmod_human_face_detector.dat.download")
if os.path.isfile(path_to_temp_detector):
os.remove(os.path.join(path_to_temp_detector))
request_file.urlretrieve(
"https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
os.path.join(path_to_temp_detector))
os.rename(os.path.join(path_to_temp_detector), os.path.join(path_to_detector))
self.face_detector = dlib.cnn_face_detection_model_v1(path_to_detector)
else:
self.face_detector = dlib.get_frontal_face_detector()
示例2: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, is_dlib=False):
# resolution of input and output image size.
self.resolution_inp = 256
self.resolution_op = 256
# ---- load detectors
if is_dlib:
import dlib
detector_path = _util.getRelWeightsPath('dlib', 'mmod_human_face_detector.dat')
self.face_detector = dlib.cnn_face_detection_model_v1(detector_path)
# ---- load PRN
self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
prn_path = _util.getRelWeightsPath('prnet', 'net/256_256_resfcn256_weight')
assert os.path.isfile(prn_path + '.data-00000-of-00001'), "please download PRN trained model first."
self.pos_predictor.restore(prn_path)
# uv file: 2 x 68
self.uv_kpt_ind = np.loadtxt(_util.getRelWeightsPath('prnet', 'uv', 'uv_kpt_ind.txt')).astype(np.int32)
# get kpt: get valid vertices in the pos map
self.face_ind = np.loadtxt(_util.getRelWeightsPath('prnet', 'uv', 'face_ind.txt')).astype(np.int32)
# ntri x 3.
self.triangles = np.loadtxt(_util.getRelWeightsPath('prnet', 'uv', 'triangles.txt')).astype(np.int32)
self.uv_coords = self.generate_uv_coords()
# Cache Position map.
self.pos = None
示例3: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, is_dlib = False, prefix = '.'):
# resolution of input and output image size.
self.resolution_inp = 256
self.resolution_op = 256
#---- load detectors
if is_dlib:
import dlib
detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
self.face_detector = dlib.cnn_face_detection_model_v1(
detector_path)
#---- load PRN
self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
if not os.path.isfile(prn_path + '.data-00000-of-00001'):
print(prn_path)
print("please download PRN trained model first.")
exit()
self.pos_predictor.restore(prn_path)
# uv file
self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
self.face_ind = np.loadtxt(prefix + '/Data/uv/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
self.triangles = np.loadtxt(prefix + '/Data/uv/triangles.txt').astype(np.int32) # ntri x 3
self.uv_coords = self.generate_uv_coords()
示例4: _dlib_face_detection
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def _dlib_face_detection(image):
"""
Face detection using the CNN implementation from Dlib.
References:
Davis E. King. Dlib-ml: A Machine Learning Toolkit. Journal of Machine Learning Research 10, pp. 1755-1758, 2009
:param image: (ndarray) Raw image
:return: (ndarray) The coordinates of the detected face
"""
global _FACE_DETECTOR_DLIB
face_coordinates = []
# Verifies if dlib is initialized
if _FACE_DETECTOR_DLIB is None:
_FACE_DETECTOR_DLIB = dlib.cnn_face_detection_model_v1('./model/utils/templates/dlib/cnn_face_detector.dat')
# Calls dlib's face detection method
faces = _FACE_DETECTOR_DLIB(image)
# Gets coordinates
if not (faces is None):
for face_id, net_output in enumerate(faces):
xi, xf, yi, yf = (net_output.rect.left(), net_output.rect.right(), net_output.rect.top(), net_output.rect.bottom())
face_coordinates.append([[xi, yi], [xf, yf]])
return np.array(face_coordinates)
开发者ID:siqueira-hc,项目名称:Efficient-Facial-Feature-Learning-with-Wide-Ensemble-based-Convolutional-Neural-Networks,代码行数:30,代码来源:cvision.py
示例5: _preload_dlib_detector_fitter
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def _preload_dlib_detector_fitter(self):
r"""
Returns the dlib face detector and the landmark fitters (5 and 68 landmarks)
-------
"""
if self._gpu is True:
self._detect = dlib.cnn_face_detection_model_v1(self._detector_path)
else:
self._detect = dlib.get_frontal_face_detector()
self._fitter5 = dlib.shape_predictor(self._predictor5_path)
self._fitter68 = dlib.shape_predictor(self._predictor68_path)
示例6: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, device, path_to_detector=None, verbose=False):
super().__init__(device, verbose)
print('Warning: this detector is deprecated. Please use a different one, i.e.: S3FD.')
base_path = os.path.join(appdata_dir('face_alignment'), "data")
# Initialise the face detector
if 'cuda' in device:
if path_to_detector is None:
path_to_detector = os.path.join(
base_path, "mmod_human_face_detector.dat")
if not os.path.isfile(path_to_detector):
print("Downloading the face detection CNN. Please wait...")
path_to_temp_detector = os.path.join(
base_path, "mmod_human_face_detector.dat.download")
if os.path.isfile(path_to_temp_detector):
os.remove(os.path.join(path_to_temp_detector))
request_file.urlretrieve(
"https://www.adrianbulat.com/downloads/dlib/mmod_human_face_detector.dat",
os.path.join(path_to_temp_detector))
os.rename(os.path.join(path_to_temp_detector), os.path.join(path_to_detector))
self.face_detector = dlib.cnn_face_detection_model_v1(path_to_detector)
else:
self.face_detector = dlib.get_frontal_face_detector()
示例7: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, is_dlib = False, prefix = '.'):
# resolution of input and output image size.
self.resolution_inp = 256
self.resolution_op = 256
#---- load detectors
if is_dlib:
import dlib
detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
self.face_detector = dlib.cnn_face_detection_model_v1(
detector_path)
#---- load PRN
self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
if not os.path.isfile(prn_path + '.data-00000-of-00001'):
print("please download PRN trained model first.")
exit()
self.pos_predictor.restore(prn_path)
# uv file
self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
self.face_ind = np.loadtxt(prefix + '/Data/uv-data/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
self.triangles = np.loadtxt(prefix + '/Data/uv-data/triangles.txt').astype(np.int32) # ntri x 3
self.uv_coords = self.generate_uv_coords()
示例8: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, is_dlib = False):
# resolution of input and output image size.
self.resolution_inp = 256
self.resolution_op = 256
prefix = os.path.dirname(__file__)
#---- load detectors
if is_dlib:
import dlib
detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
self.face_detector = dlib.cnn_face_detection_model_v1(
detector_path)
#---- load PRN
self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
if not os.path.isfile(prn_path + '.data-00000-of-00001'):
print("please download PRN trained model first.")
exit()
self.pos_predictor.restore(prn_path)
# uv file
self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
self.face_ind = np.loadtxt(prefix + '/Data/uv-data/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
self.triangles = np.loadtxt(prefix + '/Data/uv-data/triangles.txt').astype(np.int32) # ntri x 3
self.canonical_vertices_fan = (np.load(prefix + '/Data/uv-data/canonical_vertices_68_fan.npy'))
self.canonical_vertices_40 = np.load(prefix + '/Data/uv-data/canonical_vertices_40k.npy') # pos angle with 40k point, add by sai
self.uv_coords = self.generate_uv_coords()
示例9: __init__
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def __init__(self, is_dlib = False, is_opencv = False, prefix = '.'):
# resolution of input and output image size.
self.resolution_inp = 256
self.resolution_op = 256
#---- load detectors
if is_dlib:
import dlib
detector_path = os.path.join(prefix, 'Data/net-data/mmod_human_face_detector.dat')
self.face_detector = dlib.cnn_face_detection_model_v1(
detector_path)
if is_opencv:
import cv2
#---- load PRN
self.pos_predictor = PosPrediction(self.resolution_inp, self.resolution_op)
prn_path = os.path.join(prefix, 'Data/net-data/256_256_resfcn256_weight')
if not os.path.isfile(prn_path + '.data-00000-of-00001'):
print("please download PRN trained model first.")
exit()
self.pos_predictor.restore(prn_path)
# uv file
self.uv_kpt_ind = np.loadtxt(prefix + '/Data/uv-data/uv_kpt_ind.txt').astype(np.int32) # 2 x 68 get kpt
self.face_ind = np.loadtxt(prefix + '/Data/uv-data/face_ind.txt').astype(np.int32) # get valid vertices in the pos map
self.triangles = np.loadtxt(prefix + '/Data/uv-data/triangles.txt').astype(np.int32) # ntri x 3
示例10: get_face_detector
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def get_face_detector():
"""Get a singleton dlib face detector."""
global _face_detector
if not _face_detector:
try:
dat_path = _get_dlib_data_file('mmod_human_face_detector.dat')
_face_detector = dlib.cnn_face_detection_model_v1(dat_path)
except:
xml_path = _get_opencv_xml('lbpcascade_frontalface_improved.xml')
_face_detector = cv.CascadeClassifier(xml_path)
return _face_detector
示例11: face_detection
# 需要导入模块: import dlib [as 别名]
# 或者: from dlib import cnn_face_detection_model_v1 [as 别名]
def face_detection(
img_path,
verbose=False,
model_file='utils/dlib_face_detector/mmod_human_face_detector.dat'):
"""
Detects faces using dlib cnn face detection, and extend the bounding box
to include the entire face.
"""
def shrink(img, max_length=2048):
ow, oh = img.size
if max_length >= max(ow, oh):
return img, 1.0
if ow > oh:
mult = max_length / ow
else:
mult = max_length / oh
w = int(ow * mult)
h = int(oh * mult)
return img.resize((w, h), Image.BILINEAR), mult
global cnn_face_detector
if cnn_face_detector is None:
cnn_face_detector = face_detect_model(model_file)
img = Image.open(img_path).convert('RGB')
w, h = img.size
img_shrinked, mult = shrink(img)
im = np.asarray(img_shrinked)
if len(im.shape) != 3 or im.shape[2] != 3:
return []
crop_ims = []
dets = cnn_face_detector(im, 0)
for k, d in enumerate(dets):
top = d.rect.top() / mult
bottom = d.rect.bottom() / mult
left = d.rect.left() / mult
right = d.rect.right() / mult
wid = right - left
left = max(0, left - wid // 2.5)
top = max(0, top - wid // 1.5)
right = min(w - 1, right + wid // 2.5)
bottom = min(h - 1, bottom + wid // 2.5)
if d.confidence > 1:
if verbose:
print("%d-th face detected: (%d, %d, %d, %d)" %
(k, left, top, right, bottom))
crop_im = img.crop((left, top, right, bottom))
crop_ims.append((crop_im, (left, top, right, bottom)))
return crop_ims