人像面部关键点检测

此工作为本人近期做人脸情绪识别,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  # 关键点半径 只能为整型
    )
相关推荐
云知谷10 分钟前
【C++基本功】C++适合做什么,哪些领域适合哪些领域不适合?
c语言·开发语言·c++·人工智能·团队开发
rit843249939 分钟前
基于MATLAB实现基于距离的离群点检测算法
人工智能·算法·matlab
初学小刘2 小时前
深度学习:从图片数据到模型训练(十分类)
人工智能·深度学习
递归不收敛2 小时前
大语言模型(LLM)入门笔记:嵌入向量与位置信息
人工智能·笔记·语言模型
之墨_3 小时前
【大语言模型】—— 自注意力机制及其变体(交叉注意力、因果注意力、多头注意力)的代码实现
人工智能·语言模型·自然语言处理
从孑开始4 小时前
ManySpeech.MoonshineAsr 使用指南
人工智能·ai·c#·.net·私有化部署·语音识别·onnx·asr·moonshine
涛涛讲AI4 小时前
一段音频多段字幕,让音频能够流畅自然对应字幕 AI生成视频,扣子生成剪映视频草稿
人工智能·音视频·语音识别
可触的未来,发芽的智生4 小时前
新奇特:黑猫警长的纳米世界,忆阻器与神经网络的智慧
javascript·人工智能·python·神经网络·架构
WWZZ20254 小时前
快速上手大模型:机器学习2(一元线性回归、代价函数、梯度下降法)
人工智能·算法·机器学习·计算机视觉·机器人·大模型·slam
AKAMAI5 小时前
数据孤岛破局之战 :跨业务分析的难题攻坚
运维·人工智能·云计算