Detecting Eyes using OpenCV and Numpy Python Libraries

Whatever topic you read or learn, experiencing it in a practical way is much important. If you are planning to learn Python libraries like OpenCV and Numpy, then these simple 12 lines of code are easy to practice and experience the real-time use of these libraries.

Let’s start exploring the required libraries for detecting eyes.

OpenCV:

OpenCV is an image processing library created by Intel. It provides simple and useful ways to read and write images. The OpenCV library allows you to run Computer Vision algorithms efficiently in real-time. OpenCV is a popular Computer Vision library that has many built-in tools for image analysis. One of the main advantages of OpenCV is that it is highly optimized and available on almost all platforms. OpenCV reads images in BGR format.

NumPy:

Numpy is a library for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays. A NumPy array is similar to the list. We can cast a list to a NumPy array by first importing it. Numpy arrays contain data of the same type, we can use attribute “dtype” to obtain the data type of the array’s elements.

Let’s start the coding with simple lines of code. Here I am using Jupyter Notebook to code!!

Step-1: Import required libraries

Step-2: Download the cascade file available in the below link

Click on the Raw option, after that press Ctrl + S to download the file to your computer.

Upload this file in Jupyter Notebook and use it in the code as shown below:

Cascade Classifier function is trained from a lot of positive and negative images. It is further used to detect objects in other images.

Step-3: Read and resize the image, copy the image and some functions are used as shown below:

cv2.imread(“kid.jpg”) loads the image and shape defines the dimensions of the image.

To resize the image, we can use cv2.resize() function as shown above.

cvtColor() is used to convert an image from one color space to another. Here img_rgb is the image whose color space has to be changed.

cv2. rectangle() method is used to draw a rectangle on any image.

Step-4: Display multiple images and display the specified image.

np.hstack() is used to display multiple images in one window.

cv2.imshow() displays the specified image.

cv2.waitkey(0) will display the window infinitely until any key is pressed.

Follow this page for more related articles:

https://likhithakakanuru.medium.com/

Data Science Enthusiast, Technical Writer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store