前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【代码】读取图像,计算面宽比,并保存至表格

【代码】读取图像,计算面宽比,并保存至表格

作者头像
未名编程
发布2024-10-12 20:57:53
1130
发布2024-10-12 20:57:53
举报
文章被收录于专栏:Python
计算面宽比

此处计算(第一个点和第17个点之间的距离)/(第28个点和第52个点之间的距离)

代码语言:javascript
复制
import cv2
import dlib
import math
import numpy as np  # 导入numpy库



#读取图片
img = cv2.imread("C:/Users/Lenovo/Desktop/c.jpg")
print(img.shape)

#图像转化为灰度图

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#获得脸部位置检测器(加载人脸检测模块)
detector = dlib.get_frontal_face_detector()

#使用detector进行人脸检测 dets为返回的结果
faces = detector(gray_img, 1)

#用dlib库加载预测模型
predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

for k, d in enumerate(faces):
    shape = predictor(gray_img, d)

    # 计算第一个点和第17个点之间的距离
    point_1 = shape.part(0)   # 第一个特征点
    point_17 = shape.part(16) # 第17个特征点
    distance_1 = math.sqrt((point_1.x - point_17.x)**2 + (point_1.y - point_17.y)**2)

    # 计算第28个点和第52个点之间的距离
    point_28 = shape.part(27) # 第28个特征点
    point_52 = shape.part(51) # 第52个特征点
    distance_2 = math.sqrt((point_28.x - point_52.x)**2 + (point_28.y - point_52.y)**2)

    # 计算面部比例
    facial_ratio = distance_1 / distance_2

    print("面部比例是:", facial_ratio)
读取某一文件夹下的图片并计算面宽比,并保存至表格

将代码放到图片同级路径,注意更改图片路径和表格存放路径

代码语言:javascript
复制
import cv2
import dlib
import math
import numpy as np  # 导入numpy库

import os
from PIL import Image
import pandas as pd

folder_path = r"F:\face_code\pics"
filename_list = []
for filename in os.listdir(folder_path):
    filepath = os.path.join(folder_path, filename)

    # 检查文件类型是不是图片
    if os.path.isfile(filepath) and filepath.endswith(('.bmp','.jpg')):
        # 打开图片并处理
        with Image.open(filepath) as img:
            # TODO: 在这里进行你的图片处理操作
            # 示例代码:输出图片信息
            print(filename, "- Size:", img.size, "- Format:", img.format)
            filename_list.append(filename)
facial_ratio_list = []
for filename in filename_list:
    #读取图片
    print(filename)
    img = cv2.imread(filename)
    print(img.shape)

    #图像转化为灰度图

    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #获得脸部位置检测器(加载人脸检测模块)
    detector = dlib.get_frontal_face_detector()

    #使用detector进行人脸检测 dets为返回的结果
    faces = detector(gray_img, 1)

    #用dlib库加载预测模型
    predictor=dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    for k, d in enumerate(faces):
        shape = predictor(gray_img, d)

        # 计算第一个点和第17个点之间的距离
        point_1 = shape.part(0)   # 第一个特征点
        point_17 = shape.part(16) # 第17个特征点
        distance_1 = math.sqrt((point_1.x - point_17.x)**2 + (point_1.y - point_17.y)**2)

        # 计算第28个点和第52个点之间的距离
        point_28 = shape.part(27) # 第28个特征点
        point_52 = shape.part(51) # 第52个特征点
        distance_2 = math.sqrt((point_28.x - point_52.x)**2 + (point_28.y - point_52.y)**2)

        # 计算面部比例
        facial_ratio = distance_1 / distance_2

        print("面部比例是:", facial_ratio)
        facial_ratio_list.append(facial_ratio)
print(filename_list)
print(facial_ratio_list)

# 创建数据
data = {
    '图片': filename_list,
    '面宽比': facial_ratio_list
}

# 将数据转换为DataFrame
df = pd.DataFrame(data)

# 将数据输出到Excel文件
filepath = 'F:/face_code/excel.xlsx'
df.to_excel(filepath, index=False)
安装dlib报错怎么办
代码语言:javascript
复制
pip install dlib==19.6.1
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 计算面宽比
  • 读取某一文件夹下的图片并计算面宽比,并保存至表格
  • 安装dlib报错怎么办
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档