根据双眼的坐标对齐人脸Python实现
最近要做一个人脸识别的东西,可数据库的人脸图像还没有做对齐处理。所以需要自己做人脸对齐预处理。网上查了,发现已经有人做过了类似的脚本。直接拿过来用了,原地址在这:https://github.com/bytefish/facerec/blob/master/py/apps/scripts/crop_face.py 自己为了以后更改,加了一些注释。
- # -*- coding: utf-8 -*-
- """
- Created on Thu Jan 1 16:09:32 2015
- @author: crw
- """
- # 参数含义:
- # CropFace(image, eye_left, eye_right, offset_pct, dest_sz)
- # eye_left is the position of the left eye
- # eye_right is the position of the right eye
- # 比例的含义为:要保留的图像靠近眼镜的百分比,
- # offset_pct is the percent of the image you want to keep next to the eyes (horizontal, vertical direction)
- # 最后保留的图像的大小。
- # dest_sz is the size of the output image
- #
- import sys,math,Image
- # 计算两个坐标的距离
- def Distance(p1,p2):
- dx = p2[0]- p1[0]
- dy = p2[1]- p1[1]
- return math.sqrt(dx*dx+dy*dy)
- # 根据参数,求仿射变换矩阵和变换后的图像。
- def ScaleRotateTranslate(image, angle, center =None, new_center =None, scale =None, resample=Image.BICUBIC):
- if (scale is None)and (center is None):
- return image.rotate(angle=angle, resample=resample)
- nx,ny = x,y = center
- sx=sy=1.0
- if new_center:
- (nx,ny) = new_center
- if scale:
- (sx,sy) = (scale, scale)
- cosine = math.cos(angle)
- sine = math.sin(angle)
- a = cosine/sx
- b = sine/sx
- c = x-nx*a-ny*b
- d =-sine/sy
- e = cosine/sy
- f = y-nx*d-ny*e
- return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
- # 根据所给的人脸图像,眼睛坐标位置,偏移比例,输出的大小,来进行裁剪。
- def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
- # calculate offsets in original image 计算在原始图像上的偏移。
- offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
- offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
- # get the direction 计算眼睛的方向。
- eye_direction = (eye_right[0]- eye_left[0], eye_right[1]- eye_left[1])
- # calc rotation angle in radians 计算旋转的方向弧度。
- rotation =-math.atan2(float(eye_direction[1]),float(eye_direction[0]))
- # distance between them # 计算两眼之间的距离。
- dist = Distance(eye_left, eye_right)
- # calculate the reference eye-width 计算最后输出的图像两只眼睛之间的距离。
- reference = dest_sz[0]-2.0*offset_h
- # scale factor # 计算尺度因子。
- scale =float(dist)/float(reference)
- # rotate original around the left eye # 原图像绕着左眼的坐标旋转。
- image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)
- # crop the rotated image # 剪切
- crop_xy = (eye_left[0]- scale*offset_h, eye_left[1]- scale*offset_v) # 起点
- crop_size = (dest_sz[0]*scale, dest_sz[1]*scale) # 大小
- image = image.crop((int(crop_xy[0]),int(crop_xy[1]),int(crop_xy[0]+crop_size[0]),int(crop_xy[1]+crop_size[1])))
- # resize it 重置大小
- image = image.resize(dest_sz, Image.ANTIALIAS)
- return image
- if __name__ =="__main__":
- image = Image.open("/media/crw/DataCenter/Dataset/CAS-PEAL-R1/POSE/000001/MY_000001_IEU+00_PD-22_EN_A0_D0_T0_BB_M0_R1_S0.tif")
- leftx =117
- lefty=287
- rightx=187
- righty= 288
- CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.1,0.1), dest_sz=(200,200)).save("test_10_10_200_200.jpg")
- CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.2,0.2), dest_sz=(200,200)).save("test_20_20_200_200.jpg")
- CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.3,0.3), dest_sz=(200,200)).save("test_30_30_200_200.jpg")
- CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.4,0.4), dest_sz=(200,200)).save("test_40_40_200_200.jpg")
- CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.45,0.45), dest_sz=(200,200)).save("test_45_45_200_200.jpg")
- CropFace(image, eye_left=(leftx,lefty), eye_right=(rightx,righty), offset_pct=(0.2,0.2)).save("test_20_20_70_70.jpg")
程序的结果:(图片采用Cas-Peal 数据库)
根据双眼的坐标对齐人脸Python实现相关推荐
- 三步解决C语言中struct字节对齐问题,Python进阶篇-struct字节对齐问题
Python进阶篇-struct字节对齐问题 Python进阶篇-struct字节对齐问题 Python调用C的时候,会传递一些复杂的数据结构,例如结构体,这时候就会遇到各种各样字节对齐的问题.下边所 ...
- 嘉立创EDA专业版--PCB器件坐标对齐原图纸位置坐标
PCB器件坐标对齐原图纸位置坐标 立创EDA版本说明 问题描述 问题分析 处理方案 立创EDA版本说明 本文使用嘉立创EDA专业版信息: 客户端版本 Windows 64bit V1.7.31.78b ...
- 图片坐标提取软件/图片坐标点和像素点颜色提取软件/图片坐标获取工具/Python图片坐标获取源码/图片像素坐标获取软件/python tkinter 图片显示(完全开源)
该软件使用python写的,可以提取像素点的坐标还有也能获取像素点的16进制数据RGB565和RGB888(RGB888仅最新的源码才支持),可以单点坐标也可以按键坐标,甚至可以使用简单的左右键配合使 ...
- CV之FR之MTCNN:基于TF框架利用MTCNN算法检测并对齐人脸图像进(人脸识别/人脸相似度)而得出人脸特征向量从而计算两张人脸图片距离案例应用之详细攻略
CV之FR之MTCNN:基于TF框架利用MTCNN算法检测并对齐人脸图像进(人脸识别/人脸相似度)而得出人脸特征向量从而计算两张人脸图片距离案例应用之详细攻略 目录 基于TF框架利用MTCNN算法检测 ...
- opencv画框返回坐标 python_20行Python代码实现视频字符化
来源 | ZackSock(ID:ZackSock) 我们经常在B站上看到一些字符鬼畜视频,主要就是将一个视频转换成字符的样子展现出来. 看起来是非常高端,但是实际实现起来确实非常简单,我们只需要接触 ...
- 经纬度坐标转换xy坐标 python_在Python中使用NewtonRaphson迭代将经纬度转换为xy Mollweide地图坐标...
我试图编写一个程序,从用户那里获取一组经度和纬度坐标,将它们转换为Mollweide投影图的x&y坐标,然后报告这些坐标处的像素值(在本例中,是噪声温度).在 我使用的地图/数据是Haslam ...
- python 坐标连线_从具有和角度的坐标绘制线 - python
我基本上想从给定角度的坐标(x,y)绘制一条线(计算切线值). 用这样的简单代码行pl.plot([x1, x2], [y1, y2], 'k-', lw=1)可以在两点之间绘制一条线,但是为此我需要 ...
- lisp提取长方形坐标_用 Python 对图片主体轮廓进行提取、颜色标记、并计算区域面积...
Python + Opencv2 实现轮廓提取,轮廓区域面积计算: 对图像处理时,会遇到这样一个场景:找到图像主体轮廓,这是其一,可能为了凸显轮廓,需要用指定的颜色进行标记:轮廓标记完可能任务还没 ...
- python怎么输出坐标_使用Python实现图像标记点的坐标输出功能
Sometimes we have need to interact with an application,for example by marking points in an image,or ...
最新文章
- MySQL/ACCESS导出一句话拿WebShell后门命令
- 指纹、虹膜和人脸之后,身份认证领域的新热点:手背静脉识别
- 【机器视觉】 HDevelop语言基础(二)-变量和表达式
- 使用vue控制元素显示隐藏
- springboot 之 webscoket 服务端推送
- php绕过验证,PHP-Nuke绕过识别码验证漏洞
- 乐视网回击贾跃亭:债务处理没有进展,先拿出57亿再说
- DPDK报文收发 run to completion, pipeline
- 16、React系列之 React 路由
- java寻路算法_具有指定距离/节点数的寻路算法
- PE学习.动手写PE.见缝插针
- Linux下更换jdk和配置环境变量
- 模块参考资料-硬件资料-Air720UG/UH-功耗指标
- python群发邮件 不进垃圾箱_邮件群发不进垃圾箱
- Mockito 中被 Mocked 的对象属性及方法的默认值
- 广发证券基于分布式架构的新一代估值系统实践
- 苹果设备的微信数据收集
- web常见的 HTTP 5xx 状态汇总
- 琴生不等式(Jensen Inequality)
- Windows 8系统IE10无法安装Flash Player插件的解决办法
热门文章
- linux单体内核,加载Linux单体内核的笔记
- Math.round(11.5)等于多少? Math.round(-11.5)等于多少?
- 让数值自增_第03期:列非空与自增
- 内江将被打造成四川物联网西部副中心
- 2017,公司必须换掉的六种人,别心软!
- **Git本地仓库图解
- 别把你的目光停留在周围
- Lazy Line Painter – 很有趣的 jQuery 路径动画插件
- 【STM32 .Net MF开发板学习-17】Wifi遥控智能小车
- IE6动态插入option