人像面部关键点检测

此工作为本人近期做人脸情绪识别,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  # 关键点半径 只能为整型
    )
相关推荐
Funny_AI_LAB38 分钟前
李飞飞联合杨立昆发表最新论文:超感知AI模型从视频中“看懂”并“预见”三维世界
人工智能·算法·语言模型·音视频
数据皮皮侠4 小时前
区县政府税务数据分析能力建设DID(2007-2025)
大数据·数据库·人工智能·信息可视化·微信开放平台
极小狐6 小时前
比 Cursor 更丝滑的 AI DevOps 编程智能体 - CodeRider-Kilo 正式发布!
运维·人工智能·devops
半臻(火白)6 小时前
Prompt-R1:重新定义AI交互的「精准沟通」范式
人工智能
菠菠萝宝6 小时前
【AI应用探索】-10- Cursor实战:小程序&APP - 下
人工智能·小程序·kotlin·notepad++·ai编程·cursor
连线Insight7 小时前
架构调整后,蚂蚁继续死磕医疗健康“硬骨头”
人工智能
小和尚同志7 小时前
十月份 AI Coding 实践!Qoder、CC、Codex 还是 iflow?
人工智能·aigc
keke.shengfengpolang7 小时前
中专旅游管理专业职业发展指南:从入门到精通的成长路径
人工智能·旅游
Danceful_YJ7 小时前
35.微调BERT
人工智能·深度学习·bert
ZPC82107 小时前
FPGA 部署ONNX
人工智能·python·算法·机器人