Rock It 《Armbian》九‧一

透過範例學習一個程式庫,是傳統經典之方式也!

Help/Info

 

但時間一長恐覺索然無趣,或可輔之以自覺有趣的議題,比方說︰

如何『數位化妝』呢?

Find and manipulate facial features in pictures

Get the locations and outlines of each person’s eyes, nose, mouth and chin.

rock64@rock64:~/face_recognition/examples$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import face_recognition
>>> image = face_recognition.load_image_file("biden.jpg")
>>> face_landmarks_list = face_recognition.face_landmarks(image)
>>> face_landmarks_list
[{'right_eye': [(629, 348), (647, 342), (661, 346), (672, 357), (659, 358), (644, 354)], 'nose_bridge': [(601, 328), (599, 352), (598, 375), (596, 400)], 'nose_tip': [(555, 414), (570, 421), (586, 428), (601, 428), (614, 426)], 'left_eyebrow': [(488, 294), (509, 279), (535, 278), (561, 283), (584, 296)], 'left_eye': [(512, 320), (528, 316), (544, 319), (557, 331), (541, 330), (525, 327)], 'top_lip': [(519, 459), (545, 455), (566, 456), (580, 462), (595, 462), (610, 470), (627, 480), (620, 477), (593, 470), (579, 468), (564, 463), (527, 459)], 'right_eyebrow': [(622, 307), (646, 305), (670, 309), (691, 321), (698, 344)], 'bottom_lip': [(627, 480), (606, 482), (589, 479), (575, 477), (560, 473), (540, 468), (519, 459), (527, 459), (563, 461), (577, 466), (592, 468), (620, 477)], 'chin': [(429, 328), (426, 368), (424, 408), (425, 447), (437, 484), (460, 515), (490, 538), (524, 556), (562, 564), (600, 566), (630, 554), (655, 533), (672, 507), (684, 476), (694, 445), (702, 413), (707, 382)]}]
>>>

 

Finding facial features is super useful for lots of important stuff. But you can also use it for really stupid stuff like applying digital make-up (think ‘Meitu’):

 

digital_makeup.py

from PIL import Image, ImageDraw
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("biden.jpg")

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)

for face_landmarks in face_landmarks_list:
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image, 'RGBA')

    # Make the eyebrows into a nightmare
    d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
    d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
    d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
    d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)

    # Gloss the lips
    d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
    d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
    d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
    d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)

    # Sparkle the eyes
    d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
    d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))

    # Apply some eyeliner
    d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
    d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)

    pil_image.show()

 

     

 

借著了解他人『專案』之想法心思︰

/face_recognition

The world’s simplest facial recognition api for Python and the command line

Face Recognition

You can also read a translated version of this file in Chinese 简体中文版.

Recognize and manipulate faces from Python or from the command line with the world’s simplest face recognition library.

Built using dlib‘s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.

This also provides a simple face_recognition command line tool that lets you do face recognition on a folder of images from the command line!

 

然後深入其『原始碼』架構邏輯︰

face_recognition/face_recognition/api.py

# -*- coding: utf-8 -*-

import PIL.Image
import dlib
import numpy as np

try:
    import face_recognition_models
except Exception:
    print("Please install `face_recognition_models` with this command before using `face_recognition`:\n")
    print("pip install git+https://github.com/ageitgey/face_recognition_models")
    quit()

face_detector = dlib.get_frontal_face_detector()

predictor_68_point_model = face_recognition_models.pose_predictor_model_location()
pose_predictor_68_point = dlib.shape_predictor(predictor_68_point_model)

predictor_5_point_model = face_recognition_models.pose_predictor_five_point_model_location()
pose_predictor_5_point = dlib.shape_predictor(predictor_5_point_model)

cnn_face_detection_model = face_recognition_models.cnn_face_detector_model_location()
cnn_face_detector = dlib.cnn_face_detection_model_v1(cnn_face_detection_model)

face_recognition_model = face_recognition_models.face_recognition_model_location()
face_encoder = dlib.face_recognition_model_v1(face_recognition_model)
...

 

將能更快樂的學習乎☆