Monday, 27 June 2016

TUT3: Starting laptop Camera using OpenCV and Python

Does your laptop has a Camera? Ever wondered how it starts. What's the type of code that works behind when you start your laptop camera. If not, this tutorial is for you.

We will be using some new terms today like cv2.VideoCapture(0) , cap.read() etc.

So lets see how to start the camera on laptop:

1) import cv2
    import numpy as np

2) cap = cv2.VideoCapture(0)

    Here 'cap' is a VideoCapture object. It has an argument. This argument can be either device index       or the name of a video file. Since here we are concerned with camera device so we pass device           index. I am passing '0' as the argument since the built-in webcam is mostly accessed by passing '0'.     If your system has external webcam then you can access it by typing index > '0' i.e. '1' or bigger.         Remember, this line of code will be used as it is in all the camera or video related programs.

3) Attention!! We are going to see how to use while loop in python:

    while(True):                                            #line 1
           ret, frame = cap.read()                     #line 2
           cv2.imshow('cam', frame)               #line 3
           if cv2.waitKey == 27:                     #line 4
                   break                                       #line 5
 
    cap.realease()
    cv2.destroyAllWindows()

   Remember there are no curly braces in python.  Scope is implemented using the Colon and indentation. Colon defines that following indented lines are within the while loop.
   As you can see #line 2, #line 3 and #line 4 do not start from beginning of line. There is a tab space before each of them. This indentation means they are inside of while loop and one iteration of this loop executes these lines once. Hence we need to take care of indentation. Similarly #line 5 is inside the if condition.

NOTE: When you are writing python code in IDLE(An IDE for python), the editor will automatically indent the lines once you type #line 1 and press Enter.

cap.read() will read the frame from the camera and store it in 'frame'. 'ret' will store true or false value depending on whether the frame was read or not.  cv2.imshow('cam', frame) will show the frame on to the window named 'cam'.

"if cv2.waitKey(1) == 27" holds the window for 1 millisecond and then check if the "ESC" key is pressed("27 is ASCII for ESC"). If the "ESC" key is pressed then 'break' is encountered and we are out of loop. This will also close the camera as cap.release() is executed next.

After you have written the code press CTRL+S to save the code to a desired location. Remember to save the file using .py extension. After this, press F5 to run the code while your code is open in IDLE.

**********************************************************
Here is the summed up code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):
     ret, frame = cap.read()
     cv2.imshow('video', frame)
     if cv2.waitKey(1) == 27:
           break

cap.release()  
cv2.destroyAllWindows()
**********************************************************

OUTPUT:
 


So i hope that you have learned how to start your camera using Python and OpenCV.
That's it for this tutorial. In the next tutorial, we will see how to capture an image using in-built laptop camera. 
Any queries please ask in comments.
See you in next tutorial.


No comments:

Post a Comment