#collect 100 samples of your face from webcam input 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)
#save file in specified directory with unique name file_name_path = ‘./faces/user/’ + str(count) + ‘.jpg’ cv2.imwrite(file_name_path, face)
#put count on images and display live count 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: break
import cv2 import numpy from os import listdir from os.path import isfile, join
#get the training data we previously made data_path = ‘./faces/user/’ onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
#create arrays for training data and labels Training_Data, Labels = [], []
#open training images in our datapath #create a numpy array for training data
for i, files in enumerate(onlyfiles): image_path = data_path + onlyfiles[i] images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) Training_Data.append(numpy.asarray(images, dtype = numpy.uint8)) Labels = numpy.append(Labels,i)
#create a numpy array for both training and labels Labels = numpy.asarray(Labels, dtype = numpy.int32)
#initialize facial recognizer #model=cv2.face.createLBPHFaceRecogniser() #Note: for opencv 3.0 use cv2.face.createLBPHfacerecognizer() #pip install opencv-contrib-python #model= cv2.createLBPHfacerecognizer()
Saketh_model=cv2.face_LBPHFaceRecognizer.create() #train model Saketh_model.train(numpy.asarray(Training_Data), numpy.asarray(Labels)) print(“Model trained successfully”)
import cv2 import numpy import smtplib import pywhatkit as kt import getpass as gp import os import subprocess import time #creating function to send mail automatically def sendemail():
def face_detector(img, size=0.5): #convert image to grayscale gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces = face_classifier.detectMultiScale(gray, 1.3, 5) if faces is (): return img, []
for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2) roi = img[y:y+h,x:x+w] roi = cv2.resize(roi,(200,200)) return img, roi #open webcam cap =cv2.VideoCapture(0)
while True:
ret, frame = cap.read() image, face = face_detector(frame)
try: face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
#pass face to prediction model #”results” comprises of a tuple containing the label and the confidence value results = Saketh_model.predict(face)
if results[1] < 500: confidence = int( 100*(1 — (results[1])/400))
display_string = str(confidence) + ‘% confident it is User’