本文整理汇总了Python中imutils.face_utils.FaceAligner方法的典型用法代码示例。如果您正苦于以下问题:Python face_utils.FaceAligner方法的具体用法?Python face_utils.FaceAligner怎么用?Python face_utils.FaceAligner使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imutils.face_utils
的用法示例。
在下文中一共展示了face_utils.FaceAligner方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_image
# 需要导入模块: from imutils import face_utils [as 别名]
# 或者: from imutils.face_utils import FaceAligner [as 别名]
def load_image(image_path, shape_predictor):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(shape_predictor)
fa = FaceAligner(predictor, desiredFaceWidth=160)
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
# image = imutils.resize(image, width=256)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 2)
rect_nums = len(rects)
XY, aligned_images = [], []
if rect_nums == 0:
aligned_images.append(image)
return aligned_images, image, rect_nums, XY
else:
for i in range(rect_nums):
aligned_image = fa.align(image, gray, rects[i])
aligned_images.append(aligned_image)
(x, y, w, h) = rect_to_bb(rects[i])
image = cv2.rectangle(image, (x, y), (x + w, y + h), color=(255, 0, 0), thickness=2)
XY.append((x, y))
return np.array(aligned_images), image, rect_nums, XY
# def draw_label(image, point, ages, genders, font=cv2.FONT_HERSHEY_COMPLEX, font_scale=1, thickness=1):
# for i in range(len(point)):
# label = "{}, {}".format(int(ages[i]), "F" if genders[i] == 0 else "M")
# size = cv2.getTextSize(label, font, font_scale, thickness)[0]
# x, y = point[i]
# # cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
# cv2.putText(image, label, (x, np.max(y - 5, 0)), font, font_scale, (255, 255, 255), thickness)
示例2: __init__
# 需要导入模块: from imutils import face_utils [as 别名]
# 或者: from imutils.face_utils import FaceAligner [as 别名]
def __init__(self):
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
self.fa = face_utils.FaceAligner(self.predictor, desiredFaceWidth=256)
示例3: main
# 需要导入模块: from imutils import face_utils [as 别名]
# 或者: from imutils.face_utils import FaceAligner [as 别名]
def main(sess,age,gender,train_mode,images_pl):
args = get_args()
depth = args.depth
k = args.width
# for face detection
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
fa = FaceAligner(predictor, desiredFaceWidth=160)
# load model and weights
img_size = 160
# capture video
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
while True:
# get video frame
ret, img = cap.read()
if not ret:
print("error: failed to capture image")
return -1
input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_h, img_w, _ = np.shape(input_img)
# detect faces using dlib detector
detected = detector(input_img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
xw1 = max(int(x1 - 0.4 * w), 0)
yw1 = max(int(y1 - 0.4 * h), 0)
xw2 = min(int(x2 + 0.4 * w), img_w - 1)
yw2 = min(int(y2 + 0.4 * h), img_h - 1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
faces[i, :, :, :] = fa.align(input_img, gray, detected[i])
# faces[i,:,:,:] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
#
if len(detected) > 0:
# predict ages and genders of the detected faces
ages,genders = sess.run([age, gender], feed_dict={images_pl: faces, train_mode: False})
# draw results
for i, d in enumerate(detected):
label = "{}, {}".format(int(ages[i]), "F" if genders[i] == 0 else "M")
draw_label(img, (d.left(), d.top()), label)
cv2.imshow("result", img)
key = cv2.waitKey(1)
if key == 27:
break