frame_rate.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. import cv2
  3. import time
  4. if __name__ == '__main__' :
  5. # Start default camera
  6. video = cv2.VideoCapture(0);
  7. # Find OpenCV version
  8. (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
  9. # With webcam get(CV_CAP_PROP_FPS) does not work.
  10. # Let's see for ourselves.
  11. if int(major_ver) < 3 :
  12. fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
  13. print "Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps)
  14. else :
  15. fps = video.get(cv2.CAP_PROP_FPS)
  16. print "Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps)
  17. # Number of frames to capture
  18. num_frames = 120;
  19. print "Capturing {0} frames".format(num_frames)
  20. # Start time
  21. start = time.time()
  22. # Grab a few frames
  23. for i in xrange(0, num_frames) :
  24. ret, frame = video.read()
  25. # End time
  26. end = time.time()
  27. # Time elapsed
  28. seconds = end - start
  29. print "Time taken : {0} seconds".format(seconds)
  30. # Calculate frames per second
  31. fps = num_frames / seconds;
  32. print "Estimated frames per second : {0}".format(fps);
  33. # Release video
  34. video.release()