Detect Cat Faces in Real-Time using Python-OpenCV
Face Detection is a technology to identify faces from the image. We use Python’s OpenCV for this. We can also use Face Detection in the case of Animals too. If one can take a close look at the OpenCV repository, the haar cascades directory to be specific (where the OpenCV stores all its pre-trained haar classifiers to detect various objects, body parts, etc.), there are two files:
- haarcascade_frontalcatface.xml
- haarcascade_frontalcatface_extended.xml
The objective of the program given is to detect the object of interest(cat face) in real-time and to keep tracking the same object. This is a simple example of how to detect the cat face in Python. You can try to use training samples of any other object of your choice to be detected by training the classifier on required objects.
Below is the implementation.
import cv2
face_cascade = cv2.CascadeClassifier( 'haarcascade_frontalcatface.xml' )
cap = cv2.VideoCapture( 0 )
while 1 :
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3 , 5 )
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x + w,y + h),( 255 , 255 , 0 ), 2 )
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
cv2.imshow( 'img' ,img)
k = cv2.waitKey( 30 ) & 0xff
if k = = 27 :
break
cap.release()
cv2.destroyAllWindows()
|
Output:
Last Updated :
03 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...