diff --git a/README.md b/README.md index 934c944..58d9486 100644 --- a/README.md +++ b/README.md @@ -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) +2020年7月15日更新 + +本次更新版本: v20200715 + +本次更新了,通过dlib框架,裁剪成标准尺寸照片 + +文档:[裁剪照片](https://github.com/itainf/aiphoto/wiki/%E8%A3%81%E5%89%AA%E7%85%A7%E7%89%87) + + diff --git a/img/2in.jpg b/img/2in.jpg new file mode 100644 index 0000000..49c68ff Binary files /dev/null and b/img/2in.jpg differ diff --git a/img/meinv_id.png b/img/meinv_id.png new file mode 100644 index 0000000..bc05a14 Binary files /dev/null and b/img/meinv_id.png differ diff --git a/img/meinv_id_landmarks.png b/img/meinv_id_landmarks.png new file mode 100644 index 0000000..76c07f0 Binary files /dev/null and b/img/meinv_id_landmarks.png differ diff --git a/m_dlib/ai_crop.py b/m_dlib/ai_crop.py new file mode 100644 index 0000000..1232902 --- /dev/null +++ b/m_dlib/ai_crop.py @@ -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") diff --git a/m_dlib/face_marks.py b/m_dlib/face_marks.py new file mode 100644 index 0000000..879a010 --- /dev/null +++ b/m_dlib/face_marks.py @@ -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") diff --git a/main.py b/main.py index 28a71a1..c103cfa 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ from u_2_net import my_u2net_test from to_background import to_background from to_background import to_standard_trimap +from m_dlib import ai_crop + import numpy as np from PIL import Image if __name__ == "__main__": @@ -16,10 +18,15 @@ if __name__ == "__main__": trimap = "..\\aiphoto\\img\\meinv_trimap_resize.png" to_standard_trimap.to_standard_trimap(alpha_resize_img, trimap) # - # # 证件照添加纯色背景 - id_image = "..\\aiphoto\\img\\meinv_id_grid.png" - # 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.png" + to_background.to_background(org_img, trimap, id_image, "blue") + #id_image = "..\\aiphoto\\img\\meinv_id_grid.png" + #to_background.to_background_grid(org_img, trimap, id_image) # image = Image.open(id_image) # data = image.getdata() # np.savetxt("data6.txt", data,fmt='%d',delimiter=',') + + # 20200719 + # 通过识别人脸关键点,裁剪图像 + ai_crop.crop_photo("..\\aiphoto\\img\\meinv_id.png", "..\\aiphoto\\img\\2in.jpg")