对AVEC2014视频进行Dlib或MTCNN人脸裁剪

ops/2024/11/23 21:01:42/

预处理:人脸裁剪对齐保存的操作

Dlib

dlib windows包在资源里
其他代码可查看注释帮助理解

import os
import random
import cv2
import dlib
from imutils.face_utils import FaceAligner, rect_to_bb
from tqdm import tqdm  # 引入tqdm库# 配置路径
dataset_path = 'datasets/AVEC2014'  # 原始数据集路径
output_path = 'datasets/avec14'  # 输出路径
crop_size = 256  # 人脸裁剪后的大小# 获取人脸对齐器
def get_face(fa, image):detector = dlib.get_frontal_face_detector()  # 获取人脸检测器gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # 将图像转换为灰度图thresh = gray.shape[0] // 4  # 设置阈值rects = detector(gray, 2)  # 检测人脸face_aligned = None  # 初始化返回的人脸图像for rect in rects:(x, y, w, h) = rect_to_bb(rect)  # 获取人脸的坐标if w > thresh:  # 如果人脸宽度大于阈值,则认为是有效人脸face_aligned = fa.align(image, gray, rect)  # 对齐人脸break  # 只处理第一张人脸return face_aligned# 处理视频
def process_video(video_path, save_dir, fa):cap = cv2.VideoCapture(video_path)  # 打开视频文件total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))  # 获取总帧数if total_frames < 64:  # 如果视频帧数少于64,跳过该视频print(f"Warning: Video '{video_path}' has less than 64 frames. Skipping.")cap.release()  # 释放视频文件returnstart_frame = random.randint(0, total_frames - 64)  # 随机选择起始帧frames = []for i in range(start_frame, start_frame + 64):  # 提取连续的64帧cap.set(cv2.CAP_PROP_POS_FRAMES, i)  # 设置当前读取的帧数ret, frame = cap.read()  # 读取该帧if ret:frames.append(frame)  # 保存读取到的帧cap.release()  # 释放视频文件for i, frame in enumerate(tqdm(frames, desc=f"Processing frames from {os.path.basename(video_path)}")):  # 加入进度条face_aligned = get_face(fa, frame)  # 对齐每一帧中的人脸if face_aligned is not None:img_name = f"{i + 1:05d}.jpg"  # 给每一帧命名save_path = os.path.join(save_dir, img_name)  # 保存路径cv2.imwrite(save_path, face_aligned)  # 保存图像else:print(f"Face not found in frame {i + 1}")  # 如果没有检测到人脸# 主函数:处理数据集中的所有视频
def align_dlib():predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")  # 加载预测器fa = FaceAligner(predictor, desiredFaceWidth=crop_size)  # 初始化人脸对齐器# 遍历主目录(Training、Development、Testing)main_dirs = ['Training', 'Development', 'Testing']for main_dir in main_dirs:main_dir_path = os.path.join(dataset_path, main_dir)if not os.path.isdir(main_dir_path):print(f"Skipping non-directory: {main_dir_path}")continue# 遍历每个子目录(Northwind、Freeform 等)sub_dirs = os.listdir(main_dir_path)for sub_dir in sub_dirs:sub_dir_path = os.path.join(main_dir_path, sub_dir)if not os.path.isdir(sub_dir_path):print(f"Skipping non-directory: {sub_dir_path}")continue# 遍历视频文件夹中的每个视频文件video_files = os.listdir(sub_dir_path)for video_file in video_files:video_path = os.path.join(sub_dir_path, video_file)if not os.path.isfile(video_path):continue# 获取视频名称(去掉文件扩展名)video_name = os.path.splitext(video_file)[0]# 构建保存路径: datasets/avec14/Training/Northwind/236_1_Northwind_videosave_path = os.path.join(output_path, main_dir, sub_dir, video_name)os.makedirs(save_path, exist_ok=True)  # 创建保存文件夹print(f"Processing video: {video_path}")process_video(video_path, save_path, fa)  # 处理该视频if __name__ == "__main__":align_dlib()  # 调用主函数进行处理

MTCNN

