2022-4-15 撰寫文章
- (其實累積了Tibame 2021年的OpenCV 物流檢測課程 + 網路IThome + 市場上書本)
A.環境: (相關資訊 進入python 用help( ) 函數查)
- 硬體:ASUS VivoBook Core i5 10th GEN ,NB 型號:X413FA(MGF:Y2020)
- Python 版本 3.7.9 64
- (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
- face_recongition VERSION
- 1.2.3 PACKAGE CONTENTS
- api
- face_detection_cli
- face_recognition_cli
- Visual Studio 2022 C++ (選項)
- Cmake VERSION 3.22.3
- dlib
- OpenCV (cv2) opencv_version = '4.5.1.48'
f this is your first time using Python, you should definitely check outthe tutorial on the Internet at
https://docs.python.org/3.7/tutorial/
B.注意事項:
- Python 請安裝合適的版本與位元(目前NB應該都是64位元)
- 必須安裝 Cmake,Visual C++,OpenCV
- 圖片檔案名稱大小寫,包含主檔名跟副檔名(ex: biden.jpg 跟biden.JPG可能會有錯誤)
- 圖片存放路徑可以直接放在與 撰寫的python 程式檔(ex: Test.py)同一個folder路徑下
- 程式有兩個python檔 =>每次新增圖片先用encoding訓練完,然後再用主程式測試
- 建議有兩三張圖片,其中一張可以用來做測試,最好不同時期拍的照片或情境差距的照片
圖片資料:
- 準備obama.jpg ,biden.jpg....等等可以先選三個人然後下載圖片.
- 拿一張盡量臉部較明顯且解析好的拿來當訓練資料的圖.
- 要增加圖,也要增加大括號裡面的圖名資料(如黃底)如下新增一個Tony.jpg:
}, # 這個接在前面大括號的逗點別忘了.
{
"name": "Tony",
"filename": "Tony.jpg",
"face_encoding": None
}
主程式Code: findfaces_webcam.py
------------------------------------------------------------------------將以下拷貝
# Step 1
# 需要先安装cmake
# CMake must be installed to build the following extensions: dlib
# 否則安裝 face_recognition 出現以下的 問題
# × Running setup.py install for dlib did not run successfully.
# │ exit code: 1
# ╰─> [8 lines of output]
# running install
# running build
# running build_py
# package init file 'tools\python\dlib\__init__.py' not found (or not a regular file)
# running build_ext
# ERROR: CMake must be installed to build dlib
# [end of output]
# Step2:
# 裝完 cmake 還得裝 Visual C++
# 否則 出現以下訊息.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# You must use Visual Studio to build a python extension on windows. If you
# are getting this error it means you have not installed Visual C++. Note
# that there are many flavors of Visual Studio, like Visual Studio for C#
# development. You need to install Visual Studio for C++.
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# 然後裝完 Visual C++ 重新開機
# 最後安裝 face_recognition
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Using legacy 'setup.py install' for dlib, since package 'wheel' is not installed.
# Installing collected packages: dlib, face_recognition
# Running setup.py install for dlib ... done
# Successfully installed dlib-19.23.1 face_recognition-1.3.0
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
import face_recognition
import cv2
import numpy as np
import pickle
with open("faces_encoding.dat", "rb") as f:
known_face_list = pickle.load(f)
known_face_encodings = [data["face_encoding"] for data in known_face_list]
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if face_distances[best_match_index] < 0.5: ### 準確度距離改這段
name = known_face_list[best_match_index]["name"]
else:
name = "Unknown"
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!畫框框標示
#畫一個boarder=2 紅方框(0,0,255)
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 畫一個填滿的實框
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
# 填入文字
cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_DUPLEX, 1.0, (255, 255, 255), 1)
#顯示出來 辨識結果
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# !!!!!!!!!!!!!! Tip 技巧:
# !!!!!!!!!!!!!!(1) 测试tolerance=0.5或0.4比较合适==> Tony 實測,覺得 distacne 0.5 真的好點
# !!!!!!!!!!!!!!(2) 可以用臉部大圖做訓練圖
# !!!!!!!!!!!!!!(3) 如果只是要拍照做人臉辨識(打卡)可以先關掉持續偵測減少負載,用靜態擷取的再做比對(亮燈擷取)
# !!!!!!!!!!!!!!!! =>輸出判斷是誰的時候,可以用三張或多張圖同時聯集相似, 然後判斷是哪個人
# !!!!!!!!!!!!!!(4) 鏡頭解析度可以增高
# !!!!!!!!!!!!!!(5) 對函數作調參
# !!!!!!!!!!!!!!(6) 對webcam capture 的影像作適當前處理 新增成另一張,多一張 不同層次的圖像 比對
# !!!!!!!!!!!!!!(7) 更準確的識別,可以設定重新採樣的次數,face_encodings 傳入 num_jitters 來實作,預設 0,範圍可設定 0-100,越高越準,但速度越慢,等比例慢(差距100倍?)