OpenCV uses cascades that is “a waterfall or series of waterfalls.” As an order of waterfalls, OpenCV cascade tackles the question of faces recognition into multiple phases. For each phase, it executes a very rude and rapid test. If the first check passes, it executes a softly more detailed test, and so on. The algorithm could have 30 to 50 of these phases or cascades, and if all phases pass it will identify a face. Pending the first phases the most frames taken in consideration will return a negative and this is a benefit, since the algorithm will not lose time controlling all 6,000 features on it. With the current calculation power face recognition can now be realized in real time.
Cascades in Python
The cascades are just a several of XML files holding OpenCV data used to identify objects. We can initialize our code with the cascade file for our porpouse, and then it works for us. OpenCV provides us a large number of just prepared cascades files for finding faces , eyes , hands , legs and body. There are also cascades for non-human things. In the following example there is the inclusion of haarcascade_frontalface_default.xml file
Python Code for Face Recognition
import cv2
import sys
import os
import time
#cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite(‘snap.jpeg’,frame)
os.system(“sendgmailc.exe”)
# Display the resulting frame
cv2.imshow(‘Video’, frame)
if cv2.waitKey(33) >= 0:
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Categories