本文整理汇总了Python中detector.Detector.detect_faces方法的典型用法代码示例。如果您正苦于以下问题:Python Detector.detect_faces方法的具体用法?Python Detector.detect_faces怎么用?Python Detector.detect_faces使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类detector.Detector
的用法示例。
在下文中一共展示了Detector.detect_faces方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Cropper
# 需要导入模块: from detector import Detector [as 别名]
# 或者: from detector.Detector import detect_faces [as 别名]
class Cropper(object):
"""Cropper"""
def __init__(self):
super(Cropper, self).__init__()
self.detector = Detector()
@staticmethod
def _bounding_rect(faces):
top, left = sys.maxint, sys.maxint
bottom, right = -sys.maxint, -sys.maxint
for (x, y, w, h) in faces:
if x < left:
left = x
if x+w > right:
right = x+w
if y < top:
top = y
if y+h > bottom:
bottom = y+h
return top, left, bottom, right
def crop(self, img, target_width, target_height):
original_height, original_width = img.shape[:2]
faces = self.detector.detect_faces(img)
if len(faces) == 0: # no detected faces
target_center_x = original_width / 2
target_center_y = original_height / 2
else:
top, left, bottom, right = self._bounding_rect(faces)
target_center_x = (left + right) / 2
target_center_y = (top + bottom) / 2
target_left = target_center_x - target_width / 2
target_right = target_left + target_width
target_top = target_center_y - target_height / 2
target_bottom = target_top + target_height
if target_top < 0:
delta = abs(target_top)
target_top += delta
target_bottom += delta
if target_bottom > original_height:
target_bottom = original_height
if target_left < 0:
delta = abs(target_left)
target_left += delta
target_right += delta
if target_right > original_width:
target_right = original_width
return img[target_top:target_bottom, target_left:target_right]