目錄
一、人臉檢測
1、OpenCV人臉檢測
操作步驟:
完整代碼:
運行結果:
引數含義:
2、Face-Recognition人臉檢測
①HOG演算法
②CNN演算法(卷積神經網路)
③問題記錄與總結
二、人臉特征點標定
操作步驟:
完整代碼:
運行結果:
人臉識別流程:人臉檢測+人臉對齊+特征點提取+人臉匹配
一、人臉檢測
1、OpenCV人臉檢測
OpenCV自帶訓練好的人臉Haar特征分類器,存盤在D:\Python\Lib\site-packages\cv2\data\檔案夾下,其中幾個.xml檔案如下:
人臉檢測器(默認):haarcascade_frontalface_default.xml
人臉檢測器(快速Harr):haarcascade_frontalface_alt2.xml
人臉檢測器(側視):haarcascade_profileface.xml
眼部檢測器(左眼):haarcascade_lefteye_2splits.xml
眼部檢測器(右眼):haarcascade_righteye_2splits.xml
身體檢測器:haarcascade_fullbody.xml
微笑檢測器:haarcascade_smile.xml
操作步驟:
- cv2.CascadeClassifier()加載Haar特征檢測分類器
- cv2.imread()載入影像(讀取結果為BGR格式,顏色區別于RGB格式),并轉換為灰度圖片
- 識別影像中的人臉,回傳所有人臉的矩形框向量組
- 在影像中畫上矩形框,顯示識別結果
完整代碼:
import cv2
import matplotlib.pyplot as plt
def detect(filename):
face_cascade=cv2.CascadeClassifier('D:\Python\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml') #定義級聯分類器
img=cv2.imread(filename)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #將BGR格式轉換成灰度圖片
faces=face_cascade.detectMultiScale(gray, 1.3, 5) #識別影像中的人臉,回傳所有人臉的矩形框向量組
num=0
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) #在影像中畫上矩形框
num+=1
print('人臉數量:',num)
plt.imshow(img)
plt.axis('off')
plt.show()
detect('calorie.jpg')
運行結果:

(可以看出,圖片中共有11人,只識別出來10人,有所偏差)
引數含義:
學習方式——查檔案 Python Module Docs,其中‘[’表示可選項

cv2.cvtColor(src, code[, dst[, dstCn]]) -> dst
src 需要轉換的圖片
code 轉換成何種格式
cv2.COLOR_BGR2GRAY 將BGR格式轉換成灰度圖片
detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) -> objects
image 被檢測的圖片,需要轉換為灰度圖
scaleFactor 為了檢測到不同大小的目標,通過該引數把影像長寬同時按照一定比例(默認為1.1)逐步縮小后檢測(該引數設定越大,計算速度越快,但可能會錯過了某個大小的人臉)
minNeighbors 只有檢測目標被識別出來的次數大于等于這個值(默認為3),才被認為是正確的結果
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
pt1 Vertex of the rectangle
pt2 Vertex of the rectangle opposite to pt1
color Rectangle color
thickness Thickness of lines that make up the rectangle
2、Face-Recognition人臉檢測
①HOG演算法
“HOG” is less accurate but faster on CPUs.The default is “HOG”.
操作步驟:
- face_recognition.load_image_file() 加載影像
- 呼叫face_recognition.face_locations(image),定位所有影像中識別出的人臉位置資訊(回傳值是串列形式,每個人臉是一個tuple存盤,包括[top, right, bottom, left])
- 回圈找到的所有人臉,列印每張臉的位置資訊
- 在影像中畫上矩形框,顯示識別結果
完整代碼:
import face_recognition
import cv2
import matplotlib.pyplot as plt
image = face_recognition.load_image_file("calorie.JPG")
face_locations=face_recognition.face_locations(image)
face_num=len(face_locations)
print('人臉數量:',face_num)
org = cv2.imread("calorie.JPG")
for i in range(0,face_num):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left, top)
end = (right, bottom)
color = (255,0,0)
thickness = 2
img=cv2.rectangle(org, start, end, color, thickness)
plt.imshow(img)
plt.axis('off')
plt.show()
運行結果:

②CNN演算法(卷積神經網路)
“CNN” is a more accurate deep-learning model which is GPU/CUDA accelerated (if available).
完整代碼:
import face_recognition
import cv2
import matplotlib.pyplot as plt
image = face_recognition.load_image_file("calorie.JPG")
face_locations_useCNN = face_recognition.face_locations(image,model='cnn') #model默認為'hog'
face_num1=len(face_locations_useCNN)
print('人臉數量:',face_num1)
org = cv2.imread("calorie.JPG")
for i in range(0,face_num1):
top = face_locations_useCNN[i][0]
right = face_locations_useCNN[i][1]
bottom = face_locations_useCNN[i][2]
left = face_locations_useCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
img=cv2.rectangle(org, start, end, color, thickness)
plt.imshow(img)
plt.axis('off')
plt.show()
運行結果:

③問題記錄與總結
dlib安裝失敗:
在安裝dlib庫時,采用pip install dlib、pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl兩種方法均失敗,部分報錯資訊如下:
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++.
最終,通過安裝cmake、boost、Visual Studio 2019的方式解決問題
下載速度緩慢:
1、使用whl檔案:通過網站https://pypi.org/project,下載whl第三方庫安裝檔案(與Python版本一致),將其放置在Python安裝目錄下;在當前目錄下,運行命令pip install xxx.whl
2、臨時使用清華鏡像源:運行命令pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx
3、永久配置清華鏡像源:在Users目錄中創建pip目錄,再新建檔案pip.ini;打開pip.ini檔案,復制粘貼以下內容并保存
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
二、人臉特征點標定
操作步驟:
- 獲取人臉框位置的檢測器與人臉關鍵點檢測器
- 讀取影像,進行影像的灰度化
- 使用detector進行人臉檢測,獲得人臉框的位置資訊
- 遍歷檢測到的框,尋找人臉的68個標定點;遍歷所有點,列印出其坐標,并圈出來
完整代碼:
import cv2
import dlib
import matplotlib.pyplot as plt
detector = dlib.get_frontal_face_detector() #獲取人臉分類器
predictor = dlib.shape_predictor(r"D:\Python\Lib\site-packages\face_recognition_models\models\shape_predictor_68_face_landmarks.dat") #獲取人臉檢測器
path = "Martina.JPEG"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray, 1) #使用detector進行人臉檢測,dets為回傳的結果,(gray, 1)=(灰度圖,1個通道)
for face in dets:
shape = predictor(img, face) #尋找人臉的68個標定點
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
img=cv2.circle(img, pt_pos, 2, (255, 0, 0), 1) #遍歷所有點,列印出其坐標,并圈出來
plt.imshow(img)
plt.axis('off')
plt.show()
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301378.html
標籤:其他
