Face Recognition with Python

Face Recognition with Python

Hey, how you doing?

Today we will be doing our first coding tutorial here!!!✨✨

So, in this tutorial we gonna be building a face recognition program in python using an amazing module called openCV

What is opencv?

OpenCV is a library of programming functions mainly aimed at real-time computer vision. As you can see there is something there called Computer vision.. am gonna brief that now

Computer vision: Computer vision is a field of artificial intelligence that trains computers to interpret and understand the visual world. Using digital images from cameras and videos and deep learning models, machines can accurately identify and classify objects — and then react to what they “see.” Now am pretty sure we understand those, so lets code

First thing you need to do is open up your code editor and open your project directory in there Next we have to install OpenCV, to install type

pip install opencv-python

Then it should be done Next create a python file and save it as anything you want Now in the editor import opencv by typing

import cv2

Add this after that

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

What that does is that it loads up the datasets we are gonna be using, you can download them Here

Next add

img = cv2.imread('nn.jpg') faces = face_cascade.detectMultiScale(img, 1.3, 5)

in this part, we read our image from our project directory, as you can see the image name is nn.jpg

Next add

print(f'faces found {len(faces)}') print(img.shape) print('Cord', faces)

This just print the faces found in the image, the width, height and the coordinates

Next add

for (x,y,w,h) in faces: cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)

this loops the x,y,w,h in the faces variable. the x and y represent length and the w, h represents weight and height then cv2.rectangle is used to draw rectangle on a image. so it selects the img and look of the position of face and we added color green (0,255,0) and border thickness of 2

Next add

cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()

this will show the image and if any key is been pressed it closes the image You should be having this now

ts2.png

so now lets add the eye detection

below the cv2.rectangle add this

roi_face = img[y:y+h, x:x+w] eyes = eye_cascade.detectMultiScale(roi_face, 1.3, 5) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_face, (ex,ey),(ex+ew, ey+eh), (255,0,0), 2)

what this those is detect the eye on the face

so before we round up let add some text on the image

right after that code above, add this

font = cv2.FONT_HERSHEY_SIMPLEX text = cv2.putText(img, 'Face detected', (0, img.shape[0]), font, 2, (255,255,255), 2)

what this those is that the font variable selects the simplest font in cv2 and we created a text and we added the text face detected then included it right below the image and also set a color for the text and border weight

so if you follow up till now, you should be having this

devv.png

✨✨✨ If you have any questions comment them below and if you need the project instead check my Github

Thanks