Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (18.8k points)

1 Answer

0 like 0 dislike
by (18.8k 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() 

Winter 10 Days bootcamp classes(7 HRS Daily) will start from 5 & 20 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

...