Saturday, December 26, 2015

Python, OpenCV: Video Capture and Display

OpenCV: Video Capture

import time
import cv2

video_capture = cv2.VideoCapture(0)
while True:
    t0=time.time()
    ret, frame = video_capture.read()
    t1=time.time()
    print "passed ", t1-t0, "s"

    cv2.imshow('Video', frame)
    #on pressing "q" exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        
video_capture.release()
cv2.destroyAllWindows()  

Frames Per Second Counter

#test how many frames per second for a webcam
import time
import cv2

video_capture = cv2.VideoCapture(1)
t0 = time.time()
i=0
while True:
    ret, frame = video_capture.read()
    print frame.shape
    t = time.time()
    if t-t0 > 1:
        t0=t
        print "frames ",i
        i=0
        
    i+=1    
                    
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
        
video_capture.release()
cv2.destroyAllWindows()   

No comments:

Post a Comment