日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python下应用opencv 人脸检测

發(fā)布時(shí)間:2023/12/20 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python下应用opencv 人脸检测 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用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í)完本文后可以參考我的另篇博文:目標(biāo)檢測(cè)的模型haartraining培訓(xùn)

知道構(gòu)造庫(kù),就可以檢測(cè)各種你想要檢測(cè)的東西了。

人臉檢測(cè)不是人臉識(shí)別,但是人臉識(shí)別的前提。

運(yùn)行效果如下:

前提:

這個(gè)原始代碼來(lái)自?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ù)的話(huà),就要修改好你的缺省值。

這樣命令行就是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下应用opencv 人脸检测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。