Python下應(yīng)用opencv 實(shí)現(xiàn)人臉檢測(cè)功能
使用OpenCV's Haar cascades作為人臉檢測(cè),因?yàn)樗龊昧藥?kù),我們只管使用。
代碼簡(jiǎn)單,除去注釋,總共有效代碼只有10多行。
所謂庫(kù)就是一個(gè)檢測(cè)人臉的xml 文件,可以網(wǎng)上查找,下面是一個(gè)地址:
https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
如何構(gòu)造這個(gè)庫(kù),學(xué)習(xí)完本文后可以參考:
http://note.sonots.com/SciSoftware/haartraining.html
https://www.instructables.com/id/Create-OpenCV-Image-Classifiers-Using-Python/
知道構(gòu)造庫(kù),就可以檢測(cè)各種你想要檢測(cè)的東西了。
人臉檢測(cè)不是人臉識(shí)別,但是人臉識(shí)別的前提。
運(yùn)行效果如下:
前提:
這個(gè)原始代碼來自 https://www.pyimagesearch.com/2016/11/21/raspbian-opencv-pre-configured-and-pre-installed/ 的一個(gè)教學(xué)講稿。
你需要下載haarcascade_frontalface_default.xml 以及準(zhǔn)備你要檢測(cè)的文件,我這里是family.jpg,放在python 文件detect_faces.py 所在目錄(工作目錄)的子目錄images下。haarcascade_frontalface_default.xml是放在工作目錄。
如果加上攝像頭連接程序,也可實(shí)時(shí)檢測(cè),另文介紹。
代碼1介紹
導(dǎo)入庫(kù),并做命令行參數(shù)處理。你在命令行可以輸入如下:
python detect_faces.py --image image/family.jpg --detector haarcascade_frontalface_default.xml
我在程序中都有缺省參數(shù)處理,你如果集成測(cè)試或命令行不輸參數(shù)的話,就要修改好你的缺省值。
這樣命令行就是python detect_faces.py
,同時(shí)也可以輸入命令行輸入?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ù)分析 # 為了集成測(cè)試,或者命令行輸入的簡(jiǎn)單,這里都有缺省參數(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)入檢測(cè)文件,檢測(cè)圖中人臉,顯示檢測(cè)到的人臉數(shù) # load the face detector and detect faces in the image # 導(dǎo)入臉部檢測(cè)文件 detector = cv2.CascadeClassifier(args["detector"]) #檢測(cè)圖形中的臉部 rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9, minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE) #顯示檢測(cè)到的人臉數(shù)目 print("[INFO] detected {} faces".format(len(rects))) 循環(huán),繪圖每個(gè)檢測(cè)到的人臉框,并圖形顯示 # load the face detector and detect faces in the image # 導(dǎo)入臉部檢測(cè) detector = cv2.CascadeClassifier(args["detector"]) #檢測(cè)圖形中的臉部 rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9, minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE) #顯示檢測(cè)到的人臉數(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ù)分析 # 為了集成測(cè)試,或者命令行輸入的簡(jiǎn)單,這里都有缺省參數(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)入臉部檢測(cè)文件 detector = cv2.CascadeClassifier(args["detector"]) #檢測(cè)圖形中的臉部 rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9, minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE) #顯示檢測(cè)到的人臉數(shù)目 print("[INFO] detected {} faces".format(len(rects))) # loop over the bounding boxes and draw a rectangle around each face # 循環(huán)rects,繪圖每個(gè)檢測(cè)到的人臉框 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)
總結(jié)
以上所述是小編給大家介紹的Python下應(yīng)用opencv 實(shí)現(xiàn)人臉檢測(cè)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- python 基于dlib庫(kù)的人臉檢測(cè)的實(shí)現(xiàn)
- Python3利用Dlib實(shí)現(xiàn)攝像頭實(shí)時(shí)人臉檢測(cè)和平鋪顯示示例
- Linux下python與C++使用dlib實(shí)現(xiàn)人臉檢測(cè)
- python 3利用Dlib 19.7實(shí)現(xiàn)攝像頭人臉檢測(cè)特征點(diǎn)標(biāo)定
- Python基于OpenCV實(shí)現(xiàn)人臉檢測(cè)并保存
- Python OpenCV利用筆記本攝像頭實(shí)現(xiàn)人臉檢測(cè)
- Python3.6.0+opencv3.3.0人臉檢測(cè)示例
- 50行Python代碼實(shí)現(xiàn)人臉檢測(cè)功能
- python使用dlib進(jìn)行人臉檢測(cè)和關(guān)鍵點(diǎn)的示例
相關(guān)文章
Python關(guān)于OS文件目錄處理的實(shí)例分享
在本篇文章里小編給大家整理的是一篇關(guān)于Python關(guān)于OS文件目錄處理的實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)一下。2021-05-05使用Python代碼實(shí)現(xiàn)PDF文檔與SVG文件之間的轉(zhuǎn)換
PDF作為普遍采用的文件格式,確保了文檔的一致性和可靠性,而SVG(可縮放矢量圖形)則因其矢量性質(zhì),在網(wǎng)頁(yè)設(shè)計(jì)、高分辨率打印及動(dòng)態(tài)交互內(nèi)容中展現(xiàn)出無與倫比的優(yōu)勢(shì),本文將介紹如何使用Python將PDF文件轉(zhuǎn)換為SVG文件以及將SVG文件轉(zhuǎn)換為PDF文件,需要的朋友可以參考下2024-05-05Pandas數(shù)據(jù)合并的兩種實(shí)現(xiàn)方法
本文主要介紹了Pandas數(shù)據(jù)合并的兩種實(shí)現(xiàn)方法,DataFrame數(shù)據(jù)合并主要使用merge()方法和concat()方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11選擇python進(jìn)行數(shù)據(jù)分析的理由和優(yōu)勢(shì)
在本篇文章中小編給大家整理了關(guān)于選擇python進(jìn)行數(shù)據(jù)分析的理由和優(yōu)勢(shì),對(duì)此有需要的朋友們可以跟著學(xué)習(xí)參考下。2019-06-06Python django實(shí)現(xiàn)簡(jiǎn)單的郵件系統(tǒng)發(fā)送郵件功能
這篇文章主要介紹了Python django實(shí)現(xiàn)簡(jiǎn)單的郵件系統(tǒng)發(fā)送郵件功能,結(jié)合實(shí)例形式分析了django發(fā)送郵件的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07詳解基于Jupyter notebooks采用sklearn庫(kù)實(shí)現(xiàn)多元回歸方程編程
這篇文章主要介紹了詳解基于Jupyter notebooks采用sklearn庫(kù)實(shí)現(xiàn)多元回歸方程編程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Django框架實(shí)現(xiàn)的簡(jiǎn)單分頁(yè)功能示例
這篇文章主要介紹了Django框架實(shí)現(xiàn)的簡(jiǎn)單分頁(yè)功能,在之前一篇留言板之上增加了簡(jiǎn)單分頁(yè)功能,涉及Paginator模塊的簡(jiǎn)單使用技巧,需要的朋友可以參考下2018-12-12