Commit 49a7fec1 authored by I.K Seneviratne's avatar I.K Seneviratne

Merge branch 'IT17098960_RSD_FERNANDO' into 'QA_RELEASE'

It17098960 rsd fernando

See merge request !27
parents 271d584c 75eb0f13
import cv2
import os
import numpy as np
#This module contains all common functions that are called in tester.py file
#Given an image below function returns rectangle for face detected alongwith gray scale imagess
def faceDetection(test_img):
gray_img=cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)#convert color image to grayscale
gray_img=cv2.normalize(gray_img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
gray_img=(255*gray_img).astype(np.uint8)
gray_img=cv2.fastNlMeansDenoising(gray_img);
face_haar_cascade=cv2.CascadeClassifier('HaarCascade/haarcascade_frontalface_default.xml')#Load haar classifier
faces=face_haar_cascade.detectMultiScale(gray_img,scaleFactor=1.32,minNeighbors=5)#detectMultiScale returns rectangles
return faces,gray_img
#Given a directory below function returns part of gray_img which is face alongwith its label/ID
def labels_for_training_data(directory):
faces=[]
faceID=[]
for path,subdirnames,filenames in os.walk(directory):
for filename in filenames:
if filename.startswith("."):
print("Skipping system file")#Skipping files that startwith .
continue
id=os.path.basename(path)#fetching subdirectory names
img_path=os.path.join(path,filename)#fetching image path
print("img_path:",img_path)
print("id:",id)
test_img=cv2.imread(img_path)#loading each image one by one
if test_img is None:
print("Image not loaded properly")
continue
faces_rect,gray_img=faceDetection(test_img)#Calling faceDetection function to return faces detected in particular image
if len(faces_rect)!=1:
continue #Since we are assuming only single person images are being fed to classifier
(x,y,w,h)=faces_rect[0]
roi_gray=gray_img[y:y+w,x:x+h]#cropping region of interest i.e. face area from grayscale image
faces.append(roi_gray)
faceID.append(int(id))
return faces,faceID
def train_classifier(faces,faceID):
face_recognizer=cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces,np.array(faceID))
return face_recognizer
#Below function draws bounding boxes around detected face in image
def draw_rect(test_img,face):
(x,y,w,h)=face
cv2.rectangle(test_img,(x,y),(x+w,y+h),(255,0,0),thickness=4)
#Below function writes name of person for detected label
def put_text(test_img,text,x,y):
cv2.putText(test_img,text,(x,y),cv2.FONT_HERSHEY_DUPLEX,1,(255,0,0),3)
#Save video frames
def extractAndSaveFrames():
vidcap = cv2.VideoCapture('IT17098960.mp4')
success, image = vidcap.read()
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success, image = vidcap.read()
print('Read a new frame: ', success)
count += 1
import cv2
from .faceRecognition import faceDetection, train_classifier, labels_for_training_data, draw_rect, put_text
#This module takes images stored in diskand performs face recognition
test_img=cv2.imread('TestImages/frame0.jpg')#test_img path
faces_detected,gray_img=faceDetection(test_img)
print("faces_detected:",faces_detected)
#Comment belows lines when running this program second time.Since it saves training.yml file in directory
faces,faceID=labels_for_training_data('trainingImages')
face_recognizer=train_classifier(faces,faceID)
face_recognizer.write('trainingData.yml')
# For subsequent runs
face_recognizer=cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read('trainingData.yml')#use this to load training data for subsequent runs
name={0: "IT17138000", 1: "IT17100908", 2: "IT17098960"}#creating dictionary containing names for each label
file = open("attendance.txt", "w")
for face in faces_detected:
(x,y,w,h)=face
roi_gray=gray_img[y:y+h,x:x+h]
label,confidence=face_recognizer.predict(roi_gray)#predicting the label of given image
print("confidence:",confidence)
print("label:",label)
draw_rect(test_img,face)
predicted_name=name[label]
if(confidence>90):#If confidence more than 37 then don't print predicted face text on screen
continue
file.write(predicted_name)
put_text(test_img,predicted_name,x,y)
file.close()
resized_img=cv2.resize(test_img,(700,700))
cv2.imshow("face dtecetion tutorial",resized_img)
cv2.waitKey(0)#Waits indefinitely until a key is pressed
cv2.destroyAllWindows
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment