好機會,我用Python給女同事頭像戴上口罩

突如其來的新型冠狀病毒讓全國人民春節過的誠惶誠恐,出門必須帶口罩已經達成共識。

疫情防控期間,我們大家要做的就是遵從政府號令:“少出門,戴口罩、勤洗手......”,乖乖宅家就是為抗擊疫情做貢獻。

看到一些朋友叫設計同學幫忙給自己的頭像戴上口罩,作為技術人,心想一定還有更多人有這樣的訴求,不如開發一個簡單的程序來實現這個需求,也算是幫助設計小姐姐減少工作量。

好機會,我用Python給女同事頭像戴上口罩

圖片來自 Pexels

於是我花了些時間,寫了一個叫做 face-mask[1] 的命令行工具,能夠輕鬆的給圖片中的人像戴上口罩,而且口罩的方向和大小都是適應人臉的哦~

使用

①安裝 face-mask

<code>pip install face-mask /<code>

確保 Python 版本在 3.6 及以上。

②使用 face-mask

直接指定圖片路徑即可為圖片中的人像戴上口罩,並會生成一個新的圖片(額外有 -with-mask 後綴):

<code>face-mask /path/to/face/picture /<code>

通過指定 --show 選項,還可以使用默認圖片查看器打開新生成的圖片:

<code>face-mask /path/to/face/picture --show /<code>

③效果

給一個人戴上口罩,效果如下圖:

好機會,我用Python給女同事頭像戴上口罩

給多個人戴上口罩,效果如下圖:

好機會,我用Python給女同事頭像戴上口罩

給動漫人物戴上口罩:

好機會,我用Python給女同事頭像戴上口罩

實現

思路

要想實現上面的效果,我們應該怎麼做?不妨這麼想:

  • 首先是識別出人的鼻子(nose_bridge)和臉輪廓(chin)。
  • 通過臉輪廓確定出臉左點(chin_left_point)、臉底點(chin_bottom_point)和臉右點(chin_right_point)。
  • 由鼻子和臉底點確定口罩大小的高度、中心線。
  • 將口罩左右平均分為兩個部分,調整左口罩大小,寬度為臉左點到中心線的距離;調整右口罩大小,寬度為臉右點到中心線的距離;合併左右口罩為新口罩。
  • 旋轉新口罩,角度為中心線相對於 y 軸的旋轉角。
  • 將新口罩放在原圖適當位置。

關於人臉識別,可以使用 face_recognition[2] 庫進行識別。關於圖像處理,可以使用 Pillow[3] 庫進行處理。

代碼

有了思路之後,實現就是件相對輕鬆的事情。不過對庫的熟悉和圖片的變換計算可能要花些時間。

詳細的代碼請參考如下鏈接,這裡僅說明下最核心的步驟:

https://github.com/Prodesire/face-mask

人臉識別:

<code>import face_recognition  face_image_np = face_recognition.load_image_file('/path/to/face/picture') face_landmarks = face_recognition.face_landmarks(face_image_np) /<code>

藉助 face_recognition 庫可以輕鬆的識別出人像,最終得到的 face_landmarks 是一個列表,裡面的每個 face_landmark 都表示一個人像數據。

face_landmark 是一個字典,其中的鍵表示人像特徵,值表示該特徵的點的列表。比如:

  • 鍵 nose_bridge 表示鼻樑
  • 鍵 chin 表示臉頰

我們需要根據每個 face_landmark,給對應的頭像戴上口罩。

獲得鼻子和臉頰的特徵點:

<code>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] /<code>

通過上述代碼,我們獲得了:

  • 表示上鼻樑的一個點:nose_point
  • 表示臉左點:chin_left_point
  • 表示臉右點:chin_right_point
  • 表示臉底點:chin_bottom_point

拆分、縮放和合並口罩:

<code>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) /<code>

上述代碼主要做了如下內容:

  • 將口罩左右平均分為兩個部分。
  • 調整左口罩大小,寬度為臉左點到中心線的距離*寬度係數 1.2。
  • 調整右口罩大小,寬度為臉右點到中心線的距離*寬度係數 1.2。
  • 合併左右口罩為新口罩。

get_distance_from_point_to_line 用來獲取一個點到一條線的距離,具體實現可看源代碼。

width_ratio 是寬度係數,用來適當擴大口罩。原因我們是根據臉頰的寬度計算口罩的寬度,但口罩是待在耳朵上的,真實寬度應該要更寬。

旋轉口罩、並放到原圖適當位置:

<code># 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) /<code>

上述代碼主要做了如下內容:

  • 旋轉新口罩,角度為中心線相對於 y 軸的旋轉角。
  • 計算口罩應該放置的座標。
  • 將新口罩放在原圖的計算出的座標下。

最後就是將新圖片保存到本地路徑,代碼不再展示。

總結

我們藉助 face_recognition 庫可以輕鬆的識別出人像,然後根據臉頰的寬度和鼻樑位置計算出口罩的大小、方向和位置,並最終生成出戴上口罩的圖片。

整個過程並不複雜,但在座標計算上要格外小心,如此,我們便打造了一個短小精悍的“自動戴上口罩”程序!


分享到:


相關文章: