how to open camera using your python code(Computer Vision)

Does A computer have a vision ?
Yes, a computer can use its camera for its vision.

Here is the code to open camera or webcam using python code or we can say the code behind the cheese command in linux .


import cv2
cap=cv2.VideoCapture(0)
while True:
       r,photo=cap.read()
       cv2.imshow("hi",photo)
       cv2.waitKey(1)
       if cv2.waitKey(1)==27:
            break
cv2.destroyAllWindows()
cap.release()


# r is a variable which store True value if capturing is successful else it stores False.
#photo is a variable of type pnj or jpg store the photo.
#waitKey(1) is an in-built function which takes times in milisecond  as argument and wait until that time for next event.





Note:    Press  ESC key for closing your camera as given value ASCII is 27 which is of esc key.

Explanation:  

   Here the first line is to import the cv2 module thereafter we are creating a variable of image type.
In while loop we are checking after each milisecond and if  esc key is pressed the if codition will get true and the loop will  break. cv2.destroyAllWindows() is for closing the windows and cap.realease() is for releasing the resource i.e camera.

Python code for making colored and black and white video


Given code is for making a colored followed by black and white video with rectangle of given
 co-ordinate after each milisecond


Note: Here press ENTER key for closing the camera as the Ascii code given is 13.


Comments

Popular posts from this blog

Error http://172.0.0.1:8080/apex application not found in oracle 10g Solved

Sequence diagram of Airport Check-in and Sreening System in uml