1. 程式人生 > 程式設計 >Python下應用opencv 實現人臉檢測功能

Python下應用opencv 實現人臉檢測功能

使用OpenCV's Haar cascades作為人臉檢測,因為他做好了庫,我們只管使用。

程式碼簡單,除去註釋,總共有效程式碼只有10多行。

所謂庫就是一個檢測人臉的xml 檔案,可以網上查詢,下面是一個地址:

https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml

如何構造這個庫,學習完本文後可以參考:

http://note.sonots.com/SciSoftware/haartraining.html

https://www.instructables.com/id/Create-OpenCV-Image-Classifiers-Using-Python/

知道構造庫,就可以檢測各種你想要檢測的東西了。

人臉檢測不是人臉識別,但是人臉識別的前提。

執行效果如下:

前提:

這個原始程式碼來自https://www.pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/的一個教學講稿。

你需要下載haarcascade_frontalface_default.xml 以及準備你要檢測的檔案,我這裡是family.jpg,放在python 檔案detect_faces.py 所在目錄(工作目錄)的子目錄images下。haarcascade_frontalface_default.xml是放在工作目錄。

如果加上攝像頭連線程式,也可實時檢測,另文介紹。

程式碼1介紹

匯入庫,並做命令列引數處理。你在命令列可以輸入如下:

python detect_faces.py --image image/family.jpg  --detector haarcascade_frontalface_default.xml

我在程式中都有預設引數處理,你如果整合測試或命令列不輸引數的話,就要修改好你的預設值。

這樣命令列就是python detect_faces.py ,同時也可以輸入命令列輸入引數。

# USAGE 使用方法是:
# python detect_faces.py --image images/family.jpg \
# --detector haarcascade_frontalface_default.xml
# import the necessary packages 輸入包
# import imutils 
import argparse
import cv2
# construct the argument parser and parse the arguments //構造命令列引數分析
# 為了整合測試,或者命令列輸入的簡單,這裡都有預設引數
#image 是 images/family.jpg
#detector 是 haarcascade_frontalface_default.xml
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",default='images/family.jpg',help="path to the input image")
ap.add_argument("-d","--detector",default='haarcascade_frontalface_default.xml',help="path to Haar cacscade face detector")
args = vars(ap.parse_args())
 匯入圖形檔案,並灰度處理
# load our image and convert it to grayscale 匯入圖形檔案,並灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,width=800)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
匯入檢測檔案,檢測圖中人臉,顯示檢測到的人臉數
# load the face detector and detect faces in the image
# 匯入臉部檢測檔案
detector = cv2.CascadeClassifier(args["detector"])
#檢測圖形中的臉部
rects = detector.detectMultiScale(gray,scaleFactor=1.05,minNeighbors=9,minSize=(40,40),flags=cv2.CASCADE_SCALE_IMAGE)
#顯示檢測到的人臉數目
print("[INFO] detected {} faces".format(len(rects)))
 迴圈,繪圖每個檢測到的人臉框,並圖形顯示
# load the face detector and detect faces in the image
# 匯入臉部檢測
detector = cv2.CascadeClassifier(args["detector"])
#檢測圖形中的臉部
rects = detector.detectMultiScale(gray,flags=cv2.CASCADE_SCALE_IMAGE)
#顯示檢測到的人臉數目
print("[INFO] detected {} faces".format(len(rects)))

最後串接所有程式碼如下:

# USAGE 使用方法是:
# python detect_faces.py --image images/family.jpg \
# --detector haarcascade_frontalface_default.xml
# import the necessary packages 輸入包
# import imutils 如果需要成比例縮放圖形才需要,這裡不需要
import argparse
import cv2
# construct the argument parser and parse the arguments //構造命令列引數分析
# 為了整合測試,或者命令列輸入的簡單,這裡都有預設引數
#image 是 images/family.jpg
#detector 是 haarcascade_frontalface_default.xml
ap = argparse.ArgumentParser()
ap.add_argument("-i",help="path to Haar cacscade face detector")
args = vars(ap.parse_args())
# load our image and convert it to grayscale 匯入圖形檔案,並灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,cv2.COLOR_BGR2GRAY)
# load the face detector and detect faces in the image
# 匯入臉部檢測檔案
detector = cv2.CascadeClassifier(args["detector"])
#檢測圖形中的臉部
rects = detector.detectMultiScale(gray,flags=cv2.CASCADE_SCALE_IMAGE)
#顯示檢測到的人臉數目
print("[INFO] detected {} faces".format(len(rects)))
# loop over the bounding boxes and draw a rectangle around each face
# 迴圈rects,繪圖每個檢測到的人臉框
for (x,y,w,h) in rects:
 cv2.rectangle(image,(x,y),(x + w,y + h),(0,255,0),2)
# show the detected faces
cv2.imshow("Faces",image)
cv2.waitKey(0)

總結

以上所述是小編給大家介紹的Python下應用opencv 實現人臉檢測功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!