怎么在Python中使用opencv實(shí)現(xiàn)人臉檢測功能-創(chuàng)新互聯(lián)

怎么在Python中使用opencv 實(shí)現(xiàn)人臉檢測功能?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)公司2013年至今,先為彭州等服務(wù)建站,彭州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為彭州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

導(dǎo)入庫,并做命令行參數(shù)處理。你在命令行可以輸入如下:

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

我在程序中都有缺省參數(shù)處理,你如果集成測試或命令行不輸參數(shù)的話,就要修改好你的缺省值。

這樣命令行就是python detect_faces.py ,同時也可以輸入命令行輸入?yún)?shù)。

# 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 //構(gòu)造命令行參數(shù)分析
# 為了集成測試,或者命令行輸入的簡單,這里都有缺省參數(shù)
#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())
 導(dǎo)入圖形文件,并灰度處理
# load our image and convert it to grayscale 導(dǎo)入圖形文件,并灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,width=800)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
導(dǎo)入檢測文件,檢測圖中人臉,顯示檢測到的人臉數(shù)
# load the face detector and detect faces in the image
# 導(dǎo)入臉部檢測文件
detector = cv2.CascadeClassifier(args["detector"])
#檢測圖形中的臉部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#顯示檢測到的人臉數(shù)目
print("[INFO] detected {} faces".format(len(rects)))
 循環(huán),繪圖每個檢測到的人臉框,并圖形顯示
# load the face detector and detect faces in the image
# 導(dǎo)入臉部檢測
detector = cv2.CascadeClassifier(args["detector"])
#檢測圖形中的臉部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#顯示檢測到的人臉數(shù)目
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 //構(gòu)造命令行參數(shù)分析
# 為了集成測試,或者命令行輸入的簡單,這里都有缺省參數(shù)
#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 導(dǎo)入圖形文件,并灰度化
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
# 導(dǎo)入臉部檢測文件
detector = cv2.CascadeClassifier(args["detector"])
#檢測圖形中的臉部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#顯示檢測到的人臉數(shù)目
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)

看完上述內(nèi)容,你們掌握怎么在Python中使用opencv 實(shí)現(xiàn)人臉檢測功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章名稱:怎么在Python中使用opencv實(shí)現(xiàn)人臉檢測功能-創(chuàng)新互聯(lián)
分享地址:http://muchs.cn/article38/hiipp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)做網(wǎng)站、品牌網(wǎng)站制作微信小程序、營銷型網(wǎng)站建設(shè)、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)