import cv2
video_capture = cv2.VideoCapture(0)
# Loading the required haar-cascade xml classifier file, classifies face from non face
haar_cascade = cv2.CascadeClassifier('Haarcascade_frontalface_default.xml')
print(haar_cascade)
while True:
# Capture frame-by-frame
ret, img = video_capture.read()
# Converting image to grayscale, detection model sees it as grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Applying the face detection method on the grayscale image
faces_rect = haar_cascade.detectMultiScale(gray_img, 1.1, 9)
# Iterating through rectangles of detected faces, displaying the rectangle
for (x, y, w, h) in faces_rect:
cv2.rectangle(img, (x, y), (x+w, y+h), (70, 0, 255), 2)
# Display the resulting frame
cv2.imshow('Video', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
# simple python code: face recognition
import cv2
import numpy as np
detect = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture('image.jpg')
check, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detect.detectMultiScale(gray,1.2,5)
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Face Detect', img)
cv2.waitKey(600000)
cam.release()
cv2.destroyAllWindows()