Skip to content

Detect Faces using CV2 and Numpy in Python easily (or modify it to detect cats/dogs or plateNumbers)

Posted in VIDEOS

Welcome back ! Today we are detecting faces (and cats too) using Haarcascades , python and cv2 and numpy modules.

Download cascades here : https://github.com/opencv/opencv/tree/master/data/haarcascades

And use the code below to load them in, turn on your camera and read from it to detect faces :

CODE 1:


import numpy as np
import cv2

face_load_cascade_thing = cv2.CascadeClassifier('frontalface.xml')
#load in the cascade - into cv2

cap = cv2.VideoCapture(0)
#pick a videocapture device (0) is the first one -my case built in camera for the laptop


#font stuff for the text under the rectangle, will need later
font= cv2.FONT_HERSHEY_SIMPLEX
fontScale= 0.6
fontColor= (0,0,255)
#line under the rectangle, also will need later
lineType= 2


while 1:
    #we need the while loop to keep the camera going, not stop after one frame or so
    
    ret, img = cap.read()
    #grab camera data and store it into ret and img variables
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #convert to grayscale for easier detection
    
    faces = face_load_cascade_thing.detectMultiScale(gray, 1.3, 5)
    #                               1.3 - Parameter specifying how much the image size is reduced at each image scale.
    #                               5 -  (minNeighbors) higher value = less detections , better result
    #                   1 for a bunch of people 5 for 1 or two
    #this one detects the faces

    
    for (x,y,w,h) in faces:
        print("Face found:",x,y)
        #print out that it found face
        cv2.rectangle(img,(x,y),(x+w,y+h),(50,50,255),1)
        #draw a nice rectangle (what window, where,where, color, thickness of rectangle)
        

        textlocation = x,y
        cv2.putText(img,'Person',textlocation, font, fontScale,fontColor,lineType)
        

    cv2.imshow('SCANNER ON',img)
    #display the window (window title, which window)
    
    #cv2.imshow('Graything',gray)
    #to see the grayscale 
        
    cv2.waitKey(10)
    #this basically just leaves it running. Can be combined with key press
    #for example you can press Enter to close the window



Code 2:


import numpy as np
import cv2
#btw sauce is haarcascades
face_load_cascade_thing = cv2.CascadeClassifier('catdetect.xml')
cap = cv2.VideoCapture(0)

font= cv2.FONT_HERSHEY_SIMPLEX
fontScale= 0.5
fontColor= (0,0,255)
lineType= 2

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_load_cascade_thing.detectMultiScale(gray, 1.3, 1)
    
    for (x,y,w,h) in faces:
        print("Cat found: \n ",x,y,w,h)
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
        textlocation = x,y
        cv2.putText(img,'Cat',textlocation, font, fontScale,fontColor,lineType)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        
        

    cv2.imshow('Scanner ACTIVE',img)
    cv2.waitKey(10)

cap.release()
cv2.destroyAllWindows()

And that’s it. Thank you so much for watching and have a nice day :).