python調(diào)用opencv實現(xiàn)貓臉檢測功能
Python 小貓檢測,通過調(diào)用opencv自帶的貓臉檢測的分類器進行檢測。
分類器有兩個:haarcascade_frontalcatface.xml和
haarcascade_frontalcatface_extended.xml。可以在opencv的安裝目錄下找到
D:\Program Files\OPENCV320\opencv\sources\data\haarcascades
小貓檢測代碼為:
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)
檢測效果:
2. 通過命令控制符調(diào)用
也可以通過調(diào)用argparse庫,進行整體調(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)
通過“命令控制符”調(diào)用
cmd cd E:\WORK\py\detectCat E:\WORK\py\detectCat>python cat_detector.py --image cat_07.png
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python?Seaborn繪制統(tǒng)計圖全面指南(直方圖散點圖小提琴圖熱力圖相關系數(shù)圖多張合并)
這篇文章主要介紹了python?Seaborn繪制統(tǒng)計圖全面指南,包括直方圖,散點圖,小提琴圖,熱力圖,相關系數(shù)圖及多張圖合并的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2024-01-01詳解Python之數(shù)據(jù)序列化(json、pickle、shelve)
本篇文章主要介紹了Python之數(shù)據(jù)序列化,本節(jié)要介紹的就是Python內(nèi)置的幾個用于進行數(shù)據(jù)序列化的模塊,有興趣的可以了解一下。2017-03-03Python使用pyglet庫完整實現(xiàn)漢諾塔游戲流程詳解
這篇文章主要介紹了Python使用pyglet庫完整實現(xiàn)漢諾塔游戲流程,漢諾塔問題是一個遞歸問題,也可以使用非遞歸法來解決,這個問題不僅是一個數(shù)學和邏輯問題,也是一個很好的教學工具,可以用來教授遞歸、算法和邏輯思考等概念,需要的朋友可以參考下2007-02-02Python?Httpx庫實現(xiàn)超跑式網(wǎng)絡請求用法實例
這篇文章主要為大家介紹了Python?Httpx庫實現(xiàn)超跑式網(wǎng)絡請求用法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01在Python中定義函數(shù)并調(diào)用的操作步驟
這篇文章主要介紹了在Python中如何定義函數(shù)并調(diào)用它,函數(shù)的定義和調(diào)用是Python編程中最基本也是最重要的概念之一,掌握它們對于進行有效的Python編程至關重要,需要的朋友可以參考下2024-01-01