TShopping

 找回密碼
 註冊
搜索
查看: 890|回復: 0
打印 上一主題 下一主題

[教學] 人臉辨識 Python

[複製鏈接]
跳轉到指定樓層
1#
發表於 2021-5-4 22:01:09 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
人臉辨識於Python,很常用到dlib套件,但此套件於python3.6板之後就沒有更新。 • 於dlib官網下載資料後重新編譯,如下連結

Python 用 CMake 編譯並安裝 dlib (Win10)

• dlib是經過編譯後的dll檔案,將人臉特徵分成68個,可快速解析。



• 人臉辨識的資料,這些資料於網路上取得,僅供練習
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


完整代碼
  1. from imutils.face_utils import FaceAligner
  2. from imutils.video import count_frames
  3. import imutils
  4. import numpy as np
  5. import dlib
  6. import cv2

  7. detector = dlib.get_frontal_face_detector()
  8. predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
  9. fa = FaceAligner(predictor, desiredFaceWidth=64)

  10. face_filename = 1


  11. def detect(img, idx, totle):
  12.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  13.     faces = detector(gray, 1)

  14.     for face in faces:
  15.         faceAligned = fa.align(gray, gray, face)
  16.         global face_filename
  17.         cv2.imwrite('./face/{0}.png'.format(face_filename), faceAligned)
  18.         face_filename += 1
  19.     print('Working with {0} frame. completed {1:.2f} %'.format(idx, idx / float(totle) * 100))


  20. detect_video = '0612.mp4'
  21. videoCapture = cv2.VideoCapture(detect_video)
  22. success, frame = videoCapture.read()
  23. frame_counter = 1
  24. frame_totle = count_frames(detect_video)
  25. while success:
  26.     detect(frame, frame_counter, frame_totle)
  27.     success, frame = videoCapture.read()
  28.     frame_counter += 1

  29. 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進行分類,判斷這個人臉比較接近誰。


 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-23 19:35 , Processed in 0.047183 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表