python - 如何在 dlib python 中保存/裁剪检测到的人脸

标签 python opencv face-detection face-recognition dlib

我想通过裁剪矩形将检测到的人脸保存在 dlib 中 任何人都知道我该如何裁剪它。我第一次使用 dlib 并且 有这么多问题。我也想运行 fisherface 算法 检测到的面孔,但是当我将检测到的矩形传递给 predictor 时,它给我类型错误。 在这个问题上我非常需要帮助。

import cv2, sys, numpy, os
import dlib
from skimage import io
import json
import uuid
import random
from datetime import datetime
from random import randint
#predictor_path = sys.argv[1]
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
size = 4
detector = dlib.get_frontal_face_detector()
#predictor = dlib.shape_predictor(predictor_path)
options=dlib.get_frontal_face_detector()
options.num_threads = 4
options.be_verbose = True

win = dlib.image_window()

# Part 1: Create fisherRecognizer
print('Training...')

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)

for (subdirs, dirs, files) in os.walk(fn_dir):
    for subdir in dirs:
        names[id] = subdir
        subjectpath = os.path.join(fn_dir, subdir)
        for filename in os.listdir(subjectpath):
            path = subjectpath + '/' + filename
            lable = id
            images.append(cv2.imread(path, 0))
            lables.append(int(lable))
        id += 1

(im_width, im_height) = (112, 92)

# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]

# OpenCV trains a model from the images

model = cv2.createFisherFaceRecognizer(0,500)
model.train(images, lables)

haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
webcam.set(5,30)
while True:
    (rval, frame) = webcam.read()
    frame=cv2.flip(frame,1,0)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))

    dets = detector(gray, 1)

    print "length", len(dets)

    print("Number of faces detected: {}".format(len(dets)))
    for i, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            i, d.left(), d.top(), d.right(), d.bottom()))

    cv2.rectangle(gray, (d.left(), d.top()), (d.right(), d.bottom()), (0, 255, 0), 3)


    '''
        #Try to recognize the face
        prediction  = model.predict(dets)
        print "Recognition Prediction" ,prediction'''





    win.clear_overlay()
    win.set_image(gray)
    win.add_overlay(dets)

if (len(sys.argv[1:]) > 0):
    img = io.imread(sys.argv[1])
    dets, scores, idx = detector.run(img, 1, -1)
    for i, d in enumerate(dets):
        print("Detection {}, score: {}, face_type:{}".format(
            d, scores[i], idx[i]))

最佳答案

应该是这样的:

crop_img = img_full[d.top():d.bottom(),d.left():d.right()]

关于python - 如何在 dlib python 中保存/裁剪检测到的人脸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40008806/

相关文章:

c++ - 当画面中没有人脸时,OpenCV 中的人脸检测器变慢

python - 谷歌云平台,机器学习引擎, "No module named absl"

python - 如何删除列表组合列表中列表的特定元素?

python - 使用 Python 访问内存映射文件

OpenCV 2.4.6 SIFT 关键点检测使用大量内存

android - 具有 LBP 级联的 javaCV detectMultiScale 在物理设备上不起作用

python - 具有多个 Pandas DataFrame 的并排箱线图

python - 从图像中查找车辆的速度

c++ - OpenCV WarpPerspective 问题

python - 如何创建自己的 haar 级联并应用它?