import os
import pandas as pd
import cv2
from tqdm import tqdm
from mtcnn import MTCNNdef get_files(path):file_info = os.walk(path)file_list = []for r, d, f in file_info:file_list += freturn file_listdef get_dirs(path):file_info = os.walk(path)dirs = []for d, r, f in file_info:dirs.append(d)return dirs[1:]def generate_label_file():print('get label....')base_url = './AVEC2014/label/DepressionLabels/'file_list = get_files(base_url)labels = []loader = tqdm(file_list)for file in loader:label = pd.read_csv(base_url + file, header=None)labels.append([file[:file.find('_Depression.csv')], label[0][0]])loader.set_description('file:{}'.format(file))pd.DataFrame(labels, columns=['file', 'label']).to_csv('./processed/label.csv', index=False)return labelsdef generate_img(path, v_type, img_path):videos = get_files(path)loader = tqdm(videos)for video in loader:name = video[:5]save_path = img_path + v_type + '/' + nameos.makedirs(save_path, exist_ok=True)cap = cv2.VideoCapture(path + video)n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))gap = int(n_frames / 100)for i in range(n_frames):success, frame = cap.read()if success and i % gap == 0:cv2.imwrite(save_path + '/{}.jpg'.format(int(i / gap)), frame, [int(cv2.IMWRITE_JPEG_QUALITY), 100])loader.set_description("data:{} type:{} video:{} frame:{}".format(path.split('/')[2], v_type, name, i))cap.release()def get_img():print('get video frames....')train_f = './AVEC2014/train/Freeform/'train_n = './AVEC2014/train/Northwind/'test_f = './AVEC2014/test/Freeform/'test_n = './AVEC2014/test/Northwind/'validate_f = './AVEC2014/dev/Freeform/'validate_n = './AVEC2014/dev/Northwind/'dirs = [train_f, train_n, test_f, test_n, validate_f, validate_n]types = ['Freeform', 'Northwind', 'Freeform', 'Northwind', 'Freeform', 'Northwind']img_path = ['./img/train/', './img/train/', './img/test/', './img/test/', './img/validate/', './img/validate/']os.makedirs('./img/train', exist_ok=True)os.makedirs('./img/test', exist_ok=True)os.makedirs('./img/validate', exist_ok=True)for i in range(6):generate_img(dirs[i], types[i], img_path[i])def get_face():print('get frame faces....')detector = MTCNN()'''save_path = ['./processed/train/Freeform/', './processed/train/Northwind/', './processed/test/Freeform/','./processed/test/Northwind/', './processed/validate/Freeform/', './processed/validate/Northwind/']paths = ['./img/train/Freeform/', './img/train/Northwind/', './img/test/Freeform/', './img/test/Northwind/','./img/validate/Freeform/', './img/validate/Northwind/']save_path = ['./processed/validate/Freeform/', './processed/validate/Northwind/']paths = ['./img/validate/Freeform/', './img/validate/Northwind/']'''save_path = [ './processed/validate/Northwind/']paths = ['./img/validate/Northwind/']for index, path in enumerate(paths):dirs = get_dirs(path)loader = tqdm(dirs)for d in loader:os.makedirs(save_path[index] + d.split('/')[-1], exist_ok=True)files = get_files(d)for file in files:img_path = d + '/' + files_path = save_path[index] + d.split('/')[-1] + '/' + fileimg = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)info = detector.detect_faces(img)if (len(info) > 0):x, y, width, height = info[0]['box']confidence = info[0]['confidence']b, g, r = cv2.split(img)img = cv2.merge([r, g, b])img = img[y:y + height, x:x + width, :]cv2.imwrite(s_path, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])loader.set_description('confidence:{:4f} img:{}'.format(confidence, img_path))if __name__ == '__main__':os.makedirs('./img', exist_ok=True)os.makedirs('./processed', exist_ok=True)os.makedirs('./processed/train', exist_ok=True)os.makedirs('./processed/test', exist_ok=True)os.makedirs('./processed/validate', exist_ok=True)label = generate_label_file()get_img()get_face()

http://www.ppmy.cn/ops/136138.html

相关文章

基于 NCD 与优化函数结合的非线性优化 PID 控制

基于 NCD 与优化函数结合的非线性优化 PID 控制 1. 引言 NCD&#xff08;Normalized Coprime Factorization Distance&#xff09;优化是一种用于非线性系统的先进控制方法。通过将 NCD 指标与优化算法结合&#xff0c;可以在动态调整控制参数的同时优化控制器性能。此方法特别…

【强化学习+组合优化】SAC + PointerNetwork 解决TSP问题

TSP强化学习环境见之前的博客&#xff1a;https://blog.csdn.net/weixin_41369892/article/details/131519384 先上效果&#xff0c;跑了20个点&#xff0c;感觉不是很好&#xff08;RL解决组合优化问题真的不好调参&#xff09; 平均总距离随训练的变化&#xff0c;可以看出的…

淘宝评论大冒险:Java爬虫的“探险记”

在互联网的海洋中&#xff0c;淘宝无疑是一座巨大的宝藏岛&#xff0c;而商品评论就是岛上那些闪闪发光的金币。今天&#xff0c;我们将化身为Java程序员&#xff0c;驾驶着我们的爬虫号&#xff0c;去淘宝的海域里寻找那些隐藏在商品页面下的评论金币。请系好安全带&#xff0…

Spring Boot Web应用开发:创建RESTful服务

Spring Boot使得创建RESTful服务变得非常简单。通过使用Spring Web MVC提供的注解&#xff0c;可以轻松定义控制器、处理请求和返回响应。 RestController与Controller的区别 RestController和Controller是Spring MVC中的两个用于定义控制器的注解&#xff0c;它们的主要区别…

Windows多JDK版本管理工具JVMs

Windows多JDK版本管理工具JVMs 官网安装使用手动下载jdk 官网 https://github.com/ystyle/jvms 下载 https://github.com/ystyle/jvms/releases 当前下载版本为v2.1.6 安装 下载后&#xff0c;解压到某个目录。 比如&#xff1a;D:\soft\JVMs\jvms_v2.1.6_amd64 把这个目录…

Vue开发04:结合Vue 2 总结C# 常见方法的JavaScript实现

测试地址 在线运行Vue组件 一、Trim()方法 去除首末 <template><div><p>原始字符串: "{{ originalStr }}"</p><p>处理后的字符串: "{{ trimmedStr }}"</p></div> </template><script> export d…

[Unity]【游戏相关】 游戏设计基础:如何创建有效的游戏设计文档

在游戏开发中,游戏设计文档(Game Design Document, GDD)是团队成员协同工作、确保项目顺利推进的关键工具。GDD不仅帮助团队清晰地定义游戏的愿景、玩法和目标,还提供了一个统一的参考标准,使开发流程更加流畅。本文将深入探讨创建GDD的常见方法和技巧,以及MVP和迭代开发…

nc网络工具的使用

nc是一个功能强大的网络工具&#xff0c;它可以用于端口扫描&#xff0c;模拟TCP/UDP数据传输&#xff0c;代理端口等。ubuntu14.04默认自带nc工具&#xff0c;windows下需自行下载&#xff0c;一般防病毒软件会认为它有害。下面主要介绍它在linux下的用法。 一.nc扫描某台主机…