Computer Vision Programming with OpenCv in Python

In this part of Learning Python we Cover Computer Vision Programming with OpenCv In Python in Pandas in Python
Written by Paayi Tech |24-Apr-2019 | 0 Comments | 88 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 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:

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

 

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

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

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 image is saved or not:

How to save the image

Figure 1

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:

 

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

Locally stored Image

Figure 2

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:

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 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 which 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 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.

 

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

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

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 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.



Login/Sign Up

Comments




Related Posts



© Copyright 2019, 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.