十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
怎么在Python中使用opencv 實現人臉檢測功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

導入庫,并做命令行參數處理。你在命令行可以輸入如下:
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)))
循環(huán),繪圖每個檢測到的人臉框,并圖形顯示
# 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)))最后串接所有代碼如下:
# 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)))
# loop over the bounding boxes and draw a rectangle around each face
# 循環(huán)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 實現人臉檢測功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯成都網站設計公司行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。