CODE FOR FACE DETECTION USING OpenCV #1

Hi, guys, I am here to share the code for face detection using python and openCV.
Image result for python and openCV

Step1-
Install libraries of openCV, as well as openCV-contrib-python

Step2-
Use this code to train your model.
in CascadeClassifier paste the location for haarcascade_frontalface_default.xml
(keep in mind that while pasting location change the from "\" to "/" )
provide the path for your image file_name_path

FOR #2(https://t.co/Z8Snwtiam0)

import cv2
import numpy as np
face_classifier=cv2.CascadeClassifier('C:/Users/PycharmProjects/open/venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
def face_extractor(img):

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)

    if faces is():
        return None
    for(x,y,w,h) in faces:
        cropped_face = img[y:y+h,x:x+w]

    return cropped_face



cap = cv2.VideoCapture(0)
count = 0
while True:
    ret, frame = cap.read()
    if face_extractor(frame) is not None:
        count+=1
        face = cv2.resize(face_extractor(frame),(200,200))
        face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)

        file_name_path = "C:/python/open cv/faces/user"+str(count)+ '.jpg'

        cv2.imwrite(file_name_path,face)

        cv2.putText(face,str(count),(50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
        cv2.imshow('Face Cropper',face)
    else:
        print('face not found')
        pass    if cv2.waitKey(1)==13 or count==100:
        breakcap.release()
cv2.destroyAllWindows()
print("collecting sample complete")

I will soon provide you another code where it will detect the face you have trained; if you have any problem you can contact me I will help you