好機會,我用Python給女同事頭像戴上口罩!
突如其來的新型冠狀病毒讓全國人民春節(jié)過的誠惶誠恐,出門必須帶口罩已經(jīng)達(dá)成共識。
圖片來自 Pexels
疫情防控期間,我們大家要做的就是遵從政府號令:“少出門,戴口罩、勤洗手......”,乖乖宅家就是為抗擊疫情做貢獻(xiàn)。
看到一些朋友叫設(shè)計同學(xué)幫忙給自己的頭像戴上口罩,作為技術(shù)人,心想一定還有更多人有這樣的訴求,不如開發(fā)一個簡單的程序來實現(xiàn)這個需求,也算是幫助設(shè)計小姐姐減少工作量。
于是我花了些時間,寫了一個叫做 face-mask[1] 的命令行工具,能夠輕松的給圖片中的人像戴上口罩,而且口罩的方向和大小都是適應(yīng)人臉的哦~
使用
①安裝 face-mask
- pip install face-mask
確保 Python 版本在 3.6 及以上。
②使用 face-mask
直接指定圖片路徑即可為圖片中的人像戴上口罩,并會生成一個新的圖片(額外有 -with-mask 后綴):
- face-mask /path/to/face/picture
通過指定 --show 選項,還可以使用默認(rèn)圖片查看器打開新生成的圖片:
- face-mask /path/to/face/picture --show
③效果
給一個人戴上口罩,效果如下圖:
給多個人戴上口罩,效果如下圖:
給動漫人物戴上口罩:
實現(xiàn)
思路
要想實現(xiàn)上面的效果,我們應(yīng)該怎么做?不妨這么想:
- 首先是識別出人的鼻子(nose_bridge)和臉輪廓(chin)。
- 通過臉輪廓確定出臉左點(chin_left_point)、臉底點(chin_bottom_point)和臉右點(chin_right_point)。
- 由鼻子和臉底點確定口罩大小的高度、中心線。
- 將口罩左右平均分為兩個部分,調(diào)整左口罩大小,寬度為臉左點到中心線的距離;調(diào)整右口罩大小,寬度為臉右點到中心線的距離;合并左右口罩為新口罩。
- 旋轉(zhuǎn)新口罩,角度為中心線相對于 y 軸的旋轉(zhuǎn)角。
- 將新口罩放在原圖適當(dāng)位置。
關(guān)于人臉識別,可以使用 face_recognition[2] 庫進(jìn)行識別。關(guān)于圖像處理,可以使用 Pillow[3] 庫進(jìn)行處理。
代碼
有了思路之后,實現(xiàn)就是件相對輕松的事情。不過對庫的熟悉和圖片的變換計算可能要花些時間。
詳細(xì)的代碼請參考如下鏈接,這里僅說明下最核心的步驟:
https://github.com/Prodesire/face-mask
人臉識別:
- import face_recognition
- face_image_np = face_recognition.load_image_file('/path/to/face/picture')
- face_landmarks = face_recognition.face_landmarks(face_image_np)
借助 face_recognition 庫可以輕松的識別出人像,最終得到的 face_landmarks 是一個列表,里面的每個 face_landmark 都表示一個人像數(shù)據(jù)。
face_landmark 是一個字典,其中的鍵表示人像特征,值表示該特征的點的列表。比如:
- 鍵 nose_bridge 表示鼻梁
- 鍵 chin 表示臉頰
我們需要根據(jù)每個 face_landmark,給對應(yīng)的頭像戴上口罩。
獲得鼻子和臉頰的特征點:
- 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)
上述代碼主要做了如下內(nèi)容:
- 將口罩左右平均分為兩個部分。
- 調(diào)整左口罩大小,寬度為臉左點到中心線的距離*寬度系數(shù) 1.2。
- 調(diào)整右口罩大小,寬度為臉右點到中心線的距離*寬度系數(shù) 1.2。
- 合并左右口罩為新口罩。
get_distance_from_point_to_line 用來獲取一個點到一條線的距離,具體實現(xiàn)可看源代碼。
width_ratio 是寬度系數(shù),用來適當(dāng)擴大口罩。原因我們是根據(jù)臉頰的寬度計算口罩的寬度,但口罩是待在耳朵上的,真實寬度應(yīng)該要更寬。
旋轉(zhuǎn)口罩、并放到原圖適當(dāng)位置:
- # 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)
上述代碼主要做了如下內(nèi)容:
- 旋轉(zhuǎn)新口罩,角度為中心線相對于 y 軸的旋轉(zhuǎn)角。
- 計算口罩應(yīng)該放置的坐標(biāo)。
- 將新口罩放在原圖的計算出的坐標(biāo)下。
最后就是將新圖片保存到本地路徑,代碼不再展示。
總結(jié)
我們借助 face_recognition 庫可以輕松的識別出人像,然后根據(jù)臉頰的寬度和鼻梁位置計算出口罩的大小、方向和位置,并最終生成出戴上口罩的圖片。
整個過程并不復(fù)雜,但在坐標(biāo)計算上要格外小心,如此,我們便打造了一個短小精悍的“自動戴上口罩”程序!