根据双眼的坐标对齐人脸Python实现

最近要做一个人脸识别的东西,可数据库的人脸图像还没有做对齐处理。所以需要自己做人脸对齐预处理。网上查了,发现已经有人做过了类似的脚本。直接拿过来用了,原地址在这:https://github.com/bytefish/facerec/blob/master/py/apps/scripts/crop_face.py   自己为了以后更改,加了一些注释。


[python]  view plain  copy
  1. # -*- coding: utf-8 -*-  
  2. """ 
  3. Created on Thu Jan  1 16:09:32 2015 
  4.  
  5. @author: crw 
  6. """  
  7. # 参数含义:  
  8. #  CropFace(image, eye_left, eye_right, offset_pct, dest_sz)  
  9. # eye_left is the position of the left eye  
  10. # eye_right is the position of the right eye  
  11. # 比例的含义为:要保留的图像靠近眼镜的百分比,  
  12. # offset_pct is the percent of the image you want to keep next to the eyes (horizontal, vertical direction)  
  13. # 最后保留的图像的大小。  
  14. # dest_sz is the size of the output image  
  15. #  
  16. import sys,math,Image  
  17.    
  18.  # 计算两个坐标的距离  
  19. def Distance(p1,p2):  
  20.       dx = p2[0]- p1[0]  
  21.       dy = p2[1]- p1[1]  
  22.       return math.sqrt(dx*dx+dy*dy)  
  23.    
  24.  # 根据参数,求仿射变换矩阵和变换后的图像。  
  25. def ScaleRotateTranslate(image, angle, center =None, new_center =None, scale =None, resample=Image.BICUBIC):  
  26.       if (scale is None)and (center is None):  
  27.             return image.rotate(angle=angle, resample=resample)  
  28.       nx,ny = x,y = center  
  29.       sx=sy=1.0  
  30.       if new_center:  
  31.             (nx,ny) = new_center  
  32.       if scale:  
  33.             (sx,sy) = (scale, scale)  
  34.       cosine = math.cos(angle)  
  35.       sine = math.sin(angle)  
  36.       a = cosine/sx  
  37.       b = sine/sx  
  38.       c = x-nx*a-ny*b  
  39.       d =-sine/sy  
  40.       e = cosine/sy  
  41.       f = y-nx*d-ny*e  
  42.       return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)  
  43.  # 根据所给的人脸图像,眼睛坐标位置,偏移比例,输出的大小,来进行裁剪。  
  44. def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):  
  45.       # calculate offsets in original image 计算在原始图像上的偏移。  
  46.       offset_h = math.floor(float(offset_pct[0])*dest_sz[0])  
  47.       offset_v = math.floor(float(offset_pct[1])*dest_sz[1])  
  48.       # get the direction  计算眼睛的方向。  
  49.       eye_direction = (eye_right[0]- eye_left[0], eye_right[1]- eye_left[1])  
  50.       # calc rotation angle in radians  计算旋转的方向弧度。  
  51.       rotation =-math.atan2(float(eye_direction[1]),float(eye_direction[0]))  
  52.       # distance between them  # 计算两眼之间的距离。  
  53.       dist = Distance(eye_left, eye_right)  
  54.       # calculate the reference eye-width    计算最后输出的图像两只眼睛之间的距离。  
  55.       reference = dest_sz[0]-2.0*offset_h  
  56.       # scale factor   # 计算尺度因子。  
  57.       scale =float(dist)/float(reference)  
  58.       # rotate original around the left eye  # 原图像绕着左眼的坐标旋转。  
  59.       image = ScaleRotateTranslate(image, center=eye_left, angle=rotation)  
  60.       # crop the rotated image  # 剪切  
  61.       crop_xy = (eye_left[0]- scale*offset_h, eye_left[1]- scale*offset_v)  # 起点  
  62.       crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)   # 大小  
  63.       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])))  
  64.       # resize it 重置大小  
  65.       image = image.resize(dest_sz, Image.ANTIALIAS)  
  66.       return image  
  67.     
  68. if __name__ =="__main__":  
  69.       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")  
  70.       leftx =117  
  71.       lefty=287  
  72.       rightx=187  
  73.       righty= 288  
  74.       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")  
  75.       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")  
  76.       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")  
  77.       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")  
  78.       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")  
  79.       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 数据库)






 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值