face_recognition 基礎接口
face_recognition使用世界上最簡單的人臉識別庫,在Python或命令行中識別和操作人臉。
使用dlib最先進的人臉識別技術構建而成,並具有深度學習功能。 該模型在Labeled Faces in the Wild基准中的准確率為99.38%。
face_recognition 官方文檔 :https://pypi.org/project/face_recognition/
查找圖片中的面孔
# 導入face_recognition模塊 import face_recognition # 查找圖片中的面孔 # 將jpg文件添加到numpy數組中 image = face_recognition.load_image_file("1.jpg") # 查找圖片中人臉(上下左右)的位置,圖像中可能有多個人臉 # face_locations可能的值類似為 [(135,536,198,474),()] face_locations = face_recognition.face_locations(image) print(face_locations)
查找和操作圖片中的面部特征
# 導入face_recognition模塊 import face_recognition # 查找圖片中人臉的所有面部特征(眉毛,眼睛,鼻子,上下嘴唇,面部輪廓) # 將jpg文件添加到numpy數組中 image = face_recognition.load_image_file("1.jpg") face_landmarks_list = face_recognition.face_landmarks(image) print(face_landmarks_list)
/usr/bin/python3.6 /home/wjw/PycharmProjects/face/find_nose.py [{'chin': [(280, 439), (282, 493), (283, 547), (290, 603), (308, 654), (340, 698), (380, 733), (427, 760), (485, 770), (544, 766), (592, 738), (634, 704), (668, 661), (689, 613), (701, 563), (712, 514), (722, 466)], 'left_eyebrow': [(327, 373), (354, 340), (395, 323), (442, 324), (487, 337)], 'right_eyebrow': [(560, 344), (603, 340), (647, 348), (682, 372), (698, 410)], 'nose_bridge': [(519, 410), (517, 444), (515, 477), (513, 512)], 'nose_tip': [(461, 548), (485, 554), (508, 561), (532, 558), (555, 556)], 'left_eye': [(372, 424), (399, 420), (426, 420), (451, 429), (424, 433), (397, 432)], 'right_eye': [(577, 440), (605, 437), (631, 442), (655, 451), (628, 454), (601, 449)], 'top_lip': [(415, 617), (452, 600), (484, 593), (506, 600), (525, 598), (551, 610), (579, 634), (566, 630), (524, 620), (504, 619), (482, 616), (428, 616)], 'bottom_lip': [(579, 634), (546, 636), (518, 636), (498, 635), (475, 632), (447, 626), (415, 617), (428, 616), (479, 605), (500, 610), (520, 610), (566, 630)]}] Process finished with exit code 0
美圖
尋找面部特征對於許多重要的東西非常有用,比如美圖。
from PIL import Image, ImageDraw import face_recognition # 將圖片文件添加到numpy數組中 image = face_recognition.load_image_file("1.jpg") # 查找圖像中的所有面部特征 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') # 美化眉毛 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) # 嘴唇光澤 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) # 閃耀的眼睛 d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30)) d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30)) # 塗一些眼線 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模塊 import face_recognition # 識別圖像中出現的人臉 # 獲取每個圖像文件中每個面部的面部編碼 known_image = face_recognition.load_image_file("zhangjie.jpg") unknown_image = face_recognition.load_image_file("uknow.jpg") # 由於每個圖像中可能有多個人臉,所以返回一個編碼列表。 # 但是事先知道每個圖像只有一個人臉,每個圖像中的第一個編碼,取索引0。 biden_encoding = face_recognition.face_encodings(known_image)[0] unknown_encoding = face_recognition.face_encodings(unknown_image)[0] # 獲取比較結果 result = face_recognition.compare_faces([biden_encoding],unknown_encoding) print(result)