Friday, December 13, 2019

6. Using Numpy with OpenCV-Python

Hey, welcome back its been a while since I have been active on this blog, but yes i was preparing something great, that is a free YouTube course on Advanced Computer Vision using OpenCV-Python.


I have already released one section of the course you can find it on this
URL: https://www.youtube.com/playlist?list=PLwRoxHWReaEhVFjTeKlifKUimbw6ZyV7K 


Now about Numpy, Numpy stands for Numeric Python, it is highly optimized library for python for doing numeric operations, that is addition multiplication, subtraction and division not only on numbers but n-dimentional array or matrix you can say.

So OpenCV uses Numpy to represent the images, now whatever operation you do, you'll be doing with Numpy Array or matrix you can say.

To prove this I'm going to show you a small code snippet which reads image using opencv and displays the data type of the image variable.



As you can see it shows numpy.ndarray which means numpy n-dimentional array because the image is RGB image, it has width,height and channels.


Can we create an image using Numpy then??


The answer is Yes, we can create a blank image using Numpy, let see how to do it.


The first step is to import numpy library with alias, alias is nothing but the custom name you give to your package which is to be considered throughout the program life cycle. It can be np, it can be num or anything you like, the best practice is to keep it a short name because we will be needing the alias name to access numpy methods.

In can second step we are creating a blank image, we are using np alias of numpy package to access zeros method what it means that whatever size of the array will be created it will be filled with zero(0) values. In OpenCV 0 value means black Pixel, so our image will be of black pixel.

Zeros method takes two arguments 1. Shape 2. data type.

1. Shape: shape is nothing the the dimension or size of our image, if its gray or single channel image the shape will be (width,height) and if your image is RGB then the shape will be  (width,height,channels).

Important thing to not here is these values are passed as tuples, tuples are nothing but the type of array you can say except its values can not be changed.

2. dtype (data type): opencv uses uint8 data type to represent the images, because the pixel values ranges from 0 to 255. any addition or  or subtraction from the values should be result in circular increment or decrements . that means the maximum value of the pixel is 255 if you add +1 to the pixel value dtype="uint8" will reset the value to 0 so 256 value will be considered as 0 and 257 as 1 and so on.

Same when subtracting value from 0 it will reset the value to 255 like 0 - 1 = 255, 0 - 2 = 254 and so on.



 Changing the value of x=0 y=0 c=0 by adding 265 to its original value it results in 9. its circular.

In other words, 0 + 265 = 9 when uint8 data type is used, because 255 is max value for uint8 data type. when the values reaches 256 its sets its value to 0 and then again the increment starts so result is 9.

I hope you understood the concept. Let me know in case of any doubts.


Until then Happy Learning.


Jaimin Bhoi
Assistant System Engineer/Computer Vision Researcher
Tata Consultancy Services.





#opencv #python #ai #ml #dl #machinelearning #deeplearning #keras #tensorflow #pytorch #anaconda #conda #spyder #jupyter #notebook #opencv #computervision #logo #deeplearning #artificialintelligence #ai #python #aipython #machinelearning #deeplearning #photography #photoshop

Wednesday, July 10, 2019

5. Saving Video using OpenCV

Hi there, its been a long time since i haven't updated anything on this blog, but i haven't come back with empty hands or empty mind, i have very great ideas to post on this blog, so stay tuned.

If you haven't check my other posts i request you to go through it once.

Let me start where i left, i have completed below topics.

1. Introduction to OpenCV & Installing OpenCV in your PC. (Link)
2. Writing your first OpenCV program. (Link)
3. Read,Save and display Images (Link)
4. Reading videos with OpenCV (Link)
4. Writing videos with OpenCV (This blog)

In this blog i want to show you how to save videos with OpenCV, before that we need to read video or video stream to our program so if you dont know how to do that i request you to go through this post to know how to read videos using OpenCV.

Ok lets get started,

1. Read video from either your webcam or from any other video file.


In my previous post i showed you how to read videos using imutils package, but in this video i want to show you how to read video using inbuilt OpenCV function cv2.VideoCapture().

