-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat_detector.py
37 lines (33 loc) · 1.34 KB
/
cat_detector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# USAGE
# python cat_detector.py --image images/cat_01.jpg
# 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())
cat_ext_cascade = cv2.CascadeClassifier('haarcascade_frontalcatface_extended.xml')
# 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"])
cats_ext = cat_ext_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
rects = detector.detectMultiScale(gray, scaleFactor=1.3,
minNeighbors=5, minSize=(75, 75))
total = (len(rects) + len(cats_ext))
print(total)
# 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)