Python Tutorials: Computer Vision Programming with OpenCv in Python

Python Tutorials: In this part of Learning Python we Cover Computer Vision Programming with OpenCv In Python in Pandas in Python
Written by Paayi Tech |17-Oct-2020 | 0 Comments | 473 Views

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 is used in normal image enhancement activities, image recognition, face recognition, CNN, deep neural networks.

Still, there is plenty of research that is happening in the world on how to increase the efficiency of the 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 can all 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 are C++ and Java.

 

Types of Feed:

OpenCV allows multiple feeds like static images, 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 the NumPy array. Moreover, mostly images take the value as RGB, but the OpenCV returns the image as BGR. That is why when the 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:

  1. OpenCV by an image source
  2. OpenCV by a video source
  3. OpenCV by an IP camera source
  4. Transformations
  5. How do Pixels work?
  6. Face detection
  7. Eye Detection
  8. Color detection
  9. Object recognition algorithm
  10. Face Recognition


For this purpose, I am going to use the normal IDLE you can use whatever you want, but the jupyter notebook for this task is not appropriate because the 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:

Windows user

pip install OpenCV-python

 

Linux or Mac user

pip3 install OpenCV-python

or

python3 -m pip install OpenCV-python

 

After installation, import cv2 to check if all is ok:

Installation of OpenCV

Figure 1

Now you are good to go to learn about OpenCV.

 

How to make a self-made image:

As we know, the 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:

import cv2

import NumPy as np

 

 

a = [[0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0]

    ]

img = np.array(a)

 

cv2.imshow('Image',img)

cv2.waitKey(0)

 

We first imported cv2 and then the 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 the waitKey method stops the canvas unless the user closes it by himself.

 

The output that is generated is as follows:

How to make a self-made image in python

Figure 2

 

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 has to write the extension in the path parameter. It takes two 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:

import cv2

import NumPy as np

a = [[0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0],

     [0,0,0,0,0,0,0,0,0,0]

    ]

img = np.array(a)

cv2.imwrite('image.png',img)

 

 

 

After saving the image, we can see by visiting the folder that the image is saved or not:

How to save the image

Figure 1

And in our case, the image is saved as a 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 to an image. The source may be a video, or webcam feed, or an IP camera feed. We will see all of the source types 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:

import cv2

import NumPy as np

a = cv2.imread('logo.png')

cv2.imshow('image', a)

 

cv2.waitKey(0)

 

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 the image, and the output looks like this:

Locally stored Image

Figure 2

This is a 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 the URL by sending the request and then convert it into a NumPy array. After that, we will be able to display the image.

Following is the code to display a URL image:

import numpy as np

import urllib.request

import cv2

with urllib.request.urlopen("https://raw.githubusercontent.com/docker-library/docs/01c12653951b2fe592c1f93a13b4e289ada0e3a1/python/logo.png") as url:

    response = url.read()

img = np.asarray(bytearray(response), dtype="uint8")

img = cv2.imdecode(img, cv2.IMREAD_COLOR)

cv2.imshow('Image',img)

cv2.waitKey(0)

 

 

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 a 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:

Call The image as a website link

Figure 3

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 that can run the video with audio.

Following is the code to run the video with the help of OpenCV:

import NumPy as np

import cv2

cap = cv2.VideoCapture('movie.mkv')

while True:

    _,frame = cap.read()

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

cap.release()

cv2.destroyAllWindows()

 

So, after importing the modules, we call the function Video Capture and give the parameter of the movie path. When this is 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 the window gets destroyed.

 

Webcam feed:

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 the webcam feed as well for that, and we have to use py audio to get the voice of the user when the feed starts. So here is the code for the webcam feed.

import NumPy as np

import cv2

cap = cv2.VideoCapture(0)

while True:

    _,frame = cap.read()

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

 

 

cap.release()

cv2.destroyAllWindows()

 

The code of the webcam feed is similar to the video feed. However, there is a slight difference. At the point where the parameter path was given here, we gave an integer value of 0. So now, the question arises on what that integer value is.

The number of webcam in your computer is 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, then 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 you change the value from 0 to 1, then it will generate an error as follows:

Traceback (most recent call last):

  File "/home/lalatoofani/Desktop/Python Module/opencv/1.py", line 8, in

    cv2.imshow('frame',frame)

cv2.error: OpenCV(3.4.1) /io/OpenCV/modules/highgui/src/window.cpp:356: error: (-215) size.width>0 && size.height>0 in function imshow

 

This error means your camera or even your image is wrong; there may be some problem. In the case of the image, the path may be wrong, or the image is not found, and in the 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.





Login/Sign Up

Comments




Related Posts



© Copyright 2020, All Rights Reserved. paayi.com

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.