国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python調用opencv實現貓臉檢測功能

2020-02-16 00:40:09
字體:
來源:轉載
供稿:網友

Python 小貓檢測,通過調用opencv自帶的貓臉檢測的分類器進行檢測。

分類器有兩個:haarcascade_frontalcatface.xml和
haarcascade_frontalcatface_extended.xml。可以在opencv的安裝目錄下找到

D:/Program Files/OPENCV320/opencv/sources/data/haarcascades

小貓檢測代碼為:

1. 直接讀取圖片調用

import cv2image = 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 imagedetector = cv2.CascadeClassifier("haarcascade_frontalcatface.xml")#haarcascade_frontalcatface_extended.xmlrects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(100, 100))# loop over the cat faces and draw a rectangle surrounding eachprint (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 facescv2.imshow("Cat Faces", image)cv2.waitKey(1)

檢測效果:

2. 通過命令控制符調用

也可以通過調用argparse庫,進行整體調用

新建cat_detect.py文件

# import the necessary packagesimport argparseimport cv2# construct the argument parse and parse the argumentsap = 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 imagedetector = 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 eachprint (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 facescv2.imshow("Cat Faces", image)cv2.waitKey(0)

通過“命令控制符”調用

cmdcd E:/WORK/py/detectCatE:/WORK/py/detectCat>python cat_detector.py --image cat_07.png

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永和县| 锡林郭勒盟| 唐河县| 娱乐| 长治市| 湾仔区| 凉城县| 治多县| 绥阳县| 德化县| 肃南| 溧水县| 廉江市| 中方县| 北海市| 宾阳县| 禄丰县| 湖北省| 德惠市| 崇阳县| 保亭| 阿城市| 邵东县| 库车县| 抚远县| 茶陵县| 台前县| 廉江市| 奉化市| 河东区| 磴口县| 灵川县| 平湖市| 中宁县| 彰化县| 巴马| 凉城县| 江源县| 仙居县| 徐汇区| 郴州市|