Computer Vision with OpenCV — Face-Eyes-Smile Detection

Koray Efe Yağmur
2 min readAug 25, 2021

--

Today ı had been a project for everyone. This project will show you some way for using OpenCV and detecting Face-Eyes-Smile.

But first of all you need to save some trained .xml documents to your project folder and after you need to try my codes which is staying down side.

What we need in this project ?

Pycharm

Python

OpenCV

Trained .xml files for what we want to detect.

and that's all.

And after you need to find which algorithm do you want to use. I used Haar model in this project. Haar model is not perfect but it can be usable.

Code inputs;

# Face-Eyes-Smile Recognition

# Importing the libraries
import cv2

# Loading the cascades
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')
upperbody_cascade = cv2.CascadeClassifier('haarcascade_upperbody.xml')

# Defining a function that will do the detections
def detect(gray, frame):
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 3)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 22)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2)

return frame # We return the image with the detector rectangles.

# Doing some Face Recognition with the webcam
video_capture = cv2.VideoCapture(0)
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
canvas = detect(gray, frame)
cv2.imshow('Video', canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

Code Output;

Output of my special code

And if you asking yourself where can ı find the trained datas then you need to follows this github link;

https://github.com/opencv/opencv/tree/master/data/haarcascades

If you need any help or information please contact with me on my linkedin accont.

Best

Koray

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Koray Efe Yağmur
Koray Efe Yağmur

Written by Koray Efe Yağmur

Im a Robotic and Mechanical Engineer. I will write some codes about robotics and share them with you. Just follow me!

No responses yet

Write a response