Tuesday, January 16, 2018

4. Reading Video File or Video Stream over a network using Opencv

In this post we will be learning how to read video file and process it frame by frame. how to acess your webcam or usb webcam using opencv.


What you'll require?

1. imutils package
2. USB camera or IP camera


I am assuming you have conda installed in your system and conda path is configured.
if not then follow on older posts or you can check my blog archives from right side of the page or very below if you are on mobile device.


To install imutils package use the following command.

1. conda install -c anaconda pip


once first command is completed proceed to 2nd.

2. pip install imutils


Once you install imutils we are ready to do our programming.



Make a new File namely video_process.py
and import following modules to our new file.






Important thing to learn here is that when you import any package like cv2 it becomes an object or a variable in your program from which you can access the methods and properties.

Here imutils is one module, module includes video as a class and VideoStream is our object.
we are also importing main imutils class to have access to simple image manipulation. which we will see very soon.


now that we are ready lets open a video stream from any source.

source can be different, below image shows examples.





You can chose any source to open your video. i'll be using src=0 my local leptop webcam.
  • In many cases multiple camera is connected so the src=0 can be changed to 1 or 2 depending on number of cameras connected as same time, by default 0 works in all condition.

now we need a logic to grabs frame from our video source

we will be using while loop here.




Notice that i have commented other two capture variable lines, because i'll be using src=0 only.


Run this program and you will see your VideoStream of your leptop webcam.





Happy coding.


Practice : Try opening any .mp4 or .avi file.





tags: imutils,opencv,python,python opencv, imutls opencv, videostream, read video stream opencv, capture video stream opencv, stream video opencv.




3. Read, write and display image from your computer using image processing.

What you'll be learning in this post.

1. How to read images from your computer with opencv.
2. How to display images from your computer with opencv.
3. How to write images to your computer with opencv.


Lets get started,

i'm using the old file that we have used in previous post namely opencv_test.py, you can find the post here.
and you will need any image file, i have named it as test.jpg. 





So lest start from the first.

1. How to read images from your computer with opencv.




  • here we are using opencv's function imread to read our image from any directory. and we are assigning it to the img variable.

This is how we read our image.


2. How to display image that we have read in our program.


so to do that we will be using opencv's imshow function, which accepts two parameters.

  1. Opencv diplay name (this is to differentiate more than one windows of opencv )
  2. Image variable 




Here we have two new functions waitKey() and destroyAllWindows().

The reason why we are using these function is that the waitKey function will hold the
diplay window until any key is press from keyboard, we can specify any keyboard key like 'q' to quit the program. we will see it in future posts.

The destroyAllWindows() will destroy all the opencv windows that is opened now.


Now you can run and test your program. if you dont know how to do that make sure you read my previous posts.


2. How to save image from our program to your computer.


in this section we will learn new function that will convert our color image to black and white.

cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)



This is the final block of code that we will be running bu let me explain you first.

here i have used two new functions cvtColor(), imwrite()

  • cvtColor takes input image variable as first parameter and opencv specified property to convert color to B&W image.

  • imwrite take file name to be saved as first parameter, ans image variable as second.
  • i have used another window so we can see the image color and black and white at same time but the difference is that i have specified different window names to each window.



Run this program ans see what is the output.






Hope you liked the post, let me know in comments.






Tags: opencv, opencv python, opencv programming, python, imread, imwrite, imshow.

2. Writing your first opencv python program.

2. Writing your first opencv python program.

First of all create one directory for your opencv programs.

Lets say OpencvPython, Now we will save our every program into this Folder, i'll tell you why we are doing this later on.

Now open your favorite Text Editor, i suggested few in my last post. I'll be using sublime text.

write following line and save it as opencv_test.py in the directory OpencvPython that we have created earlier.

Make sure there should not be any space before import or you might get error of indentation by python compiler.



Once you have saved the file now its time to compile and run our Program.
To do so, go to you python program directory in windows explorer.

Now before you run your program make sure your file opencv_test.py is there in you directory. if not then make sure you have saved it earlier if not then save as again in that directory.


I assume now you are in OpencvPython directory and you have opencv_test.py in that directory.

Now

1. Hold Shift key + press Right mouse Click. (make sure no file is selected)
2. click on open command window here.


Now it will open CMD window,
Run the following command in cmd.

python opencv_test.py

and press enter.


if you see below window like this, that means you have no error and your opencv installation is perfect.



See this is the easiest way to run python  opencv program, you see why i told you to create new folder and place your python file in that folder so we can run it easily.

Now we will do one more test to finalize our opencv installation by printing opencv version.

To do so write following code in your program.




if you see version printed out like below window, then you should jump out the chair or bed because opencv is finally working on your computer. now you are ready to write some awesome code that can amaze anyone in the world.





See you in next post, we will be learning how to read image file from your computer and display it.



Tags: opencv, opencvpyhon, firstprogram, opencv version test, windows.

1. Getting Started with OpenCV Python

1. Introduction to opencv python

What you'll know in this post.

  1. What is OpenCV?
  2. Why python?
  3. How to configure OpenCV in windows.

Lets get started, 

Are you working on any project that uses image processing? that's why you are here i suppose.

So what is OpenCV?
  • Well OpenCV is (Open Source Computer Vision Library), here computer vision is total different thing than Image processing so dont get confused between two terms.
  • In opencv you process your image according to your requirements, like converting image to black & white or object detection in image so there are lots of application. we will see that step by step.

OpenCV is originally written in C++, but we will be using Python to work with OpenCV.


So why python?

Well python is my friend now days a very popular programming language, due to its less coding features it provides flexibility for space time complexities. Still python is not faster than c++, because python eventually converts its OpenCV function calls to c++ and c++ executes the actual code and returns the result, this converting and executing process, we will call it as python bindings.


Ok, so now you know what is opencv.

Now how to configure OpenCV on windows?
(for linux you need to compile the source code, we will see that in future, for now go for further reading)

There are two way to configure your PC with OpenCV.

  1. Download OpenCV source Code and compile it using cmake and Visual Studio.
  2. Download Anaconda Version of Python and install OpenCV with command line.

Well we will take advantage of windows platform, and we will keep our hands clear by not compiling OpenCV source and making lots of mistakes, well if you want to compile the source code let me know in comments, i'll make new post on how to compile OpenCV source code.

Download following file.

https://www.anaconda.com/download/

Now there are two version of python 

  1. Python 2.x
  2. Python 3.x
Keep in mind that these two version have lot different in syntax and other things, please refer to python original documentation for the difference.

We will chose python 3.x version for our OpenCV installation.

So download Anaconda python 3.x version according to your requirements like 32-bit or 64-bit version.


Once you downloaded install it by default setting do not change anything. if there is any option that says add conda to your PATH environment, please make sure you enable that option.

Once you complete installation of conda.

Open Command Prompt by windows + R key. and type cmd and press ok.

This will open cmd window. 


Now its time to Install opencv on your conda python enviroment. 

To do that paste the following command in your command prompt window.

  • conda install -c conda-forge opencv 

This will install opencv in your machine, it will take time to download and install, please make sure you have enough internet data, and speed.


For creating python files and editing i'm using Sublime Text, you can use Notepad++ also.
here are the links.
  • https://www.sublimetext.com/3
  • https://notepad-plus-plus.org/

In next post we will see how to run your first OpenCV program.



Tags: opencv, python, getting started, opencvpython,windows,python