Finance[US] Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
2.0k views
in OpenCV (Open Source Computer Vision Library) by Goeduhub's Expert (9.3k points)
How to interact with live camera using OpenCV. How to capture a image from live Camera using OpenCV Python. Live video playing using OpenCV.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k points)
selected by
 
Best answer

Let's Take Picture

# Import Computer Vision package - cv2

import cv2

import numpy as np

webcam = cv2.VideoCapture(0)

ret, frame = webcam.read()

print (ret)

webcam.release()

True

How to show it?

cv2.imshow("my image", frame)

cv2.waitKey()

cv2.destroyAllWindows() 

Getting Started with Videos 

Ø Capture Video from Camera

To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera. Normally one camera will be connected (as in my case). So I simply pass 0 (or -1). You can select the second camera by passing 1 and so on. After that, you can capture frame-by-frame. But at the end, don’t forget to release the capture.

import cv2

import numpy as np

cap = cv2.VideoCapture(0)

while(True):

   #capture frame-by-frame

   ret, frame = cap.read()

   #our operations on  the frame came here

   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

   #display the resulting frame

   cv2.imshow("goeduhub", gray)

     

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

   

#when everything done release the capture

cap.release()

cv2.destroyAllWindows() 

·  cap.read() returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.

·  You can end the video by pressing ‘q’.

Ø Playing Video from File

It is same as capturing from Camera, just change camera index with video file name. Also while displaying the frame, use appropriate time for cv2.waitKey(). If it is too less, video will be very fast and if it is too high, video will be slow (Well, that is how you can display videos in slow motion). 25 milliseconds will be OK in normal cases.

import cv2

import numpy as np

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):

     ret, frame = cap.read()

   #our operations on  the frame came here

   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

   #display the resulting frame

   cv2.imshow("goeduhub", gray)

   if cv2.waitKey(1) & 0xFF == ord('q'):

        break

   #when everything done release the capture

cap.release()

cv2.destroyAllWindows() 

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...