python調(diào)用opencv實(shí)現(xiàn)貓臉檢測(cè)功能
Python 小貓檢測(cè),通過(guò)調(diào)用opencv自帶的貓臉檢測(cè)的分類(lèi)器進(jìn)行檢測(cè)。
分類(lèi)器有兩個(gè):haarcascade_frontalcatface.xml和
haarcascade_frontalcatface_extended.xml。可以在opencv的安裝目錄下找到
D:\Program Files\OPENCV320\opencv\sources\data\haarcascades
小貓檢測(cè)代碼為:
1. 直接讀取圖片調(diào)用
import cv2
image = cv2.imread("cat_04.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the cat detector Haar cascade, then detect cat faces
# in the input image
detector = cv2.CascadeClassifier("haarcascade_frontalcatface.xml")
#haarcascade_frontalcatface_extended.xml
rects = detector.detectMultiScale(gray, scaleFactor=1.1,
minNeighbors=10, minSize=(100, 100))
# loop over the cat faces and draw a rectangle surrounding each
print (enumerate(rects))
for (i, (x, y, w, h)) in enumerate(rects):
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(image, "Cat #{}".format(i + 1), (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
print (i, x,y,w,h)
# show the detected cat faces
cv2.imshow("Cat Faces", image)
cv2.waitKey(1)
檢測(cè)效果:

2. 通過(guò)命令控制符調(diào)用
也可以通過(guò)調(diào)用argparse庫(kù),進(jìn)行整體調(diào)用
新建cat_detect.py文件
# import the necessary packages
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-c", "--cascade", default="haarcascade_frontalcatface_extended.xml",
help="path to cat detector haar cascade")
args = vars(ap.parse_args())
#"haarcascade_frontalcatface_extended.xml",
# load the input image and convert it to grayscale
#image = cv2.imread(args["image"])
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the cat detector Haar cascade, then detect cat faces
# in the input image
detector = cv2.CascadeClassifier(args["cascade"])
rects = detector.detectMultiScale(gray, scaleFactor=1.1,
minNeighbors=10, minSize=(120, 120)) # cat good
# loop over the cat faces and draw a rectangle surrounding each
print (enumerate(rects))
for (i, (x, y, w, h)) in enumerate(rects):
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(image, "cat #{}".format(i + 1), (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
# show the detected cat faces
cv2.imshow("Cat Faces", image)
cv2.waitKey(0)
通過(guò)“命令控制符”調(diào)用
cmd cd E:\WORK\py\detectCat E:\WORK\py\detectCat>python cat_detector.py --image cat_07.png

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas對(duì)每個(gè)分組應(yīng)用apply函數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Pandas對(duì)每個(gè)分組應(yīng)用apply函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python-flask調(diào)用接口返回中文數(shù)據(jù)問(wèn)題
這篇文章主要介紹了Python-flask調(diào)用接口返回中文數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Python實(shí)現(xiàn)FTP弱口令掃描器的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)FTP弱口令掃描器的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python實(shí)現(xiàn)GB格式序列文件轉(zhuǎn)換Fasta格式文件
這篇文章主要為大家介紹了Python實(shí)現(xiàn)GB格式序列文件轉(zhuǎn)換Fasta格式文件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

