Skip to content

Instantly share code, notes, and snippets.

@whatsnewsisyphus
Last active October 12, 2019 05:00
Show Gist options
  • Save whatsnewsisyphus/edaf2bd7e1e5d3ce13d84710df348465 to your computer and use it in GitHub Desktop.
Save whatsnewsisyphus/edaf2bd7e1e5d3ce13d84710df348465 to your computer and use it in GitHub Desktop.
import argparse
import imutils
import cv2
import os
for subdir, dirs, files in os.walk(r'/Volumes/Hamal/Aaaaargh/Stuff/'):
for file in files:
filepath = subdir + os.sep + file
if filepath.endswith(".mkv"):
newfile = os.path.join(os.path.splitext(filepath)[0] + '_Average.png')
# initialize the Red, Green, and Blue channel averages, along with
# the total number of frames read from the file
(rAvg, gAvg, bAvg) = (None, None, None)
total = 0
# open a pointer to the video file
print("[INFO] opening video file pointer...")
stream = cv2.VideoCapture(filepath)
print("[INFO] computing frame averages (this will take awhile)...")
# loop over frames from the video file stream
while True:
# grab the frame from the file stream
(grabbed, frame) = stream.read()
# if the frame was not grabbed, then we have reached the end of
# the sfile
if not grabbed:
break
# otherwise, split the frmae into its respective channels
(B, G, R) = cv2.split(frame.astype("float"))
# if the frame averages are None, initialize them
if rAvg is None:
rAvg = R
bAvg = B
gAvg = G
# otherwise, compute the weighted average between the history of
# frames and the current frames
else:
rAvg = ((total * rAvg) + (1 * R)) / (total + 1.0)
gAvg = ((total * gAvg) + (1 * G)) / (total + 1.0)
bAvg = ((total * bAvg) + (1 * B)) / (total + 1.0)
# increment the total number of frames read thus far
total += 1
# merge the RGB averages together and write the output image to disk
avg = cv2.merge([bAvg * 255, gAvg * 255, rAvg * 255]).astype("uint16")
cv2.imwrite(newfile, avg)
# do a bit of cleanup on the file pointer
stream.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment