2020# are printed to stdout. Note that this program depends on the Python
2121# OpenCV bindings and NumPy.
2222#
23+ #
2324# Copyright (C) 2013-2014 Brandon Castellano <http://www.bcastell.com>.
25+ #
2426# I hereby release this file into the public domain.
2527#
2628# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
@@ -68,16 +70,15 @@ def main():
6870 start_time = cv2 .getTickCount () # Used for benchmarking/statistics after loop.
6971
7072 while True :
71- (rv , im ) = cap .read () # im is a valid image if and only if rv is true
72- if not rv :
73+ # Get next frame from video.
74+ (rv , im ) = cap .read ()
75+ if not rv : # im is a valid image if and only if rv is true
7376 break
7477
75- # im.mean() and numpy.mean(im) run at roughly the same speed
76- #frame_mean = im.mean()
77- #frame_mean = np.mean(im)
78-
79- # dividing np.sum(im) by the image size increases speed by ~35-40%
78+ # Compute mean intensity of pixels in frame.
8079 frame_mean = np .sum (im ) / float (im .shape [0 ] * im .shape [1 ] * im .shape [2 ])
80+ # Dividing the sum by the image size is 35-40% faster than using
81+ # either im.mean() or np.mean(im).
8182
8283 # Detect fade in from black.
8384 if frame_mean >= threshold and last_mean < threshold :
@@ -93,9 +94,9 @@ def main():
9394
9495 last_mean = frame_mean # Store current mean to compare in next iteration.
9596
96- # get # of frames in video as position in video we ended at
97+ # Get # of frames in video based on the position of the last frame we read.
9798 frame_count = cap .get (cv2 .cv .CV_CAP_PROP_POS_FRAMES )
98- # compute runtime and average framerate
99+ # Compute runtime and average framerate
99100 total_runtime = float (cv2 .getTickCount () - start_time ) / cv2 .getTickFrequency ()
100101 avg_framerate = float (frame_count ) / total_runtime
101102
0 commit comments