AI — Python Computer Vision Tutorial with OpenCV
AI — Python Computer Vision Tutorial with OpenCV
What is Computer Vision in Python?
Computer Vision is a field of multiple disciplines that care about how computers can gain a 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 the real world (to produce numerical/symbolic information.)
Typical tasks involved Python Computer Vision are:
- Recognition
- Motion Analysis
- Scene Reconstruction
- Image Restoration
Fields related to Python Computer Vision:
- Artificial Intelligence
- Solid-state Physics
- Neurobiology
- Signal Processing
- Statistics, Optimization, Geometry
- 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)
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.
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. Installing OpenCV in 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:
- >>> cv2.__version__
‘3.4.3’
Python Computer Vision — Working with Images
Now that we’ve successfully installed OpenCV, let’s get started with it.
a. Reading Images in Python
To read an image, we have the built-in function/method imread().
- >>> img=cv2.imread(‘py.jpg’)
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.
- >>> img=cv2.imread(‘py.jpg’,0)
If you pass an incorrect image path, this gives us no error, but print(img) gives us None.
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?
Python Computer Vision — Displaying Images in Python
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.
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.
- >>> cv2.imwrite(‘pygray.png’,img)
True
This saves the image in grayscale with the name ‘pygray.png’ in the current directory. This image is in the PNG format.
d. Displaying Images With Matplotlib
- >>> import matplotlib.pyplot as plt
- >>> plt.imshow(img,cmap=’gray’,interpolation=’bicubic’)
<matplotlib.image.AxesImage object at 0x0584C630>
- >>> plt.xticks([]),plt.yticks([])
(([], <a list of 0 Text xticklabel objects>), ([], <a list of 0 Text yticklabel objects>))
- >>> plt.show()
Drawing with OpenCV Python Computer Vision
a. Drawing Lines in Python
Can we even draw with Python? Let’s begin with a simple line. This takes the starting and ending coordinates.
- >>> import numpy as np
- >>> img=np.zeros((512,512,3),np.uint8)
- >>> cv2.line(img,(0,0),(511,511),(255,0,0),5)
b. Drawing Rectangles in Python
We can draw a rectangle with the rectangle() method/function. This takes the top-left corner and bottom-right corner of the rectangle.
- >>> cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
c. Drawing Circles in Python
We have the circle() method/function for this. This takes the coordinates and the radius.
- >>> cv2.circle(img,(447,63),63,(0,0,255),-1)
d. Drawing Ellipses in Python
We can pass multiple arguments to the ellipse() method/function- center location (x,y), axes lengths, angle, startAngle, and endAngle.
- >>> cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
- >>> cv2.imshow(‘image’,img)
e. Drawing Polygon in Python
The polylines() method/function takes the coordinates of vertices.
- >>> pts=np.array([[10,5],[50,70],[250,360],[400,400]],np.int32)
- >>> pts=pts.reshape((-1,1,2))
- >>> cv2.polylines(img,[pts],True,(0,255,255))
f. Putting Text in Images
Finally, let’s see how we can add text to this image.
- >>> font=cv2.FONT_HERSHEY_SIMPLEX
- >>> cv2.putText(img,’OpenCV’,(10,500),font,4,(255,255,23),2,cv2.LINE_AA)
- >>> cv2.imshow(‘image’,img)
Basic Operations on Images
Let’s demonstrate this through one code.
- >>> import cv2
- >>> img=cv2.imread(‘py.jpg’)
- >>> y,x=100,50
- >>> (b,g,r)=img[y,x] #Reading color values at y, x positions
- >>> b,g,r #Printing color values to screen
(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(‘ROI’,region_of_interest)
- >>> region_of_interest[:,:]=(66,22,78) #Setting pixels to new color
- >>> cv2.imshow(‘New Image’,img)
Detecting Edges in Python Computer Vision
Edges of objects are how we remember them. We can choose to display only object edges in OpenCV with the function/method Canny().
- >>> import numpy as np
- >>> img=cv2.imread(‘py.jpg’)
- >>> cv2.imwrite(‘edges_py.jpg’,cv2.Canny(img,602,315))
True
- >>> cv2.imshow(‘edges’,cv2.imread(‘edges_py.jpg’))
Detecting Faces in Python Computer Vision
Let’s come to the fun part! OpenCV will also let us detect faces in pictures! Let’s take a sample image. (Can you guess who that is?)
Now, we will use the Haar cascade classifier to do this. We need the haarcascade_frontalface_default.xml file for this; you can search for this on your machine.
- >>> import numpy as np
- >>> img=cv2.imread(‘py.jpg’)
- >>> cv2.imwrite(‘edges_py.jpg’,cv2.Canny(img,602,315))
True
- >>> 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)
True
You can see that this drew a blue square around the face in the picture. Now, let’s try detecting her eyes.
Eye Detection in Python Computer Vision
Now, one last thing we’d like to discuss is detecting eyes. We use the Haar classifier for this too.
- >>> eye_cascade=cv2.CascadeClassifier(‘C:\\Users\\Ayushi\\Downloads\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_eye.xml’)
- >>> img=cv2.imread(‘mel.jpg’)
- >>> gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- >>> eyes=eye_cascade.detectMultiScale(gray,1.03,5)
- >>> for (ex,ey,ew,eh) in eyes:
- img=cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
- >>> cv2.imwrite(‘mel_eyes.jpg’,img)
True
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.
Conclusion — Python Computer Vision
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.