最近模式識别課程的上機課中,老師給了人臉識别的Python程式讓我們自己學習,目前研究到了人臉檢測程式中的人臉特征點的标定,和如何用Opencv中的rectangle函數和circle函數來标記人臉的特征資訊。
其中所用到的子產品主要是dlib、和cv2。
先對程式中的一些陌生代碼做一個總結:
一.程式中所涉及到的OpenCV的常用函數:
1.讀入圖檔——cv2.imread(filepath,flags)
- filepath: 要讀入圖檔的完整路徑
- flags: 讀入圖檔的标志
- cv2.IMREAD_COLOR:預設參數,讀入一副彩色圖檔,忽略alpha通道
- cv2.IMREAD_GRAYSCALE:讀入灰階圖檔
- cv2.IMREAD_UNCHANGED:顧名思義,讀入完整圖檔,包括alpha通道
例如:
imgpath = './image/wsdgd.jpg'
img = cv2.imread(imgpath,cv2.IMREAD_COLOR)
2.通道拆分和合并——cv2.split()、cv2.merge()
先解釋一下為什麼要進行通道的拆分和合并,因為opencv讀取圖檔的預設像素排列是BGR,和很多其他軟體不一緻,需要轉換。
使用方法如下:
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])
3.畫矩形cv2.rectangle()
cv2.rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None),需要确定的就是矩形的兩個點(左上角與右下角),顔色,線的類型(不設定就預設)。
其中各個參數為:
- img:在img上繪圖
- pt1:起點; (left, top)
- pt2:終點; (right, bottom)
- color:線的顔色;例如:(0,255,0)(綠色)
- thickness:線的粗細程度,例如:-1,1,2,3…
例子:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = np.zeros((512,512,3),np.uint8)#生成一個空彩色圖像
cv2.rectangle(img,(20,20),(411,411),(55,255,155),5)
plt.imshow(img,'brg')
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLxEkeNdXVU1UeRpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1ATNwUjNxIjM2AjMxgTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
4.畫圓框cv2.circle()
cv2.circle(img, center, radius, color, thickness=None, lineType=None, shift=None)
- img:在img上繪圖;
- center:圓心;例如:(0,0)
- radius:半徑;例如:20
- color:線的顔色;例如:(0,255,0)(綠色)
- thickness:線的粗細程度,例如:-1,1,2,3…
例子:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = np.zeros((512,512,3),np.uint8)#生成一個空彩色圖像
cv2.circle(img,(200,200),50,(55,255,155),1)#修改最後一個參數
plt.imshow(img,'brg')
以上兩個例子來自于 https://blog.csdn.net/on2way/article/details/46793911
5.cv2.waitKey(delay)
檢視此博文:https://blog.csdn.net/Young__Fan/article/details/80022860
下面就具體程式來學習:
from sys import argv
import dlib
import cv2
import numpy
script, imgpath = argv
shape_predictor_path = './model/shape_predictor_68_face_landmarks.dat' #這是人臉68點特征檢測器模型
detector = dlib.get_frontal_face_detector() #擷取人臉分類器
predictor = dlib.shape_predictor(shape_predictor_path) #擷取人臉 68 點特征檢測器,進行人臉面部輪廓特征提取:
img = cv2.imread(imgpath, cv2.IMREAD_COLOR) #讀圖檔,cv.imread()共兩個參數,第二個為如何讀取圖檔,
#包括cv2.IMREAD_COLOR: 讀入一個彩色圖檔
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])
dets = detector(img2, 1)
print(f'the number of face is {len(dets)}')
for index, face in enumerate(dets):
print(f'face{index}; left:{face.left()}; top:{face.top()}; right:{face.right()}; bottom:{face.bottom()}')
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img,(left, top),(right, bottom),(0, 255, 0), 1)
shape = predictor(img, face)
for index, pt in enumerate(shape.parts()):
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 1, (255, 0, 0), 1)
cv2.imshow(imgpath,img)
cv2.namedWindow(imgpath,cv2.WINDOW_AUTOSIZE)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
下面是效果圖: