AI – Python Computer Vision Tutorial with OpenCV
1. Objective – Python Computer Vision
In this Python tutorial, we will talk about Python Computer Vision and OpenCV. Moreover, we’ll see how to use Python to do basic tasks with OpenCV. Also, we will see detecting edges, drawing with Python OpenCV, detecting faces, and eye detection.
So, let’s start the Python Computer Vision tutorial.
2. What is Computer Vision in Python?
Computer Vision is a field of multiple disciplines that care about how computers can gain high-level understanding from digital images/videos. This is an attempt to automate tasks that the human visual system is able to perform. This is a process of acquiring, processing, analyzing, and understanding digital images, and extracting high-dimensional data from real world (to produce numerical/symbolic information.)
You must read Python AI Tutorial
Typical tasks involved Python Computer Vision are:
- Motion Analysis
- Scene Reconstruction
- Image Restoration
Fields related to Python Computer Vision:
- Artificial Intelligence
- Solid-state Physics
- Signal Processing
- Statistics, Optimization, Geometry
Some of the applications of Python Computer Vision:
- Automatic inspection in manufacturing applications
- Assisting humans in identification tasks (eg, species identification system)
- Controlling processes (eg, an industrial robot)
- Detecting events (eg, visual surveillance)
- Interaction (eg, input to the device for computer-human interaction)
- Modeling objects/ environments (eg, medical image analysis)
- Navigation (eg, autonomous vehicle)
- Organizing information (eg, indexing databases of images and image sequences)
3. OpenCV Python Computer Vision
Gary Bradsky started OpenCV at Intel in 1999. While it supports a gamut of languages like C++, Python, and more, and OpenCV-Python is an API for OpenCV to unleash the power of Python and the OpenCV C++ API at once.
Learn more about Python Library
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).
a. Install OpenCV Python
Before you can install OpenCV, make sure you have Python and NumPy installed on your machine.
You can download the wheel for OpenCV here (unofficially), so you don’t run into some DLL Hell:
Then, you can install this file using pip:
pip install [path_of_wheel_file]
b. Importing OpenCV in Python
Get to the IDLE and import OpenCV:
>>> import cv2
You can also check which version you have:
4. Python Computer Vision – Working with Images
Now that we’ve successfully installed OpenCV, let’s get started with it.
Have a look at Image Processing with Python SciPy & NumPy
Note that prior to this, we have moved to the directory that holds this image.
We can also pass a value for a flag, which is the second argument-
- cv2.IMREAD_COLOR- To load a color image neglecting existing transparency (default flag)
- cv2.IMREAD_GRAYSCALE- To load a grayscale image
- cv2.IMREAD_UNCHANGED- To load an image including an alpha channel
We can pass integers 1, 0, or -1.
If you pass an incorrect image path, this gives us no error, but print(img) gives us None.
Let’s revise Python Data Structures
b. Displaying Images in Python
The function/method cv2.imshow() lets us display an image in a window which fits itself to the size of the image. The first argument is the window name- a string; the second is the image.
>>> img=cv2.imread('py.jpg') >>> cv2.imshow('Python',img)
How about we display this in grayscale?
Notice that it let us have two windows at once because we didn’t try to name them the same thing.
Working in scripts, a call to waitKey(0) is beneficial. This is a keyboard-binding function/method with time in milliseconds. This function waits for certain milliseconds for a keyboard event, within which, if we press any key, the program continues. When we pass 0, we make it wait indefinitely for a keystroke. We can also make it wait for specific keys.
Let’s discuss Python Rename File
cv2.destroyAllWindows() is another function/method to destroy all windows we created. cv2.destroyWindow() destroys a specific window.
c. Writing Images in Python
For this, we have the function/method cv2.imwrite(). The first argument is the file name and the second is the image to save.
This saves the image in grayscale with the name ‘pygray.png’ in the current directory. This image is in the PNG format.
<matplotlib.image.AxesImage object at 0x0584C630>
((, <a list of 0 Text xticklabel objects>), (, <a list of 0 Text yticklabel objects>))
(35, 10, 0)
>>> img[y,x]=(0,0,255) #Setting pixel color to red; BGR scheme >>> region_of_interest=img[y:y+50,x:x+50] #Region of interest at (x,y) of dimensions 50x50 >>> cv2.imshow('image',img)
>>> cv2.imshow('edges',cv2.imread('edges_py.jpg')) >>> import numpy as np >>> fd=cv2.CascadeClassifier('C:\\Users\\Ayushi\\Downloads\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_frontalface_default.xml') >>> img=cv2.imread('mel.jpg') >>> gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Converting it to grayscale >>> faces=fd.detectMultiScale(gray,1.3,5) #Performing the detection >>> for (x,y,w,h) in faces: img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3) >>> cv2.imwrite('face_mel.jpg',img)
Here, you can see that it detected three eyes! one of which is her lips. Anyway, this is accurate many times, we happened to stumble upon one of the pictures that make the exception. Tell us in the comments below if this has happened to you.
So, this was all in Python Computer Vision Tutorial. Hope you like our explanation.
Hence, in this Python Computer Vision tutorial, we discussed the meaning of Computer Vision in Python AI. Also, we saw drawing with OpenCV, Detecting Edges, and Faces. Moreover, we learned eye detection in Computer Vision Python. Is this explanation helpful to you? Give your feedback in the comments.
See also –
Speech Recognition with Python AI