華為雲——AI人臉編輯讓Lena微笑
import numpy as np
import PIL
import PIL.Image
import scipy
import scipy.ndimage
import dlib
def get_landmark(filepath, predictor):
"""get landmark with dlib
:return: np.array shape=(68, 2)
"""
detector = dlib.get_frontal_face_detector()
img = dlib.load_rgb_image(filepath)
dets = detector(img, 1)
for k, d in enumerate(dets):
shape = predictor(img, d)
t = list(shape.parts())
a = []
for tt in t:
a.append([tt.x, tt.y])
lm = np.array(a)
return lm
def align_face(filepath, predictor):
"""
:param filepath: str
:return: PIL Image
"""
lm = get_landmark(filepath, predictor)
lm_chin = lm[0: 17] # left-right
lm_eyebrow_left = lm[17: 22] # left-right
lm_eyebrow_right = lm[22: 27] # left-right
lm_nose = lm[27: 31] # top-down
lm_nostrils = lm[31: 36] # top-down
lm_eye_left = lm[36: 42] # left-clockwise
lm_eye_right = lm[42: 48] # left-clockwise
lm_mouth_outer = lm[48: 60] # left-clockwise
lm_mouth_inner = lm[60: 68] # left-clockwise
# Calculate auxiliary vectors.
eye_left = np.mean(lm_eye_left, axis=0)
eye_right = np.mean(lm_eye_right, axis=0)
eye_avg = (eye_left + eye_right) * 0.5
eye_to_eye = eye_right - eye_left
mouth_left = lm_mouth_outer[0]
mouth_right = lm_mouth_outer[6]
mouth_avg = (mouth_left + mouth_right) * 0.5
eye_to_mouth = mouth_avg - eye_avg
# Choose oriented crop rectangle.
x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1]
x /= np.hypot(*x)
x *= max(np.hypot(*eye_to_eye) * 2.0, np.hypot(*eye_to_mouth) * 1.8)
y = np.flipud(x) * [-1, 1]
c = eye_avg + eye_to_mouth * 0.1
quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y])
qsize = np.hypot(*x) * 2
# read image
img = PIL.Image.open(filepath)
output_size = 256
transform_size = 256
enable_padding = True
# Shrink.
shrink = int(np.floor(qsize / output_size * 0.5))
if shrink > 1:
rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))
img = img.resize(rsize, PIL.Image.ANTIALIAS)
quad /= shrink
qsize /= shrink
# Crop.
border = max(int(np.rint(qsize * 0.1)), 3)
crop = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))),
int(np.ceil(max(quad[:, 1]))))
crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]),
min(crop[3] + border, img.size[1]))
if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:
img = img.crop(crop)
quad -= crop[0:2]
# Pad.
pad = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))),
int(np.ceil(max(quad[:, 1]))))
pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0),
max(pad[3] - img.size[1] + border, 0))
if enable_padding and max(pad) > border - 4:
pad =外匯跟單gendan5.com np.maximum(pad, int(np.rint(qsize * 0.3)))
img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')
h, w, _ = img.shape
y, x, _ = np.ogrid[:h, :w, :1]
mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w - 1 - x) / pad[2]),
1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h - 1 - y) / pad[3]))
blur = qsize * 0.02
img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)
img += (np.median(img, axis=(0, 1)) - img) * np.clip(mask, 0.0, 1.0)
img = PIL.Image.fromarray(np.uint8(np.clip(np.rint(img), 0, 255)), 'RGB')
quad += pad[:2]
# Transform.
img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR)
if output_size < transform_size:
img = img.resize((output_size, output_size), PIL.Image.ANTIALIAS)
# Return aligned image.
return img
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2898667/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python人臉識別微笑檢測Python
- 如何讓照片中的人物笑起來?HMS Core影片編輯服務一鍵微笑功能,讓人物笑容更自然
- AI太複雜?別怕!華為雲Model Arts讓你分分鐘玩轉AI!AI
- 七牛雲杜江華:讓雲 + AI 成為企業服務的標配AI
- 華為雲的AI深潛之旅AI
- 讓AI像人類一樣操作手機,華為也做出來了AI
- 華為雲桌面Workspace,讓雲上工作更高效!
- vim 讓人愛不釋手的編輯器之神
- 面試程式碼題(華為)編輯距離面試
- 華為Mate 10升級人臉解鎖:可能是最好用的安卓人臉識別安卓
- 乾貨 | AI人臉識別之人臉搜尋AI
- 華為音訊編輯服務,實時分離人聲、伴奏和樂器聲音訊
- 華為音訊編輯服務(Audio Editor Kit),快速構建應用音訊編輯能力音訊
- 全棧為方,普惠為向:華為雲AI的2019全棧AI
- WidsMob AI Retoucher for Mac AI照片編輯工具AIMac
- WidsMob AI Retoucher for Mac(AI 照片編輯器)AIMac
- AI+慈善 ▏人臉識別+人臉模擬助力失散親人找到回家的路AI
- 官宣:華為雲學院帶你看AIAI
- 超簡單整合 HMS ML Kit 實現最大臉微笑抓拍
- 華為影片編輯服務(Video Editor Kit),助力開發者高效構建應用影片編輯能力IDE
- 讓自動駕駛撞牆,刷別人的臉付賬:最新的AI漏洞讓我們開眼界自動駕駛AI
- 華為與高通公開翻臉?背後的殺手鐧讓人心服
- 為人臉識別技術提供AI基礎資料服務支援AI
- 為了讓你高效工作,華為雲桌面是這樣做的
- AI+基因編輯,為何成為比爾·蓋茨口中的救命良方?AI
- Android | 超簡單整合HMS ML Kit實現最大臉微笑抓拍Android
- 摩杜雲AI人臉服務:打造智慧工地,守護最美勞動者AI
- 莫高窟永不褪色的微笑,照耀在華為未曾止步的資料保護征程
- AI主播、AI記者、AI編輯,傳媒AI化加速AI
- 實現 AI 換臉的第一步:人臉搜尋AI
- 華為音訊編輯服務,助力開發者高效創新音訊
- 華為音訊編輯服務帶你一鍵伴奏分離!音訊
- 馬雲:AI應該翻譯為機器智慧,機器和人有不同的智慧邏輯AI
- 華為雲災備,讓資料風險無處遁形
- Luminar AI for MacAI照片編輯軟體AIMac
- 華為雲徐直軍任華為雲董事長,餘承東任華為雲CEO
- 華為視訊編輯服務(Video Editor Kit),助力開發者高效構建應用視訊編輯能力IDE
- 帶你瞭解2022兩會黑科技:阿里雲視訊雲「AI編輯部3.0」阿里AI