(path, override=False)
| 3 | import cv2 |
| 4 | |
| 5 | def count_frames(path, override=False): |
| 6 | # grab a pointer to the video file and initialize the total |
| 7 | # number of frames read |
| 8 | video = cv2.VideoCapture(path) |
| 9 | total = 0 |
| 10 | |
| 11 | # if the override flag is passed in, revert to the manual |
| 12 | # method of counting frames |
| 13 | if override: |
| 14 | total = count_frames_manual(video) |
| 15 | |
| 16 | # otherwise, let's try the fast way first |
| 17 | else: |
| 18 | # lets try to determine the number of frames in a video |
| 19 | # via video properties; this method can be very buggy |
| 20 | # and might throw an error based on your OpenCV version |
| 21 | # or may fail entirely based on your which video codecs |
| 22 | # you have installed |
| 23 | try: |
| 24 | # check if we are using OpenCV 3 |
| 25 | if is_cv3(): |
| 26 | total = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) |
| 27 | |
| 28 | # otherwise, we are using OpenCV 2.4 |
| 29 | else: |
| 30 | total = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) |
| 31 | |
| 32 | # uh-oh, we got an error -- revert to counting manually |
| 33 | except: |
| 34 | total = count_frames_manual(video) |
| 35 | |
| 36 | # release the video file pointer |
| 37 | video.release() |
| 38 | |
| 39 | # return the total number of frames in the video |
| 40 | return total |
| 41 | |
| 42 | def count_frames_manual(video): |
| 43 | # initialize the total number of frames read |
nothing calls this directly
no test coverage detected
searching dependent graphs…