What is OpenCV?
OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture, and analysis including features like face detection and object detection.
Here we are focused on face detection(frontal face).
Before that, u may have some confusion between what are computer vision and image processing...Let me explain
Image processing deals with image-to-image transformation. The input and output of image processing are both images.
Computer vision is the construction of explicit, meaningful descriptions of physical objects from their image. The output of computer vision is a description or an interpretation of structures in a 3D scene.
To use OpenCV first you should install OpenCV on your device. If you have installed it already let's go...
We need a trained model so that we can use it to detect your faces. you can use the haar cascade data from
copy the whole thing and paste it into an XML file in the editor you are using and name it 'haarcascade_frontalface_default.xml'
#importing required dependent cv2 from OpenCV.
import cv2
from random import randrange
#using CascadeClassifier from cv2 to get trained face data.
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 0 for default webcam - It opens the webcam
webcam = cv2.VideoCapture(0)
while True:
#reading data
successful_frame_read, frame = webcam.read()
#converting the instant images into grayscale
grayscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#finding the faces using the trained model
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
#locating coordinates on the screen and drawing rectangle around our face
for (x, y, w, h) in face_coordinates:
cv2.rectangle(frame, (x, y), (x+w, y+h), (randrange(256), randrange(256), randrange(256)), 10)
cv2.imshow('My face', frame)
key = cv2.waitKey(1)
if key == 81 or key== 113:
break
webcam.release()
ok, you might ask, what is the Idea behind it? How our faces are detected?
It uses a technique called haar cascade. I encourage you to watch the video below if you are interested in knowing it better.
outro:
There are plenty of applications of OpenCV, It's just a basic tutorial. We can use OpenCV for object detection, car detection, and whatnot.
explore yourself in OpenCV documentation and come up with new ideas.
references:
Thank you for reading😊
Happy Learning
-JHA