Now we will discuss computer vision. Computer vision is a very vast field. It is an integral part of Convolutional Neural Networks, robotics, etc. Computer vision used in normal image enhancement activities, image recognition, face recognition, CNN, deep neural network.
Still, there is plenty of research that is happening in the world on how to increase the efficiency of image recognition technique.
In python, there is a library known as OpenCV which means Open Source Computer Vision. So, it’s good that this is open source.
In this library we can perform different functions like scaling the image, making the image sharper, thresholding the image, making the image flip, edge detection of the image, morphological changes of the image, face detection eye detection, etc. This all can be done with the help of OpenCV. Open CV is the basics of the things if you have to do anything with the images the first step is to use OpenCV. There are image libraries like Pillow in python which gives image functionality, but it is not as vast as OpenCV you can only perform changing colors. Moreover, resizing the image but OpenCV provides much more functionality than this.
In Other Languages:
OpenCV not only presents in a python programming language but also in other programming languages that is C++ and Java.
Types of Feed:
OpenCV allows multiple feeds like from static image, from the live video source, running a video, or running an IP camera. Each source related to the image can be run on OpenCV.
How It works:
OpenCV mainly works on NumPy when the image is printed than NumPy is generated. Each pixel is an element of NumPy array. Moreover, mostly image take the value as RGB, but the OpenCV returns the image as BGR. That is why when OpenCV image is plotted into matplotlib or shown by some other image library the colors of the image get converted. So before projecting the image, the image should be converted into RGB.
So, in OpenCV we are going to do some of these following tasks:
- OpenCV by an image source
- OpenCV by a video source
- OpenCV by an IP camera source
- How do Pixels work?
- Face detection
- Eye Detection
- Color detection
- Object recognition algorithm
- Face Recognition
For this purpose, I am going to use the normal IDLE you can use whatever you want, but jupyter notebook for this task is not appropriate because jupyter notebook does not allow inline displaying of OpenCV images.
Installation of OpenCV:
To install OpenCV, we have to run the following command in our terminal:
After installation import cv2 to check if all is ok:
Now you are good to go to learn about OpenCV.
How to make a self-made image:
As we know that OpenCV image is made up of NumPy arrays so we will initialize the NumPy array of all containing zero value. These zero values represent the black pixels of an image. So, when we convert and subject that NumPy array a black box show be generated as an output:
We first imported cv2 and then NumPy array module and then made a nested list of zero and converted into NumPy. After converting when we subject this NumPy array to the imshow method. The imshow method is to show the image, and waitKey method stops the canvas unless the user close it by himself.
The output that is generated is as follows:
How to save the image?
We have seen how to save the image. However, sometimes there is a need to save the image too. To save the image OpenCV provides built-in functionality which is imwrite(). This function can save the image in any format a user wants whether it is JPG, TIFF or PNG. The user have to write the extension in the path parameter. It takes 2 arguments; one is the path where to save, and the other one is the object of an image.
Now we will see how to save an image:
After saving the image we can see by visiting the folder that image is saved or not:
And in our case the image is saved as PNG extension successfully.
Adding The source to display:
As we discussed earlier, there are lots and lots of ways to add the source into the OpenCV to show the image. It can be an image, or it can be the vide. The image can reside locally in your computer, or it can be a link of an image. The source may be a video, or webcam feed or an IP camera feed. We will see all of the source type one by one.
Locally Store Image:
It is straightforward to call locally store the image in an OpenCV. We have to use the function of imread, that reads the image, and then we will be able to display that image. I have locally stored the logo of python. I am going to display that image in my console as follows:
After importing the modules, we call the imread function and pass the path as an argument. After that we show the image by creating the window the title of the window is image and the output looks like this:
This is the simple method to call an image.
Call The image as a website link:
This is not as simple as calling the locally stored image. This is a little bit tricky we first have to open URL by sending the request and then convert into NumPy array after that we will be able to display the image.
Following is the code to display a URL image:
So, after importing the modules, we requested to open the URL. Moreover, then read the response this response is in bytes so we first have to convert it into NumPy array so that OpenCV can recognize that it is an image. After that, we decode the bytes and show the image. The response is the same as we have displayed earlier figure 2:
Locally Resides Videos display:
We cannot just only display the images but can also display the videos. However, this video is not to watch because there is no audio in it. The sole purpose of this is to analyze the frames. However, we can do it by combining different modules to make a product which can run the video with audio.
Following is the code to run the video with the help of OpenCV:
So, after importing the modules, we call the function Video Capture and give the parameter of the movie path. When this will be initiated the frames will be generated one after the other. So, we will initialize the while loop and will continue until the frames are present.
Here we mentioned a condition if waitKey of (1) means if q is pressed then the loop should break and the cap gets released, and window get destroyed.
Just like the video feed, we can also have the webcam feed in OpenCV. The quality of an image will be decided by the camera that you are using. We cannot hear the voices in webcam feed as well for that we have to use py audio to get the voice of the user when feed starts. So here is the code for the webcam feed.
import numpy as np
The code of the webcam feed is similar to the video feed. However, there is a slight difference. At the point where parameter path was given here, we gave an integer value of 0. So now the question arises what that integer value is.
The number of webcam in your computer are the integer value gets higher. Mostly the default camera of your laptop has the value 0 if there is another camera attached to your computer than the value will be 1. So, you have to tweak this value if your camera is not working. For example, I have only one camera now. If change the value from 0 to 1 then it will generate an error as follows:
This error means your camera or even your image is wrong; there may be some problem. In case of the image, the path may be wrong, or image is not found, and in case of the video, the camera integer is not right so in that case you have to tweak the integer value to get the system right.