2018
January
Standard

Detecting cats in images [openCV]

#vi cat_detector.py

add following code into the cat_detector.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.xml",
	help="path to cat detector haar cascade")
args = vars(ap.parse_args())
# load the input image and convert it to grayscale
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.3,
	minNeighbors=10, minSize=(75, 75))
# loop over the cat faces and draw a rectangle surrounding each
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)
-----------------------------------------------

create folder /images (opencv-3.1.0/data/haarcascades/images) and upload some cat pictures from internet

#workon cv
#cd ~/opencv-3.1.0/data/haarcascades
$python cat_detector.py --image images/cat_01.jpg

output:
cat_face

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Big Thanks to Adrian Rosebrock
http://www.pyimagesearch.com

more