retinaface人脸对齐

2023-11-14

yolov5_face 人脸对齐:

yolov5-face_align.rar-深度学习文档类资源-CSDN下载

GitHub - foamliu/MobileFaceNet-PyTorch: PyTorch implementation of MobileFaceNets

调用:

从左到右,从上到下,依次5个点

这个需要的是facial5points 格式是:

[x1,x2,x3,x4,x5][y1,y2,y3,y4,y5]

def align_face(img_fn, facial5points):
    raw = cv2.imread(img_fn, True)  # BGR
    facial5points = np.reshape(facial5points, (2, 5))

    crop_size = (image_h, image_w)

    default_square = True
    inner_padding_factor = 0.25
    outer_padding = (0, 0)
    output_size = (image_h, image_w)

    # get the reference 5 landmarks position in the crop settings
    reference_5pts = get_reference_facial_points(
        output_size, inner_padding_factor, outer_padding, default_square)

    # dst_img = warp_and_crop_face(raw, facial5points)
    dst_img = warp_and_crop_face(raw, facial5points, reference_pts=reference_5pts, crop_size=crop_size)
    return dst_img

align_faces.py:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 24 15:43:29 2017
@author: zhaoy
"""
import cv2
import numpy as np
from skimage import transform as trans

# reference facial points, a list of coordinates (x,y)
REFERENCE_FACIAL_POINTS = [
    [30.29459953, 51.69630051],
    [65.53179932, 51.50139999],
    [48.02519989, 71.73660278],
    [33.54930115, 92.3655014],
    [62.72990036, 92.20410156]
]

DEFAULT_CROP_SIZE = (96, 112)


class FaceWarpException(Exception):
    def __str__(self):
        return 'In File {}:{}'.format(
            __file__, super.__str__(self))


def get_reference_facial_points(output_size=None,
                                inner_padding_factor=0.0,
                                outer_padding=(0, 0),
                                default_square=False):
    tmp_5pts = np.array(REFERENCE_FACIAL_POINTS)
    tmp_crop_size = np.array(DEFAULT_CROP_SIZE)

    # 0) make the inner region a square
    if default_square:
        size_diff = max(tmp_crop_size) - tmp_crop_size
        tmp_5pts += size_diff / 2
        tmp_crop_size += size_diff

    # print('---> default:')
    # print('              crop_size = ', tmp_crop_size)
    # print('              reference_5pts = ', tmp_5pts)

    if (output_size and
            output_size[0] == tmp_crop_size[0] and
            output_size[1] == tmp_crop_size[1]):
        # print('output_size == DEFAULT_CROP_SIZE {}: return default reference points'.format(tmp_crop_size))
        return tmp_5pts

    if (inner_padding_factor == 0 and
            outer_padding == (0, 0)):
        if output_size is None:
            print('No paddings to do: return default reference points')
            return tmp_5pts
        else:
            raise FaceWarpException(
                'No paddings to do, output_size must be None or {}'.format(tmp_crop_size))

    # check output size
    if not (0 <= inner_padding_factor <= 1.0):
        raise FaceWarpException('Not (0 <= inner_padding_factor <= 1.0)')

    if ((inner_padding_factor > 0 or outer_padding[0] > 0 or outer_padding[1] > 0)
            and output_size is None):
        output_size = tmp_crop_size * \
                      (1 + inner_padding_factor * 2).astype(np.int32)
        output_size += np.array(outer_padding)
        print('              deduced from paddings, output_size = ', output_size)

    if not (outer_padding[0] < output_size[0]
            and outer_padding[1] < output_size[1]):
        raise FaceWarpException('Not (outer_padding[0] < output_size[0]'
                                'and outer_padding[1] < output_size[1])')

    # 1) pad the inner region according inner_padding_factor
    # print('---> STEP1: pad the inner region according inner_padding_factor')
    if inner_padding_factor > 0:
        size_diff = tmp_crop_size * inner_padding_factor * 2
        tmp_5pts += size_diff / 2
        tmp_crop_size += np.round(size_diff).astype(np.int32)

    # print('              crop_size = ', tmp_crop_size)
    # print('              reference_5pts = ', tmp_5pts)

    # 2) resize the padded inner region
    # print('---> STEP2: resize the padded inner region')
    size_bf_outer_pad = np.array(output_size) - np.array(outer_padding) * 2
    # print('              crop_size = ', tmp_crop_size)
    # print('              size_bf_outer_pad = ', size_bf_outer_pad)

    if size_bf_outer_pad[0] * tmp_crop_size[1] != size_bf_outer_pad[1] * tmp_crop_size[0]:
        raise FaceWarpException('Must have (output_size - outer_padding)'
                                '= some_scale * (crop_size * (1.0 + inner_padding_factor)')

    scale_factor = size_bf_outer_pad[0].astype(np.float32) / tmp_crop_size[0]
    # print('              resize scale_factor = ', scale_factor)
    tmp_5pts = tmp_5pts * scale_factor
    #    size_diff = tmp_crop_size * (scale_factor - min(scale_factor))
    #    tmp_5pts = tmp_5pts + size_diff / 2
    tmp_crop_size = size_bf_outer_pad
    # print('              crop_size = ', tmp_crop_size)
    # print('              reference_5pts = ', tmp_5pts)

    # 3) add outer_padding to make output_size
    reference_5point = tmp_5pts + np.array(outer_padding)
    tmp_crop_size = output_size
    # print('---> STEP3: add outer_padding to make output_size')
    # print('              crop_size = ', tmp_crop_size)
    # print('              reference_5pts = ', tmp_5pts)
    #
    # print('===> end get_reference_facial_points\n')

    return reference_5point


def get_affine_transform_matrix(src_pts, dst_pts):
    tfm = np.float32([[1, 0, 0], [0, 1, 0]])
    n_pts = src_pts.shape[0]
    ones = np.ones((n_pts, 1), src_pts.dtype)
    src_pts_ = np.hstack([src_pts, ones])
    dst_pts_ = np.hstack([dst_pts, ones])

    A, res, rank, s = np.linalg.lstsq(src_pts_, dst_pts_)

    if rank == 3:
        tfm = np.float32([
            [A[0, 0], A[1, 0], A[2, 0]],
            [A[0, 1], A[1, 1], A[2, 1]]
        ])
    elif rank == 2:
        tfm = np.float32([
            [A[0, 0], A[1, 0], 0],
            [A[0, 1], A[1, 1], 0]
        ])

    return tfm


def warp_and_crop_face(src_img,  # BGR
                       facial_pts,
                       reference_pts=None,
                       crop_size=(96, 112),
                       align_type='smilarity'):
    if reference_pts is None:
        if crop_size[0] == 96 and crop_size[1] == 112:
            reference_pts = REFERENCE_FACIAL_POINTS
        else:
            default_square = False
            inner_padding_factor = 0
            outer_padding = (0, 0)
            output_size = crop_size

            reference_pts = get_reference_facial_points(output_size,
                                                        inner_padding_factor,
                                                        outer_padding,
                                                        default_square)

    ref_pts = np.float32(reference_pts)
    ref_pts_shp = ref_pts.shape
    if max(ref_pts_shp) < 3 or min(ref_pts_shp) != 2:
        raise FaceWarpException(
            'reference_pts.shape must be (K,2) or (2,K) and K>2')

    if ref_pts_shp[0] == 2:
        ref_pts = ref_pts.T

    src_pts = np.float32(facial_pts)
    src_pts_shp = src_pts.shape
    if max(src_pts_shp) < 3 or min(src_pts_shp) != 2:
        raise FaceWarpException(
            'facial_pts.shape must be (K,2) or (2,K) and K>2')

    if src_pts_shp[0] == 2:
        src_pts = src_pts.T

    if src_pts.shape != ref_pts.shape:
        raise FaceWarpException(
            'facial_pts and reference_pts must have the same shape')

    if align_type is 'cv2_affine':
        tfm = cv2.getAffineTransform(src_pts[0:3], ref_pts[0:3])
    #        print('cv2.getAffineTransform() returns tfm=\n' + str(tfm))
    elif align_type is 'affine':
        tfm = get_affine_transform_matrix(src_pts, ref_pts)
    #        print('get_affine_transform_matrix() returns tfm=\n' + str(tfm))
    else:
        # tfm = get_similarity_transform_for_cv2(src_pts, ref_pts)
        tform = trans.SimilarityTransform()
        tform.estimate(src_pts, ref_pts)
        tfm = tform.params[0:2, :]

    face_img = cv2.warpAffine(src_img, tfm, (crop_size[0], crop_size[1]))

    return face_img  # BGR

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

retinaface人脸对齐 的相关文章

随机推荐

  • c语言中weak的作用

    转载至 https blog csdn net q2519008 article details 82774774 在u boot源码中看到 weak关键字 在移植过程中遇到了问题 用例 weak在不同的环境中用法不同 在stm32源码中也
  • 合宙Air105

    基础资料 基于Air105开发板 Air105 LuatOS 文档 上手 开发上手 LuatOS 文档 探讨重点 官方SFUD库操作 外置flash demo相关内容的学习及探讨 扩展 合宙Air103 SDIO 扩展 LuatOS SOC
  • 服务器UDIMM, LRDIMM,RDIMM三种内存的区别

    服务器UDIMM LRDIMM RDIMM三种内存的区别 UDIMM RDIMM LRDIMM 区别与应用 随着应用程序的不断增长 内存被迫承担着更大压力 目前不管是服务器租用还是PC领域 DDR4内存技术依旧是主流 由于DDR4采用并行传
  • keil stm32f407工程环境搭建

    一 库函数 1 安装https www keil com dd2 Pack 2Feula container 直接双击Keil STM32F4xx DFP 1 0 8 pack 二 创建工程 为当前工程添加相应的库函数 点击确定 函数添加成
  • 可能影响经济体安全的技术类别

    这份新的技术出口管理新提案内容相对简洁 清晰罗列了可能会影响强大国家安全或者经济体的14类新兴和基础技术 1 生物技术 例如 1 纳米生物学 2 合成生物学 3 基因组和基因工程 4 神经科学 2 人工智能 AI 和机器学习技术 例如 1
  • mac 下 jdgui invalid input fileloader

    在一次反编译中 前面几个步骤都是正确的 将classes dex成功转为classes dex2jar jar文件 在即将把classes dex2jar jar文件在jd gui中打开的时候 出现了jdgui invalid input
  • 判断ListView的第一个/最后一个item是否完全显示

    判断最后一项 亲测可用 当然网上还有很多其他的方法 不同场景的方式可能不一样 Override public void onScroll AbsListView view int firstVisibleItem int visibleIt
  • Android shape渐变色用代码怎么写?

    前言 shape在实际开发中非常常用 一般我们会在xml中使用 但涉及到颜色动态变更时 我们需要在代码中动态创建 xml中的shape 实际上被创建出来后它是一个Drawable 点开Drawable的子类一看 我们很容易就发现一些可疑的实
  • 【Android】DataBinding 最全使用解析

    DataBinding 最全使用解析 一 DataBinding 概述 二 基本用法 2 1 使用入门 2 2 布局和绑定表达式 2 3 事件绑定 2 4 单向绑定 2 5 双向绑定 三 高级用法 BindingAdapter 一 Data
  • Linux节点释放,关于linux:如何释放Inode的使用量?

    我有一个磁盘驱动器 其索引节点使用率为100 使用df i命令 但是 在实质上删除文件后 使用率仍为100 那么正确的方法是什么 磁盘空间使用较少的磁盘驱动器如何可能具有 Inode的使用率比磁盘空间使用率更高的磁盘驱动器高 如果我压缩大量
  • 前端面试大全(jQuery篇——含移动端常见问题)

    目录 面试系列 内容介绍 1 JQuery的源码看过吗 能不能简单概况一下它的实现原理 2 jQuery fn的init方法返回的this指的是什么对象 为什么要返回this 3 jquery中如何将数组转化为json字符串 然后再转化回来
  • harbor的https访问方式及自定义证书

    一 基本安装 docker docker compose 二 https访问harbor需要自定义证书 1 首先创建存放证书的目录 到对应目录证书的位置 root host1 harbor mkdir opt cert cd opt cer
  • matlab fminbnd 寻找区域极值

    fminbnd 进行有约束的一元函数最小值求解 它的求解命令是 X FMINBND FUN x1 x2 FUN 是目标函数 可以为表达式字符串或MATLAB自定义函数的函数柄 要求解在约束 x1 lt X lt x2下的最优解X 还有其他一
  • 干货丨什么是虚拟化技术?虚拟化常见架构

    在计算机中 虚拟化 英语 Virtualization 是一种资源管理技术 是将计算机的各种实体资源 如服务器 网络 内存及存储等 予以抽象 转换后呈现出来 打破实体结构间的不可切割的障碍 使用户可以比原本的组态更好的方式来应用这些资源 这
  • STM32固件库(标准外设库)入门学习 第六章TIM定时器(二)

    STM32固件库 标准外设库 入门学习 第六章TIM定时器 二 文章目录 STM32固件库 标准外设库 入门学习 第六章TIM定时器 二 前言 一 定时中断代码 1接线图 2 程序编写 2 1 第一步开启RCC时钟 2 2 第二步选择时基单
  • 大数据应用期末总评

    本作业来自于 https edu cnblogs com campus gzcc GZCC 16SE1 homework 3363 一 将爬虫大作业产生的csv文件上传到HDFS 将爬虫大作业中爬取到的数据文件csv导入到 usr loca
  • 前端API接口的调用

    一 开启API接口 首先我们把模型部署在自己的服务器上之后开启模型的接口 linux环境下 进入模型文件 输入命令行 bash webui sh listen api 实现api接口的开启 我们获得一个api接口的地址 二 API接口调用并
  • Springboot的部分依赖及作用

    SpringBoot2使用Undertow来提高应用性能 spring boot starter undertow
  • 【Linux】---文件基础I/O

    文章目录 回顾C语言文件操作接口 文件相关的系统调用接口 打开和关闭文件 文件的打开方式 文件描述符 文件描述符的分配规则 write read 重定向 dup2 mysell 回顾C语言文件操作接口 在C语言中对于文件的操作有着几个常用的
  • retinaface人脸对齐

    yolov5 face 人脸对齐 yolov5 face align rar 深度学习文档类资源 CSDN下载 GitHub foamliu MobileFaceNet PyTorch PyTorch implementation of M