In this section, we will discuss how to get the feed of an IP camera. Most people do not have a webcam on their PC, but all have a smartphone, so it’s time to use smartphone smartly. Have you ever thought to get the camera feed in your computer, you can do lots of stuff with that like screencast it etc.
However, before jumping into the code, we have to install something on our mobile phone as well. Go to the google play store or app store and download IP Webcam the logo of that application looks like this:
After downloading open the application and scroll down to the bottom where there is an option of starting a server. At the bottom of the screen after the camera opens there will be a URL which may look like this http://10.*.*.125:8080:
After importing all the modules, we made a variable or img_url in which the URL that we have copies was paste and added /shot.jpg. This the full URL of the frame that we will get. These frames will be continuous means frame will come one after the other, so we started an infinite while loop which loops over the frame till the camera is in running position. Then just like in the case of an image from the link we open the URL and extract the bytes and then decode the bytes to an image array so the python open cv can recognize it.
Finally, when a user wants to stop, he will press q and loop will be break window will be destroyed.
This feature enables anyone to analyze live webcam feed if he does not a webcam. Moreover, if you have a spare mobile, you can make your own security system by mounting the mobile somewhere no one can see it. However, it should not be used unethically.
Moreover, this will work only on the same LAN not on internet layer so your mobile and PC must be connected to the same WIFI.
BGR to RGB:
As we have discussed earlier, the OpenCV convert the image when loaded into BGR, but mostly the like matplotlib cater it as RGB which make the color difference, so we have to convert image from BGR to RGB.
Let’s take an example to plot the image loaded from OpenCV and then plotted on matplotlib:
The generated result can be seen in figure 2:
As we can see colors are distorted. So, to convert BGR to RGB following method is applied:
After loading the image, we call OpenCV CVT Color function which converts the image to RGB now if we plot the image look like this again:
Transforming to grayscale:
Grayscale is another transformation in which we convert the color image to grayscale. First, we will see how to do this and then we will examine what happened when we convert the image to grays scale.
So, the image that is generated is as follows:
So now we will see what happened by printing the image before the conversion and after the conversion:
Also, the output looks like this:
Before converting each pixel is represented as a list of 3 elements. Now the question arises why there are three elements. The three elements represent Red Blue and Green respectively after the conversion the 3-channel image is converted into one channel image now each nested list represent the rows of pixel and rows contain only the value that is either closer to white or closer to black. It is what happens when we convert the image to grayscale its pixels get converted from 3 channels to 1 channel.
Resizing The image:
Open cv provides the functionality to resize the image. The resize image function takes two parameters. 1st the object of the image and second the tuple of width and height. Following is the code to resize the image.
We had seen earlier that when we printed the image, a huge array was formed that was not even able to printed fully, but when we resize the image the array will be printed fully on screen as follows:
There are 10 rows and 10 columns; each element represents one pixel.