裁剪成标准2寸照片
This commit is contained in:
parent
3c67358c5a
commit
9df44684f3
@ -33,3 +33,12 @@
|
|||||||
文档:[利用PyMatting替换成证件照背景](https://github.com/itainf/aiphoto/wiki/%E5%88%A9%E7%94%A8PyMatting%E7%B2%BE%E7%BB%86%E5%8C%96%E6%8A%A0%E5%9B%BE)
|
文档:[利用PyMatting替换成证件照背景](https://github.com/itainf/aiphoto/wiki/%E5%88%A9%E7%94%A8PyMatting%E7%B2%BE%E7%BB%86%E5%8C%96%E6%8A%A0%E5%9B%BE)
|
||||||
|
|
||||||
|
|
||||||
|
2020年7月15日更新
|
||||||
|
|
||||||
|
本次更新版本: v20200715
|
||||||
|
|
||||||
|
本次更新了,通过dlib框架,裁剪成标准尺寸照片
|
||||||
|
|
||||||
|
文档:[裁剪照片](https://github.com/itainf/aiphoto/wiki/%E8%A3%81%E5%89%AA%E7%85%A7%E7%89%87)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
img/2in.jpg
Normal file
BIN
img/2in.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
BIN
img/meinv_id.png
Normal file
BIN
img/meinv_id.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 508 KiB |
BIN
img/meinv_id_landmarks.png
Normal file
BIN
img/meinv_id_landmarks.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 576 KiB |
22
m_dlib/ai_crop.py
Normal file
22
m_dlib/ai_crop.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import m_dlib.face_marks as fmarks
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
def crop_photo(path, target):
|
||||||
|
path = path
|
||||||
|
shape, d = fmarks.predictor_face(path)
|
||||||
|
|
||||||
|
WIDTH_2IN = 413/2
|
||||||
|
HEIGHT_2IN = 626/2
|
||||||
|
|
||||||
|
# 人像中心点
|
||||||
|
X_CENTRE = d.left()+(d.right()-d.left()) / 2
|
||||||
|
Y_CENTER = d.top()+(d.bottom()-d.top()) / 2
|
||||||
|
|
||||||
|
im = Image.open(path)
|
||||||
|
im = im.crop((X_CENTRE-WIDTH_2IN, Y_CENTER-HEIGHT_2IN, X_CENTRE+WIDTH_2IN, Y_CENTER+HEIGHT_2IN))
|
||||||
|
im.save(target)
|
||||||
|
|
||||||
|
|
||||||
|
# 通过识别人脸关键点,裁剪图像
|
||||||
|
# crop_photo("..//img//meinv_id.png","..//img//2in.jpg")
|
||||||
53
m_dlib/face_marks.py
Normal file
53
m_dlib/face_marks.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import dlib
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
|
||||||
|
def predictor_face(path):
|
||||||
|
"""
|
||||||
|
:param path: 原始图片路径
|
||||||
|
:return shape 关键点
|
||||||
|
"""
|
||||||
|
# 模型路径 --》修改成自己的路径
|
||||||
|
predictor_path = "E:\\python\\az\\dlib-19.19.0.tar\\shape_predictor_68_face_landmarks.dat"
|
||||||
|
|
||||||
|
# 获取一个探测器
|
||||||
|
detector = dlib.get_frontal_face_detector()
|
||||||
|
# 获取一个预测模型
|
||||||
|
predictor = dlib.shape_predictor(predictor_path)
|
||||||
|
# 加载图片
|
||||||
|
img = dlib.load_rgb_image(path)
|
||||||
|
|
||||||
|
# 探测到的人脸
|
||||||
|
dets = detector(img, 1)
|
||||||
|
print("Number of faces detected: {}".format(len(dets)))
|
||||||
|
# 取一个人脸
|
||||||
|
d = dets[0]
|
||||||
|
|
||||||
|
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(0, d.left(), d.top(), d.right(), d.bottom()))
|
||||||
|
|
||||||
|
# Get the landmarks/parts for the face in box d.
|
||||||
|
shape = predictor(img, d)
|
||||||
|
|
||||||
|
print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
|
||||||
|
return shape, d
|
||||||
|
|
||||||
|
# 显示人脸关键点
|
||||||
|
def test_landmarks(path,target_path):
|
||||||
|
path = path
|
||||||
|
img = cv2.imread(path)
|
||||||
|
font=cv2.FONT_HERSHEY_SIMPLEX
|
||||||
|
i = 0
|
||||||
|
shape, d = predictor_face(path)
|
||||||
|
for pt in shape.parts():
|
||||||
|
i = i+1
|
||||||
|
pt_pos = (pt.x, pt.y)
|
||||||
|
cv2.putText(img, str(i), pt_pos, font, 0.3, (0, 255, 0))
|
||||||
|
|
||||||
|
cv2.imshow("image", img)
|
||||||
|
cv2.imwrite(target_path,img)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
|
||||||
|
# 打印人脸特征点
|
||||||
|
# test_landmarks("..//img//meinv_id.png","..//img//meinv_id_landmarks.png")
|
||||||
15
main.py
15
main.py
@ -2,6 +2,8 @@
|
|||||||
from u_2_net import my_u2net_test
|
from u_2_net import my_u2net_test
|
||||||
from to_background import to_background
|
from to_background import to_background
|
||||||
from to_background import to_standard_trimap
|
from to_background import to_standard_trimap
|
||||||
|
from m_dlib import ai_crop
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -16,10 +18,15 @@ if __name__ == "__main__":
|
|||||||
trimap = "..\\aiphoto\\img\\meinv_trimap_resize.png"
|
trimap = "..\\aiphoto\\img\\meinv_trimap_resize.png"
|
||||||
to_standard_trimap.to_standard_trimap(alpha_resize_img, trimap)
|
to_standard_trimap.to_standard_trimap(alpha_resize_img, trimap)
|
||||||
#
|
#
|
||||||
# # 证件照添加纯色背景
|
# 证件照添加蓝底纯色背景
|
||||||
id_image = "..\\aiphoto\\img\\meinv_id_grid.png"
|
id_image = "..\\aiphoto\\img\\meinv_id.png"
|
||||||
# to_background.to_background(org_img, trimap, id_image, "blue")
|
to_background.to_background(org_img, trimap, id_image, "blue")
|
||||||
to_background.to_background_grid(org_img, trimap, id_image)
|
#id_image = "..\\aiphoto\\img\\meinv_id_grid.png"
|
||||||
|
#to_background.to_background_grid(org_img, trimap, id_image)
|
||||||
# image = Image.open(id_image)
|
# image = Image.open(id_image)
|
||||||
# data = image.getdata()
|
# data = image.getdata()
|
||||||
# np.savetxt("data6.txt", data,fmt='%d',delimiter=',')
|
# np.savetxt("data6.txt", data,fmt='%d',delimiter=',')
|
||||||
|
|
||||||
|
# 20200719
|
||||||
|
# 通过识别人脸关键点,裁剪图像
|
||||||
|
ai_crop.crop_photo("..\\aiphoto\\img\\meinv_id.png", "..\\aiphoto\\img\\2in.jpg")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user