2019 年底開始蔓延的新型肺炎疫情牽動人心,作為個體,我們力所能及的就是儘量待在家中少出門。
看到一些朋友叫設計同學幫忙給自己的頭像戴上口罩,作為技術人,心想一定還有更多人有這樣的訴求,不如開發一個簡單的程序來實現這個需求,也算是幫助設計姐姐減少工作量。
於是花了些時間,寫了一個叫做 face-mask[1] 的命令行工具,能夠輕鬆的給圖片中的人像戴上口罩,而且口罩的方向和大小都是適應人臉的哦~
使用
安裝 face-mask確保 Python 版本在 3.6 及以上
使用 face-mask直接指定圖片路徑即可為圖片中的人像戴上口罩,並會生成一個新的圖片(額外有 -with-mask 後綴):
face-mask /path/to/face/picture
通過指定 --show 選項,還可以使用默認圖片查看器打開新生成的圖片:face-mask /path/to/face/picture --show
效果給一個人戴上口罩
給多個人戴上口罩
給動漫人物戴上口罩
實現思路要想實現上面的效果,我們應該怎麼做?不妨這麼想:
關於人臉識別,可以使用 face_recognition[2] 庫進行識別。
關於圖像處理,可以使用 Pillow[3] 庫進行處理。
代碼有了思路之後,實現就是件相對輕鬆的事情。不過對庫的熟悉和圖片的變換計算可能要花些時間。
詳細的代碼請閱讀 face-mask[4]。這裡僅說明下最核心的步驟。
人臉識別import face_recognition
藉助 face_recognition 庫可以輕鬆的識別出人像,最終得到的 face_landmarks 是一個列表,裡面的每個 face_landmark 都表示一個人像數據。
face_image_np = face_recognition.load_image_file('/path/to/face/picture')
face_landmarks = face_recognition.face_landmarks(face_image_np)face_landmark 是一個字典,其中的鍵表示人像特徵,值表示該特徵的點的列表。比如:
鍵 nose_bridge 表示鼻梁
鍵 chin 表示臉頰
我們需要根據每個 face_landmark,給對應的頭像戴上口罩。
獲得鼻子和臉頰的特徵點import numpy as np
nose_bridge = face_landmark['nose_bridge']
nose_point = nose_bridge[len(nose_bridge) * 1 // 4]
nose_v = np.array(nose_point)
chin = face_landmark['chin']
chin_len = len(chin)
chin_bottom_point = chin[chin_len // 2]
chin_bottom_v = np.array(chin_bottom_point)
chin_left_point = chin[chin_len // 8]
chin_right_point = chin[chin_len * 7 // 8]表示上鼻梁的一個點 nose_point
表示臉左點 chin_left_point
表示臉右點 chin_right_point
表示臉底點 chin_bottom_point
拆分、縮放和合併口罩from PIL import Image
_face_img = Image.fromarray(face_image_np)
_mask_img = Image.open('/path/to/mask/picture')
# split mask and resize
width = _mask_img.width
height = _mask_img.height
width_ratio = 1.2
new_height = int(np.linalg.norm(nose_v - chin_bottom_v))
# left
mask_left_img = _mask_img.crop((0, 0, width // 2, height))
mask_left_width = get_distance_from_point_to_line(chin_left_point, nose_point, chin_bottom_point)
mask_left_width = int(mask_left_width * width_ratio)
mask_left_img = mask_left_img.resize((mask_left_width, new_height))
# right
mask_right_img = _mask_img.crop((width // 2, 0, width, height))
mask_right_width = get_distance_from_point_to_line(chin_right_point, nose_point, chin_bottom_point)
mask_right_width = int(mask_right_width * width_ratio)
mask_right_img = mask_right_img.resize((mask_right_width, new_height))
# merge mask
size = (mask_left_img.width + mask_right_img.width, new_height)
mask_img = Image.new('RGBA', size)
mask_img.paste(mask_left_img, (0, 0), mask_left_img)
mask_img.paste(mask_right_img, (mask_left_img.width, 0), mask_right_img)get_distance_from_point_to_line 用來獲取一個點到一條線的距離,具體實現可看原始碼。
width_ratio 是寬度係數,用來適當擴大口罩。原因我們是根據臉頰的寬度計算口罩的寬度,但口罩是待在耳朵上的,真實寬度應該要更寬。
旋轉口罩、並放到原圖適當位置# rotate mask
angle = np.arctan2(chin_bottom_point[1] - nose_point[1], chin_bottom_point[0] - nose_point[0])
rotated_mask_img = mask_img.rotate(angle, expand=True)
# calculate mask location
center_x = (nose_point[0] + chin_bottom_point[0]) // 2
center_y = (nose_point[1] + chin_bottom_point[1]) // 2
offset = mask_img.width // 2 - mask_left_img.width
radian = angle * np.pi / 180
box_x = center_x + int(offset * np.cos(radian)) - rotated_mask_img.width // 2
box_y = center_y + int(offset * np.sin(radian)) - rotated_mask_img.height // 2
# add mask
_face_img.paste(mask_img, (box_x, box_y), mask_img)旋轉新口罩,角度為中心線相對於 y 軸的旋轉角
計算口罩應該放置的坐標
將新口罩放在原圖的計算出的坐標下
最後就是將新圖片保存到本地路徑,代碼不再展示。
總結我們藉助 face_recognition 庫可以輕鬆的識別出人像,然後根據臉頰的寬度和鼻梁位置計算出口罩的大小、方向和位置,並最終生成出戴上口罩的圖片。整個過程並不複雜,但在坐標計算上要格外小心,如此,我們便打造了一個短小精悍的「自動戴上口罩」程序!
參考資料:[1]face-mask: https://github.com/Prodesire/face-mask[2]face_recognition: https://github.com/ageitgey/face_recognition[3]Pillow: https://pillow.readthedocs.io/[4]face-mask: https://github.com/Prodesire/face-mask更多精彩
在公眾號後臺對話框輸入以下關鍵詞
查看更多優質內容!
女朋友 | 大數據 | 運維 | 書單 | 算法
大數據 | JavaScript | Python | 黑客
AI | 人工智慧 | 5G | 區塊鏈
機器學習 | 數學 | 送書