| 
 | 
 
 
人臉辨識於Python,很常用到dlib套件,但此套件於python3.6板之後就沒有更新。 • 於dlib官網下載資料後重新編譯,如下連結 
 
Python 用 CMake 編譯並安裝 dlib (Win10) 
 
• dlib是經過編譯後的dll檔案,將人臉特徵分成68個,可快速解析。 
人臉辨識,Python 
 
 
 
• 人臉辨識的資料,這些資料於網路上取得,僅供練習  
https://drive.google.com/file/d/1UEEpq6pLJkzuompyewCa5Zr3qcOuCHqQ/view?usp=sharing  
https://drive.google.com/file/d/1So1A76il9uTms9iORgjm4Svm3QO_Mwpx/view?usp=sharing  
• 用於練習的影片 (新聞媒體公開影片,亦只用於練習)  
https://drive.google.com/file/d/1G67R_j3R3aBfxUhFkK6y7kyBp7IOCYBV/view?usp=sharing 
 
 
完整代碼 
- from imutils.face_utils import FaceAligner
 
 - from imutils.video import count_frames
 
 - import imutils
 
 - import numpy as np
 
 - import dlib
 
 - import cv2
 
  
- detector = dlib.get_frontal_face_detector()
 
 - predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
 
 - fa = FaceAligner(predictor, desiredFaceWidth=64)
 
  
- face_filename = 1
 
  
 
- def detect(img, idx, totle):
 
 -     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
 -     faces = detector(gray, 1)
 
  
-     for face in faces:
 
 -         faceAligned = fa.align(gray, gray, face)
 
 -         global face_filename
 
 -         cv2.imwrite('./face/{0}.png'.format(face_filename), faceAligned)
 
 -         face_filename += 1
 
 -     print('Working with {0} frame. completed {1:.2f} %'.format(idx, idx / float(totle) * 100))
 
  
 
- detect_video = '0612.mp4'
 
 - videoCapture = cv2.VideoCapture(detect_video)
 
 - success, frame = videoCapture.read()
 
 - frame_counter = 1
 
 - frame_totle = count_frames(detect_video)
 
 - while success:
 
 -     detect(frame, frame_counter, frame_totle)
 
 -     success, frame = videoCapture.read()
 
 -     frame_counter += 1
 
  
- print("Done!!")
 
  複製代碼 
 
 
 
• 人臉辨識於技術上是可行,但請留意道德規範與法律規定。 
• 人臉辨識1的練習,練習從影片中擷取出人臉並且進行儲存。 
• 為何他可以預測?因為dlib有提供一個整理好的人臉資訊dat檔案,這是開源資訊,原目的是進行程式的練習。 
• 以下三行可產生fa這個人類臉部偵測的物件。 
detector = dlib.get_frontal_face_detector() 
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") 
fa = FaceAligner(predictor, desiredFaceWidth=64) 
 • dlib具有人臉68個特徵擷取功能。     pip install imutils 
• imutils可針對影片中的人臉進行偵測 
    pip install face_recognition 
• face_recognition這個套件安裝之前請先安裝dlib 
• face_recognition可搭配機器學習svm進行分類,判斷這個人臉比較接近誰。  
人臉辨識,Python 
 
 
 |   
 
 
 
 |