INTRODUCTION TO COMPUTER VISION WITH OPENCV AND REAL TIME FACE DETECTION IN PYTHON

Karan Choudhary
4 min readMay 22, 2020

Computer vision is the broad parent name for any computations involving visual content — that means images, videos, icons, and anything else with pixels involved.

How computer vision works

Machines interpret images very simply: as a series of pixels, each with their own set of color values. Consider the simplified image below, and how gray scale values are converted into a simple array of numbers:

IMAGE AND IT’S PIXEL VALUE

Computer Vision with OpenCV

For Python, this is a library of bindings with the aim to solve computer vision problems. This library uses NumPy and all its array structures convert to and from NumPy arrays. This also means we can integrate it easily with other libraries like SciPy and Matplotlib (these make use of NumPy).

1. Installing OpenCV Library

Before you install OpenCV library ,you should have Python Environment in your system with Numpy library.

Here are the ways to install it in Prompt

a)You can download the wheel for OpenCV here (unofficially), so you don’t run into some DLL Hell:

https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv

pip install [path_of_wheel_file]

b)pip install cv

2. Importing OpenCV in Python

Get to the IDLE and import OpenCV:

import cv

Computer vision with opencv— Video through Webcam

Lets start from the very basic of images and their operation with opencv library.

A) READING IMAGE

1.img=cv2.imread(“Computer_vision.jpg”)

B) DISPLAYING IMAGE

img=cv2.imread(‘Comuter_vision.jpg’)cv2.imshow(‘Our First Image’,img)

Displaying Image in Python-through OpenCV.

Similarly we can display this image through matplotlib.

import matplotlib.pyplot as pltplt.imshow(img,cmap=’hsv’,interpolation=’linear’)

<matplotlib.image.AxesImage object at 0x5084C158>

C) Saving /Writing Images in Python

cv2.imwrite(‘opencv_image.jpg’,img)

True

This saves the image with the name ‘opencv_image.jpg’ in the current directory of the system.

D) Edge detection

import cv2
import numpy as np
img = cv2.imread(“image.jpg”, 0)
cv2.imwrite(“canny.jpg”, cv2.Canny(img, 200, 300))
cv2.imshow(“Original image”,img)
cv2.imshow(“canny”, cv2.imread(“c.jpg”))
cv2.waitKey()
cv2.destroyAllWindows()

Output:

Original Image and edge detection of Original image

Face Detection in Python Computer Vision

Before implementing code you should know how to make haarcascade

1.Import library and loading the cascades which should be present in our folder directory.

#IMPORT LIBRARY
import cv2
import numpy as np
# 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')
noseCascade = cv2.CascadeClassifier('Nariz.xml')

2.Making a function to detect various face and text the output for the detection on the screen.

# Defining a function that will do the detections
def detect(gray, frame):
faces = face_cascade.detectMultiScale(gray,1.3,5)
cv2.putText(frame, “webcam working: {}”.format(‘yes’), (10, 20), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#FACE DETECTION
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]
cv2.putText(frame, “face Detect: {}”.format(‘yes’), (400, 50), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#EYES DETECTION
ey es = eye_cascade.detectMultiScale(roi_gray, 1.1, 18)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.putText(frame, “eye Detect: {}”.format(‘yes’), (400, 20), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#SMILE DETECTION
smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 18)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2)
cv2.putText(frame, “smile Detect: {}”.format(‘yes’), (400, 150), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#NOSE DETECTION
nose = noseCascade.detectMultiScale(roi_gray, 1.1,10)
for (nx, ny, nw, nh) in nose:
cv2.rectangle(roi_color, (nx, ny), (nx+nw, ny+nh), (0,255, 255), 1)
cv2.putText(frame, “nose Detect: {}”.format(‘yes’), (50, 50), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)


cv2.putText(frame, “enter q to exit: {}”.format(‘?’), (25, 150), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)

return frame

3.Capturing video through webcam and press “q” for exit.

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()

COMPLETE PYTHON CODE FOR FACE DETECTION

#PYTHON FACE DETECTION
#KARAN CHOUDHARY
#FACE,EYES,NOSE,SMILE DETECTION
#IMPORT LIBRARY
import cv2
import numpy as np
# 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’)
noseCascade = cv2.CascadeClassifier(‘Nariz.xml’)
# Defining a function that will do the detections
def detect(gray, frame):
faces = face_cascade.detectMultiScale(gray,1.3,5)
cv2.putText(frame, “webcam working: {}”.format(‘yes’), (10, 20), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#FACE DETECTION
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]
cv2.putText(frame, “face Detect: {}”.format(‘yes’), (400, 50), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#EYES DETECTION
eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 18)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.putText(frame, “eye Detect: {}”.format(‘yes’), (400, 20), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#SMILE DETECTION
smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 18)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 2)
cv2.putText(frame, “smile Detect: {}”.format(‘yes’), (400, 150), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
#NOSE DETECTION
nose = noseCascade.detectMultiScale(roi_gray, 1.1,10)
for (nx, ny, nw, nh) in nose:
cv2.rectangle(roi_color, (nx, ny), (nx+nw, ny+nh), (0,255, 255), 1)
cv2.putText(frame, “nose Detect: {}”.format(‘yes’), (50, 50), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
cv2.putText(frame, “enter q to exit: {}”.format(‘?’), (25, 150), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 255), 5)
return frame
# Doing some Face Recognition with the webcam
#We can do this by video also by
#video_capture = cv2.VideoCapture(“Name of the video with mp4,mov format”)
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()

Screenshot of the code:

Output of the code:

Face Detection

THANKS!!!!

--

--