人像面部关键点检测

此工作为本人近期做人脸情绪识别,CBAM模块前是否能加人脸关键点检测而做的尝试。由于创新点不是在于检测点的标注,而是CBAM的改进,因此,只是借用了现成库Dilb与cv2进行。

首先,下载人脸关键点预测模型:Index of /files,文件:shape_predictor_68_face_landmarks.dat

逻辑如下:

使用cv2库进行图像读取--->

将读取的图像转为灰度图--->

判断该图是否存在face--->否--->return

将读取的图像输入预测模型--->

进行关键点预测--->(存储关键点位置)

在原图上进行关键点标识--->

保存预测后的图。

效果图如下:(使用fer2013数据集)

Python代码如下

python 复制代码
import os
import cv2
import dlib
import numpy as np

# init
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("./shape_predictor_68_face_landmarks.dat")

def process_image(image_path, output_dir="output", point_radius=0.1):

    os.makedirs(output_dir, exist_ok=True)
    
    # cv2 read image
    image = cv2.imread(image_path)
    # image to gray
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = detector(gray, 1)
    
    if len(faces) == 0:
        print(f"no face!:{image_path}")
        return None
    
    # get point
    landmarks = predictor(gray, faces[0])
    landmarks = np.array([[p.x, p.y] for p in landmarks.parts()])
    
    # draw key point for face
    for (x, y) in landmarks:
        cv2.circle(image, (x, y), radius=point_radius, color=(0, 255, 0), thickness=-1)
    
    output_path = os.path.join(output_dir, f"processed_{os.path.basename(image_path)}")
    cv2.imwrite(output_path, image)
    print(f"Saved:{output_path}")
    
    return landmarks

if __name__=="__main__":
    landmarks = process_image(
        image_path="./00001.png",
        output_dir="./processed_00001",
        point_radius=1  # 关键点半径 只能为整型
    )
相关推荐
陈少波AI应用笔记3 分钟前
OpenClaw安全实测:4种攻击方式与防护指南
人工智能
小锋java12343 分钟前
【技术专题】嵌入模型与Chroma向量数据库 - Chroma 集合查询操作
人工智能
ZFSS5 分钟前
OpenAI Images Edits API 申请及使用
前端·人工智能
Jackson_Li16 分钟前
Claude Code团队成员Thariq的Agent开发心得:Seeing like an agent
人工智能
卡尔AI工坊17 分钟前
2026年3月,我实操后最推荐的3个AI开源项目
人工智能·开源·ai编程
骑着小黑马23 分钟前
Electron + Vue3 + AI 做了一个新闻生成器:从 0 到 1 的完整实战记录
前端·人工智能
风象南9 小时前
我把大脑开源给了AI
人工智能·后端
Johny_Zhao11 小时前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
飞哥数智坊11 小时前
我帮你读《一人公司(OPC)发展研究》
人工智能
冬奇Lab15 小时前
OpenClaw 源码精读(3):Agent 执行引擎——AI 如何「思考」并与真实世界交互?
人工智能·aigc