人脸检测5种方法
眾所周知,人臉識別是計算機視覺應用的一個重大領域,在學習人臉識別之前,我們先來簡單學習下人臉檢測的幾種用法。
常見的人臉檢測方法大致有5種,Haar、Hog、CNN、SSD、MTCNN:
注:本文章圖片來源于網絡
相關構造檢測器的文件:opencv/data at master · opencv/opencv · GitHub
基本步驟
一、Haar
# 調整參數 img = cv2.imread('./images/001.jpg') cv_show('img',img)# 構造harr檢測器 face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')# 轉為灰度圖 img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) plt.imshow(img_gray,'gray')# 檢測結果 上圖4個人臉所以4個方框坐標 # image # scaleFactor控制人臉尺寸 默認1.1 detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.3)# 解析 for x,y,w,h in detections:cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0)) plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) # 調整參數 img = cv2.imread('./images/004.jpeg') cv_show('img',img)# 構造harr檢測器 face_detector = cv2.CascadeClassifier('./weights/haarcascade_frontalface_default.xml')# 轉為灰度圖 img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) plt.imshow(img_gray,'gray')# 檢測結果 上圖4個人臉所以4個方框坐標 # image # scaleFactor控制人臉尺寸 默認1.1 # minNeighbors 確定一個人臉框至少要有n個候選值 越高 質量越好 # [, flags[, # minSize maxSize 人臉框的最大最小尺寸 如minSize=(40,40) detections = face_detector.detectMultiScale(img_gray,scaleFactor=1.2, minNeighbors=10)# 在質量和數量上平衡# 解析 for x,y,w,h in detections:cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0)) plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))?
上述過程中:
- scaleFactor參數:用來控制人臉框的大小,可以用它來排除一些錯誤檢測;?
- minNeighbors參數:我們給人臉框起來的時候,一般一張臉會框許多的框,假如這張臉框得越多,說明質量越好,越是一張正確的“臉”。
二、Hog
對于第一次使用這個功能的同學,要提前下載一下dlib。
import dlib# 構造HOG人臉檢測器 不需要參數 hog_face_detetor = dlib.get_frontal_face_detector()# 檢測人臉獲取數據 # img # scale類似haar的scalFactor detections = hog_face_detetor(img,1)# 解析獲取的數據 for face in detections:# 左上角x = face.left()y = face.top()# 右下角r = face.right()b = face.bottom()cv2.rectangle(img,(x,y),(r,b),(0,255,0)) plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))?
三、CNN
import dlib# 構造CNN人臉檢測器 cnn_face_detector = dlib.cnn_face_detection_model_v1("./weights/mmod_human_face_detector.dat")# 檢測人臉 參數與上一種相似 detections = cnn_face_detector(img,1)for face in detections:# 左上角x = face.rect.left()y = face.rect.top()# 右下角r = face.rect.right()b = face.rect.bottom()# 置信度c = face.confidenceprint(c)cv2.rectangle(img,(x,y),(r,b),(0,255,0))plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))通過神經網絡完成,這個過程中我們還可以查看每張臉檢測時的置信度。
?
四、SSD
# 加載模型 face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt','./weights/res10_300x300_ssd_iter_140000.caffemodel')# 原圖尺寸 img_height = img.shape[0] img_width = img.shape[1]# 放縮至輸入尺寸 img_resized = cv2.resize(img,(500,300)) # 轉為2進制 img_blob = cv2.dnn.blobFromImage(img_resized,1.0,(500,300),(104.0,177.0,123.0))# 輸入 face_detector.setInput(img_blob)# 推理 detections = face_detector.forward()此時
detections.shape # (1, 1, 200, 7)說明有200個結果,后面的7則是我們做需要的一些數據,繼續如下:
# 查看人臉數量 num_of_detections = detections.shape[2]img_copy = img.copy()for index in range(num_of_detections):# 置信度detections_confidence = detections[0,0,index,2]# 通過置信度篩選if detections_confidence > 0.15:# 位置 乘以寬高恢復大小locations = detections[0,0,index,3:7] * np.array([img_width,img_height,img_width,img_height])# 打印print(detections_confidence)lx,ly,rx,ry = locations.astype('int')# 繪制cv2.rectangle(img_copy,(lx,ly),(rx,ry),(0,255,0),2)plt.imshow(cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB))?
五、MTCNN
# 導入MTCNN from mtcnn.mtcnn import MTCNN# 記載模型 face_detetor = MTCNN()# 檢測人臉 detections = face_detetor.detect_faces(img_cvt) for face in detections:x,y,w,h = face['box']cv2.rectangle(img_cvt,(x,y),(x+w,y+h),(0,255,0),2) plt.imshow(img_cvt)?
對比
| 優勢 | 劣勢 | |
| Haar | 速度最快、清涼、適合算力較小的設備 | 準確度低、偶爾誤報、無旋轉不變性 |
| HOG+Dlib | 比Haar準確率高 | 速度比Haar低,計算量大、無旋轉不變性、Dlib兼容性問題 |
| SSD | 比Haar和hog準確率高、深度學習、大小一般 | 低光照片準確率低,受膚色影響。 |
| CNN | 最準確、誤報率低、輕量 | 相對于其他方法慢、計算量大、Dlib兼容性問題 |
總結
- 上一篇: java笔试多么_世界多么精彩!
- 下一篇: 前沿丨基于深度学习的点云分割网络及点云分