Python 检测人脸筛选指定尺寸人脸图片

主要功能是处理一个指定文件夹中的所有图像文件(.jpg 和 .png),并根据图像中检测到的人脸特征,筛选和移动符合条件的图像。具体步骤如下:

  1. 人脸检测和关键点检测 : 使用 dlib 的预训练人脸检测器(frontal_face_detector)和 68 个关键点预测器来检测图像中的人脸及其关键点(如眼睛、嘴巴等)。

  2. 条件判断: 对每张图像,如果检测到的人脸宽度小于 130 像素且两眼之间的距离小于 60 像素,则将该图片移动到指定的目标文件夹中,并保持源文件夹的目录结构不变。

  3. 目录结构保持: 在移动符合条件的图片时,保持源文件夹的目录层级结构,即原始文件夹的子目录在目标文件夹中会被保留。

  4. 无效图像处理: 如果一张图像中未检测到人脸或人脸太小,则该图像也会被移动到目标文件夹,并打印相应的提示信息。

  5. 依赖库:

    • 使用 dlib 进行人脸检测和关键点识别。
    • 使用 opencv 进行图像处理。
    • 使用 os 和 shutil 处理文件和目录操作。

主要流程:

  • 加载模型 :通过 dlib 加载人脸检测器和 68 个关键点检测模型。
  • 处理文件夹:递归遍历指定源文件夹,处理所有符合条件的图像。
  • 人脸检测与筛选:对于每张图像,检测人脸并计算人脸宽度和双眼之间的距离,符合条件的图像会被移动到目标文件夹。
  • 文件移动 :使用 shutil 进行文件的移动操作,并在目标文件夹中保持与源文件夹一致的目录结构。

这个代码可以用于图像筛选工作,尤其是基于人脸特征的筛选任务,比如选择图像中人脸较小或两眼之间距离较近的图像进行进一步处理。

复制代码
import cv2
import dlib
import math
import os
import shutil

# https://www.anaconda.com/download/success
# https://github.com/sachadee/Dlib/blob/main/dlib-19.22.99-cp38-cp38-win_amd64.whl
# https://github.com/italojs/facial-landmarks-recognition/blob/master/shape_predictor_68_face_landmarks.dat

# conda create -n python38 python=3.8
# conda activate python38
# pip install dlib-19.22.99-cp38-cp38-win_amd64.whl
# pip install opencv-python

# 加载dlib的人脸检测器和68个关键点预测模型
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(r'c:\python38\shape_predictor_68_face_landmarks.dat')

# 指定源文件夹和目标文件夹
source_folder = r'c:\python38\face'
target_folder = r'c:\python38\face60'

# 创建目标文件夹,如果不存在的话
if not os.path.exists(target_folder):
    os.makedirs(target_folder)

def process_image(img_path, relative_path):
    # 加载图片
    img = cv2.imread(img_path)

    if img is None:
        print(f"无法读取文件:{img_path}")
        return

    # 转换为灰度图像
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 检测人脸
    faces = detector(gray)

    if len(faces) == 0:
        print(f"未检测到人脸或人脸太小:{img_path}")
        # 将无效人脸或没有检测到人脸的图片移动到目标文件夹
        target_subdir = os.path.join(target_folder, relative_path)
        if not os.path.exists(target_subdir):
            os.makedirs(target_subdir)
        shutil.move(img_path, os.path.join(target_subdir, os.path.basename(img_path)))
        print(f"移动文件 {img_path} 到 {target_subdir}")
        return

    for face in faces:
        # 获取人脸的边界框坐标
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()

        # 计算人脸宽度
        face_width = x2 - x1

        # 打印人脸宽度
        print(f"文件: {img_path} 人脸宽度: {face_width} 像素")

        # 使用预测模型预测人脸的68个关键点
        landmarks = predictor(gray, face)

        # 获取左右眼的关键点(左眼36-41,右眼42-47)
        left_eye_x = (landmarks.part(36).x + landmarks.part(39).x) // 2
        left_eye_y = (landmarks.part(36).y + landmarks.part(39).y) // 2
        right_eye_x = (landmarks.part(42).x + landmarks.part(45).x) // 2
        right_eye_y = (landmarks.part(42).y + landmarks.part(45).y) // 2

        # 计算双眼之间的距离
        eye_distance = math.sqrt((right_eye_x - left_eye_x) ** 2 + (right_eye_y - left_eye_y) ** 2)

        # 如果人脸宽度小于130且双眼距离小于60,移动文件
        if face_width < 130 and eye_distance < 60:
            # 目标文件夹保持目录结构
            target_subdir = os.path.join(target_folder, relative_path)
            if not os.path.exists(target_subdir):
                os.makedirs(target_subdir)
            
            # 移动文件
            shutil.move(img_path, os.path.join(target_subdir, os.path.basename(img_path)))
            print(f"移动文件 {img_path} 到 {target_subdir}")

def process_folder(source_folder):
    for root, dirs, files in os.walk(source_folder):
        for file in files:
            if file.endswith('.jpg') or file.endswith('.png'):
                img_path = os.path.join(root, file)
                # 获取相对路径以保持目录结构
                relative_path = os.path.relpath(root, source_folder)
                process_image(img_path, relative_path)

# 处理整个文件夹
process_folder(source_folder)
相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊3 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1233 天前
2026年第38周GitHub趋势周报
python·科技·github