Python 教你一行代碼給頭像戴上口罩


前言

2019 年底開始蔓延的新型肺炎疫情牽動人心,作為個體,我們力所能及的就是盡量待在家中少出門。

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

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

使用

⚠️確保 Python 版本在 3.6 及以上

安裝 face-mask
pip install face-mask

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

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

效果

給一個人戴上口罩

給多個人戴上口罩

給動漫人物戴上口罩

實現

思路

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

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

關於人臉識別,可以使用 face_recognition[2] 庫進行識別。

關於圖像處理,可以使用 Pillow[3] 庫進行處理。

代碼

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

詳細的代碼請閱讀 face-mask[4]。這里僅說明下最核心的步驟。

人臉識別

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 都表示一個人像數據。

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)

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

  • 將口罩左右平均分為兩個部分
  • 調整左口罩大小,寬度為臉左點到中心線的距離 * 寬度系數 1.2
  • 調整右口罩大小,寬度為臉右點到中心線的距離 * 寬度系數 1.2
  • 合並左右口罩為新口罩
    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 庫可以輕松的識別出人像,然后根據臉頰的寬度和鼻梁位置計算出口罩的大小、方向和位置,並最終生成出戴上口罩的圖片。整個過程並不復雜,但在坐標計算上要格外小心,如此,我們便打造了一個短小精悍的“自動戴上口罩”程序!

對人臉識別感興趣的同學可以在百度ai的官網上在線體驗下,點擊跳轉 https://ai.baidu.com/tech/face/compare

文章來源於Prodesire ,作者Prodesire http://suo.im/6kbW29


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM