|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +@author: 闲欢 |
| 5 | +""" |
| 6 | + |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import cv2 |
| 10 | +import dlib |
| 11 | +import PySimpleGUI as sg |
| 12 | +import os.path |
| 13 | + |
| 14 | + |
| 15 | +# 给img中的人头像加上圣诞帽,人脸最好为正脸 |
| 16 | +def add_hat(imgPath): |
| 17 | + # 读取头像图 |
| 18 | + img = cv2.imread(imgPath) |
| 19 | + #print(img) |
| 20 | + # 读取帽子图,第二个参数-1表示读取为rgba通道,否则为rgb通道 |
| 21 | + hat_img = cv2.imread("hat2.png",-1) |
| 22 | + |
| 23 | + # 分离rgba通道,合成rgb三通道帽子图,a通道后面做mask用 |
| 24 | + r,g,b,a = cv2.split(hat_img) |
| 25 | + rgb_hat = cv2.merge((r,g,b)) |
| 26 | + |
| 27 | + cv2.imwrite("hat_alpha.jpg",a) |
| 28 | + |
| 29 | + # dlib人脸关键点检测器 |
| 30 | + predictor_path = "shape_predictor_5_face_landmarks.dat" |
| 31 | + predictor = dlib.shape_predictor(predictor_path) |
| 32 | + |
| 33 | + # dlib正脸检测器 |
| 34 | + detector = dlib.get_frontal_face_detector() |
| 35 | + |
| 36 | + # 正脸检测 |
| 37 | + dets = detector(img, 1) |
| 38 | + |
| 39 | + # 如果检测到人脸 |
| 40 | + if len(dets)>0: |
| 41 | + for d in dets: |
| 42 | + x,y,w,h = d.left(),d.top(), d.right()-d.left(), d.bottom()-d.top() |
| 43 | + |
| 44 | + # 关键点检测,5个关键点 |
| 45 | + shape = predictor(img, d) |
| 46 | + |
| 47 | + # 选取左右眼眼角的点 |
| 48 | + point1 = shape.part(0) |
| 49 | + point2 = shape.part(2) |
| 50 | + |
| 51 | + # 求两点中心 |
| 52 | + eyes_center = ((point1.x+point2.x)//2,(point1.y+point2.y)//2) |
| 53 | + |
| 54 | + # 根据人脸大小调整帽子大小 |
| 55 | + factor = 1.5 |
| 56 | + resized_hat_h = int(round(rgb_hat.shape[0]*w/rgb_hat.shape[1]*factor)) |
| 57 | + resized_hat_w = int(round(rgb_hat.shape[1]*w/rgb_hat.shape[1]*factor)) |
| 58 | + |
| 59 | + if resized_hat_h > y: |
| 60 | + resized_hat_h = y-1 |
| 61 | + |
| 62 | + # 根据人脸大小调整帽子大小 |
| 63 | + resized_hat = cv2.resize(rgb_hat,(resized_hat_w,resized_hat_h)) |
| 64 | + |
| 65 | + # 用alpha通道作为mask |
| 66 | + mask = cv2.resize(a,(resized_hat_w,resized_hat_h)) |
| 67 | + mask_inv = cv2.bitwise_not(mask) |
| 68 | + |
| 69 | + # 帽子相对与人脸框上线的偏移量 |
| 70 | + dh = 0 |
| 71 | + dw = 0 |
| 72 | + # 原图ROI |
| 73 | + # bg_roi = img[y+dh-resized_hat_h:y+dh, x+dw:x+dw+resized_hat_w] |
| 74 | + bg_roi = img[y+dh-resized_hat_h:y+dh,(eyes_center[0]-resized_hat_w//3):(eyes_center[0]+resized_hat_w//3*2)] |
| 75 | + |
| 76 | + # 原图ROI中提取放帽子的区域 |
| 77 | + bg_roi = bg_roi.astype(float) |
| 78 | + mask_inv = cv2.merge((mask_inv,mask_inv,mask_inv)) |
| 79 | + alpha = mask_inv.astype(float)/255 |
| 80 | + |
| 81 | + # 相乘之前保证两者大小一致(可能会由于四舍五入原因不一致) |
| 82 | + alpha = cv2.resize(alpha,(bg_roi.shape[1],bg_roi.shape[0])) |
| 83 | + # print("alpha size: ",alpha.shape) |
| 84 | + # print("bg_roi size: ",bg_roi.shape) |
| 85 | + bg = cv2.multiply(alpha, bg_roi) |
| 86 | + bg = bg.astype('uint8') |
| 87 | + |
| 88 | + cv2.imwrite("bg.jpg",bg) |
| 89 | + # cv2.imshow("image",img) |
| 90 | + # cv2.waitKey() |
| 91 | + |
| 92 | + # 提取帽子区域 |
| 93 | + hat = cv2.bitwise_and(resized_hat,resized_hat,mask = mask) |
| 94 | + cv2.imwrite("hat.jpg",hat) |
| 95 | + |
| 96 | + # cv2.imshow("hat",hat) |
| 97 | + # cv2.imshow("bg",bg) |
| 98 | + |
| 99 | + # print("bg size: ",bg.shape) |
| 100 | + # print("hat size: ",hat.shape) |
| 101 | + |
| 102 | + # 相加之前保证两者大小一致(可能会由于四舍五入原因不一致) |
| 103 | + hat = cv2.resize(hat,(bg_roi.shape[1],bg_roi.shape[0])) |
| 104 | + # 两个ROI区域相加 |
| 105 | + add_hat = cv2.add(bg,hat) |
| 106 | + # cv2.imshow("add_hat",add_hat) |
| 107 | + |
| 108 | + # 把添加好帽子的区域放回原图 |
| 109 | + img[y+dh-resized_hat_h:y+dh,(eyes_center[0]-resized_hat_w//3):(eyes_center[0]+resized_hat_w//3*2)] = add_hat |
| 110 | + |
| 111 | + # 展示效果 |
| 112 | + # cv2.imshow("img",img ) |
| 113 | + # cv2.waitKey(0) |
| 114 | + |
| 115 | + return img |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +file_list_column = [ |
| 124 | + [sg.Submit('生成', key='Go',size=(15, 1)), sg.Cancel('退出',key='Cancel', size=(15, 1))], |
| 125 | + [ |
| 126 | + sg.Text("图片位置(选择文件夹)"), |
| 127 | + sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"), |
| 128 | + sg.FolderBrowse('浏览'), |
| 129 | + ], |
| 130 | + [ |
| 131 | + sg.Listbox( |
| 132 | + values=[], enable_events=True, size=(40, 20), key="-FILE LIST-" |
| 133 | + ) |
| 134 | + ] |
| 135 | +] |
| 136 | +image_viewer_column = [ |
| 137 | + [sg.Text("从左边图片列表中选择一张图片:")], |
| 138 | + [sg.Image(key="-IMAGE-")] |
| 139 | +] |
| 140 | +layout = [ |
| 141 | + [ |
| 142 | + sg.Column(file_list_column), |
| 143 | + sg.VSeperator(), |
| 144 | + sg.Column(image_viewer_column), |
| 145 | + ] |
| 146 | +] |
| 147 | +window = sg.Window("人像添加圣诞帽软件", layout) |
| 148 | +filename = '' |
| 149 | +while True: |
| 150 | + event, values = window.read() |
| 151 | + if event == "Cancel" or event == sg.WIN_CLOSED: |
| 152 | + break |
| 153 | + if event == "-FOLDER-": |
| 154 | + folder = values["-FOLDER-"] |
| 155 | + try: |
| 156 | + file_list = os.listdir(folder) |
| 157 | + except: |
| 158 | + file_list = [] |
| 159 | + fnames = [ |
| 160 | + f |
| 161 | + for f in file_list |
| 162 | + if os.path.isfile(os.path.join(folder, f)) |
| 163 | + and f.lower().endswith((".jpg", ".png")) |
| 164 | + ] |
| 165 | + window["-FILE LIST-"].update(fnames) |
| 166 | + elif event == "-FILE LIST-": |
| 167 | + try: |
| 168 | + filename = os.path.join(values["-FOLDER-"], values["-FILE LIST-"][0]) |
| 169 | + if filename.endswith('.jpg'): |
| 170 | + im = cv2.imread(filename) |
| 171 | + cv2.imwrite(filename.replace('jpg', 'png'), im) |
| 172 | + window["-IMAGE-"].update(filename=filename.replace('jpg', 'png')) |
| 173 | + except Exception as e: |
| 174 | + print(e) |
| 175 | + elif event== "Go" : |
| 176 | + try: |
| 177 | + output = add_hat(filename) |
| 178 | + #print(output) |
| 179 | + # 展示效果 |
| 180 | + #cv2.imshow("output",output) |
| 181 | + #cv2.waitKey(0) |
| 182 | + print(os.path.join(folder, "output.png")) |
| 183 | + cv2.imwrite(os.path.join(folder, "output.png"),output) |
| 184 | + #print(output) |
| 185 | + window["-IMAGE-"].update(filename=os.path.join(folder, "output.png")) |
| 186 | + except: |
| 187 | + print('OMG!添加失败了!') |
| 188 | + |
| 189 | + cv2.destroyAllWindows() |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
0 commit comments