用Python給你的女神男神帶上口罩~

用Python給你的女神男神帶上口罩~

爪子印

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

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

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

使用

安裝 face-mask

確保 Python 版本在 3.6 及以上

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

使用 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] 庫進行處理。

代碼

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

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

人臉識別

<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 軸的旋轉角
  • 計算口罩應該放置的座標
  • 將新口罩放在原圖的計算出的座標下

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

今天就學習到這裡。

有喜歡的 有需要觀看資料的:

老規矩:轉發+關注,私信小編 “資料

” 免費分享資料給你!


分享到:


相關文章: