当前位置: 首页 > article >正文

基于Openface在ubuntu上抽取人脸图像

如题目所示,直接上代码

NOTE:代码中的路径注意更换

import os
import glob
import time
import shutil
import subprocess
import concurrent.futures
from joblib import Parallel, delayed
from tqdm import tqdm

#------------------------------------------#
# 对单个视频文件进行处理
# 适配于Ubuntu-20.04操作系统
#------------------------------------------#
def process_one_video(video_file, in_dir, out_dir, img_size=112):
    file_name = os.path.splitext(os.path.basename(video_file))[0]
    out_dir   = os.path.join(out_dir, file_name)

    if os.path.exists(out_dir):
        print(f'Note: "{out_dir}" already exist!')
        return video_file
    else:
        os.makedirs(out_dir)

    cmd = f'/home/data/openface/OpenFace/build/bin/FeatureExtraction -f "{video_file}" -out_dir "{out_dir}" -simalign -simsize {img_size} -format_aligned jpg -nomask'
    subprocess.call(cmd, shell=True)

    return video_file

#----------------------------------------#
# 基于上面的process_one_video函数构建主函数
# 默认使用多进程进行处理
#----------------------------------------#
def main(video_dir, out_dir, multi_process=True, video_template_path='*.mp4', img_size=112):
    #---------------------------------#
    # 根据视频的类型不同 需要更换后缀名
    #---------------------------------#
    video_files = glob.glob(os.path.join(video_dir, video_template_path))  # with emo dir

    n_files     = len(video_files)
    print(f'Total videos: {n_files}.')

    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
        
    start_time = time.time()
    #-------------------------#
    # 多进程处理
    # 我们默认进行多线程处理
    #-------------------------#
    if multi_process:
        Parallel(n_jobs=256)(delayed(process_one_video)(video_file, video_dir, out_dir, img_size) for video_file in tqdm(video_files))
    else:
        for i, video_file in enumerate(video_files, 1):
            print(f'Processing "{os.path.basename(video_file)}"...')
            process_one_video(video_file, video_dir, out_dir, img_size)
            print(f'"{os.path.basename(video_file)}" done, rate of progress: {100.0 * i / n_files:3.0f}% ({i}/{n_files})')

    end_time = time.time()
    print('Time used for video face extraction: {:.1f} s'.format(end_time - start_time))
 
#------------------------#
# 对目录的文件结构进行复制
#------------------------#
def copy_one_video(src_dir, tgt_dir):
    shutil.copytree(src_dir, tgt_dir)
    print(f'Copy "{src_dir}" to "{tgt_dir}"')


if __name__ == '__main__':
    #--------------------------------#
    # 确定数据集根目录与视频地址
    #--------------------------------#
    video_dir    = '/home/data/labeled/data/clips'
    img_size     = 256

    #-----------------------------#
    # 确定视频模板与输出文件夹地址
    #-----------------------------#
    file_ext            = 'mp4'
    video_template_path = f'*.{file_ext}'
    out_dir             = "/home/data/labeled/data/openface" 

    #----------------------------------#
    # 1. 使用openface进行人脸的抽取
    #----------------------------------#
    main(video_dir, out_dir, video_template_path=video_template_path, multi_process=False, img_size=img_size)

    #------------------------------#
    # 2. 重新组织已提取人脸的文件夹
    # P.S. 最后输出为face_aligned
    #------------------------------#
    src_root = out_dir
    tgt_root = out_dir.replace('openface', 'face_aligned')
    count    = 0
    src_dirs, tgt_dirs = [], []

    for sample_dir in os.scandir(src_root):
            sample_name = sample_dir.name

            tgt_dir     = os.path.join(tgt_root, sample_name) # organize videos in the original way
            src_dir     = os.path.join(sample_dir, f'{sample_name}_aligned')

            src_dirs.append(src_dir)
            tgt_dirs.append(tgt_dir)
            count += 1

    print(f'Total videos: {count}.')
    Parallel(n_jobs=64)(delayed(copy_one_video)(src_dir, tgt_dir) for src_dir, tgt_dir in tqdm(zip(src_dirs, tgt_dirs)))

openface的安装可以参考我的另外一篇blog:
https://blog.csdn.net/m0_47623548/article/details/138171121?spm=1001.2014.3001.5501


http://www.kler.cn/news/292540.html

相关文章:

  • 02【SQL sever 2005数据库安装教程】
  • python学习第三节:创建第一个python项目
  • Python 数据分析— Numpy 基本操作(下)
  • 【大模型实战篇】大模型周边NLP技术回顾及预训练模型数据预处理过程解析(预告)
  • tkcalendar中的DateEntry
  • CLION+gdbserver远程调试postgresql源码
  • 前端Vue框架,本地数据库nedb
  • python 打包tkinter图标问题
  • fastadmin 文件上传七牛云
  • html中,想添加一段文字,使用什么标签最合理?
  • nginx的基本使用示例(负载均衡,虚拟主机,动静分离)的详细配置过程
  • 2024挖漏洞给报酬的网站汇总,兼职副业3天收益2k
  • 路灯线路电气安全存在的问题与防护措施
  • ARM基础知识
  • 使用C++编写接口调用PyTorch模型,并生成DLL供.NET使用
  • pytest 常用的辅助函数和工具函数
  • springboot 实现策略模式通过id进入不同的服务类service
  • C++ 设计模式——解释器模式
  • 免费OCR 文字识别工具
  • 【机器学习】神经网络、隐藏层的基本概念、如何选择隐藏层数量以及胶囊网络对神经网络的影响
  • PowerShell脚本编写:自动化Windows开发工作流程实例介绍
  • 2024年全国大学生数学建模C题解题思路
  • 第 2 章:AJAX 的使用
  • C++:类型转换
  • 12、Flink 解决流上的确定性最佳实践
  • 使用视图方式操作MySQL数据表
  • Java笔试面试题AI答之JDBC(1)
  • C语言-数据结构 无向图普里姆Prim算法(邻接矩阵存储)
  • selenium连接远程chrome浏览器
  • MIT线性代数