国产探花免费观看_亚洲丰满少妇自慰呻吟_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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆子县| 汝阳县| 楚雄市| 安岳县| 清原| 英吉沙县| 云林县| 施秉县| 漳平市| 萨嘎县| 灵璧县| 青州市| 万全县| 宕昌县| 墨竹工卡县| 贵南县| 绥阳县| 甘孜县| 游戏| 和林格尔县| 电白县| 古交市| 钦州市| 务川| 济宁市| 湾仔区| 土默特右旗| 潼南县| 永仁县| 阿克陶县| 榆树市| 清水河县| 霍邱县| 香港 | 天津市| 多伦县| 彝良县| 隆安县| 龙海市| 富蕴县| 长垣县|