dlib实现,需要shape_predictor_68_face_landmarks.dat模型


    # 读帧
    videoCapture = cv2.VideoCapture(videoName)
    date = time.strftime("%Y-%m-%d %H:%M:%S")
    # 计数
    i = 0
    print('{} save image start'.format(videoName))
    while True:
        success, img = videoCapture.read()
        try:
            if success:
                i = i + 1
                # save_image(img, savePath + "_" + str(i))
                # 旋转图片
                if angle > 0:
                    img = imutils.rotate_bound(img, angle)
                gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                dets = detector(gray, 1)
                for face in dets:
                    shape = predictor(img, face)
                    chang = []
                    kuan = []
                    # 遍历所有点,打印出其坐标,并圈出来
                    for pt in shape.parts():
                        chang.append(pt.x)
                        kuan.append(pt.y)
                    # 调节人像大小
                    x1, x2 = max(chang) + 60, min(chang) - 65
                    minSize = min(kuan)
                    y1, y2 = max(kuan) + 100, minSize - (minSize - 20)
                    cropped = img[y2 + 1:y1, x2 + 1:x1]

                    cropped = cv2.resize(cropped, (IMAGE_SIZE, IMAGE_SIZE), cv2.INTER_CUBIC)
                    cv2.imwrite(savePath + "_" + str(i) + '.jpg', cropped)
                    print(date + " "+ savePath + "_" + str(i) + '.jpg')
            else:
                print('{} {} total {} images'.format(date, videoName, i))
                print('{} {} image save end'.format(date, videoName))
                break
        except Exception as e:
            continue
    videoCapture.release()
    cv2.destroyAllWindows()

更新:截取图片人脸dilb实现


import cv2
import dlib

path = "./data/iphone2.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
  "./model/shape_predictor_68_face_landmarks.dat"
)
color = (0, 255, 0) # 定义绘制颜色

dets = detector(gray, 1)
for face in dets:
  shape = predictor(img, face) # 寻找人脸的68个标定点
  chang = []
  kuan = []
  for pt in shape.parts():
    chang.append(pt.x)
    kuan.append(pt.y)
  # 调节人像大小
  x1, x2 = max(chang), min(chang)
  y1, y2 = max(kuan), min(kuan)
  cropped = img[y2 + 1:y1, x2 + 1:x1]
  # cv2.circle(img, (x2, y2), 1, (255, 0, 0), thickness=5)
  # cv2.circle(img, (x1, y1), 1, (255, 0, 0), thickness=5)
  # cv2.rectangle(img, (x2, y2), (x1, y1), color, 1)
  # cropped = img[0:x1, 0:x1] # 裁剪坐标为[y0:y1, x0:x1]
  cropped = img[y2:y1, x2:x1]
  # print(cropped.shape)
  cv2.imshow("image", cropped)
  k = cv2.waitKey(0)
  if k == ord("s"):
    cv2.imwrite("./data/test1.png", cropped)
cv2.destroyAllWindows()

opencv实现抠图,需要haarcascade_frontalface_alt2.xml模型


    cap = cv2.VideoCapture(path)
    classfier = cv2.CascadeClassifier("./model/haarcascade_frontalface_alt2.xml")
    suc = cap.isOpened()  # 是否成功打开
    frame_count = 0
    out_count = 0
    while suc:
        frame_count += 1
        if out_count > 5:  # 最多取出多少张
            break
        suc, frame = cap.read()  # 读取一帧
        # grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faceRects = classfier.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))  # 读取脸部位置
        if len(faceRects) > 0:  # 大于0则检测到人脸
            for faceRect in faceRects:  # 单独框出每一张人脸
                x, y, w, h = faceRect
                image = frame[y - 10: y + h + 10, x - 10: x + w + 10]
                # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # 转为灰度图
                img_new = cv2.resize(image, (64, 64), interpolation=cv2.INTER_CUBIC)  # 处理面部的大小
                cv2.imwrite('./data/%d.jpg' % out_count, img_new,[95])  # 存储到指定目录
                out_count += 1
                print('成功提取的第%d个脸部' % out_count)
                break  # 每帧只获取一张脸,删除这个即为读出全部面部
    cap.release()
    cv2.destroyAllWindows()

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