Skip to content

Instantly share code, notes, and snippets.

@BuyMyMojo
Created May 28, 2022 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BuyMyMojo/2fdc6c7b7102419f0a16490cd86950d4 to your computer and use it in GitHub Desktop.
Save BuyMyMojo/2fdc6c7b7102419f0a16490cd86950d4 to your computer and use it in GitHub Desktop.
A basic method to measure video fps using OpenCV's countNonZero function.
def framerateMeasure(pngPath, csvPath, rate):
start = time()
differenceValues = []
for i in range(rate):
differenceValues.append(0)
fields=['Frame','FPS']
writeCSV(fields, csvPath)
j = 1
# len(os.listdir(pngPath))-1
for x in range(len(os.listdir(pngPath))-1):
img1 = cv.imread(pngPath + str(j) + ".png")
j = j + 1
img2 = cv.imread(pngPath + str(j) + ".png")
difference = cv.subtract(img1, img2)
diffImg = cv.cvtColor(difference, cv.COLOR_BGR2GRAY)
nzCount = cv.countNonZero(diffImg)
if nzCount == 0:
# Dupe Frame
differenceValues.pop(0)
differenceValues.append(0)
else:
# New frame
differenceValues.pop(0)
differenceValues.append(1)
updateCSV(calcFPS(differenceValues), x, csvPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment