Posts

Showing posts with the label Python Face Detection

Face Detection Script in Python

Method # 1 import cv2 #pip install opencv-python import pathlib # take the cascade file for frontal face detection cascade_path = pathlib.Path(cv2.__file__).parent.absolute()/"data/haarcascade_frontalface_default.xml" # print(cascade_path) # training classifier on the cascade file clf = cv2.CascadeClassifier(str(cascade_path)) # face detection in camera camera = cv2.VideoCapture(0) # 0 is for default camera # face detection in video # camera = cv2.VideoCapture("cud.mp4") while True: _, frame = camera.read() # _ ignores the first return value # some Python functions return multiple values, often we don’t need all of them. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = clf.detectMultiScale( gray, scaleFactor = 1.1, minNeighbors=5, # higher this number, less faces are found minSize = (30, 30), # size of window flags = cv2.CASCADE_SCALE_IMAGE)