Create a new file name it as WriteVideo.py




Import cv2 library, and initialize video capture instance.
videofile variable should contain a path to your video file if you are using video.

if you are using webcam just replace the code as below,



Now lets see if our video or webcam is working or not,


In above code what we are doing is that, we are checking if the video capture stream is opened or not? by using isOpened() method. the reason why we are using this method is to end the infinite while loop when video frame is ended when we are using video files. while in webcam if the webcam is not found the while loop will not initiate.

Once we are in while loop, we are reading frames from videofile or the webcam, the read() method returns two values, first one is flag containing true or false for frame is received or not, and second is frame it self. frame is nothing but an image from the video source.


after that we are quickly checking if we have received frame or not, if not we are ending our program, if you are using video file the program will end as soon as the video is ended.

on line 13 we are displaying our frame by using cv2.imshow(), line 14 is to record user keyboard event when we want to exit in between by pressing q key from keyboard we can exit the while loop by breaking the loop.

once the loop is exited, we have to close the window opened by cv2.imshow() method or else it'll stay opened all the time even if we end our program, and also we have to release our capture instance as well as the the resource that we are using (i.e videofile or webcam so that other process or program can access it afterwards.


2. Save video frames.


Before saving the video, you should know that each computer has something called codecs, which is used while saving,displaying(rendering) videos on our computer. my computer might have different codec and your computer might have different codec as well. so if i write code according to my computer's codec it might possible that it'll not work on your computer, if it is having different codec. So to solve this issue i have developed a module that can check what codecs are supported by your computer so that you can use them to save the video.


so to do that you need that module, you can download it from my github repo. Download it or just do git checkout of that file, place this file in same directory where WriteVideo.py present.


Now once you have this file import this file(module) in our WriteVideo.py file.


Make sure the file name is correct with case sensitive.

Once you import this file, you are ready to test the codecs installed in your system.


In above code i have commented out the webcam code because in this module we will be needing one video file to give as an input, so i have given path to the video file and i'm passing the videofile path to the CV2VideoWriter instance. you can take any file and give it as an input, make sure you enter correct path, best practice is to put video file in same directory and give videofile name in your program.

Note: Please take video which is short in length like 1 to 2 minutes, so that the procedure finishes faster. Also take long enough so that we can extract few frames and save the video.

Once you run this code, what actually this CV2VideoWriter does is that it reads the video given as an input and tries to save video with different available codecs, one which passes the saving procedure it counts as a working codec where it fails it count that codec as a not supported codec by system.

In output you can see the available codecs in your computer which you can use to save the videos from OpenCV.

If you are running code in cmd you can see some errors with output which you can ignore,but observe the line start with codec and showing its availability, where its stated as true means that codec is available to use.


But of you run this program on spyder IDE, you can see a clear output shown below. If you want to know more about spyder IDE leave your questions in comment section.



Now we have the codec names which we can use to save the videos using OpenCV.

Below i have attached the full code for saving video.


There are few changes that we will do while saving the video,

1. While initiating CV2VideoWriter Instance we will provide codec that is working in our your PC.
2. We will provide channel type i.e single channel that is black and white or 3 channel that is RGB. so to do this we will provide channel=0 for single channel and for RGB channel=1. (if you dont do this you might get an error)
3. We will start a session for video writer to save a frame.
4. We will read a frame from webcam and convert it into gray image using cv2.cvtcolor() method. if you are using video that is fine, if you have provided channel=0 that is single channel then you need to convert your frame into gray that is single channel or else it will throw an error.
5. After converting we will save the frame using writter.save_session_frame() method.

Once all the frame saving is done we need to end the session so that our writer knows to finally save the video and of course we need to release our capture instance so that other program can access out webcam or video file.

Kudos, now you know how to save video using OpenCV python.

Let me know in case you are facing any issue with above method.

tags: opencv,python,video,videowrite,how to write video using opencv,videowritter,ffmpeg, codec, VideoWriter_fourcc, opencv with python, video operations, convert to gray.

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