Skip to content
Snippets Groups Projects
Commit ed67ade3 authored by Leonardo de Lima Gaspar's avatar Leonardo de Lima Gaspar
Browse files

Added automatic thresholding by average, and rescaling to 480p of input. After...

Added automatic thresholding by average, and rescaling to 480p of input. After extensive testing, conclusion is the method is not fit for production; too unstable with different source videos, and the processing time is just barely enough on a strong CPU. Will look into ViBe instead.
parent b7f02c06
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,10 @@ from moviepy.editor import ColorClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
import numpy as np
import math as math
import time
setFPS = 2
numberOfQuadrants =25
class bcolors:
HEADER = '\033[95m'
......@@ -39,19 +43,35 @@ def meanRGB(quadrant):
sum += quadrant[h,w,c]
return sum / (totWidth*totHeight*colourband)
def getThresholdByAvg(video):
frames = video.iter_frames(fps=setFPS, dtype="uint8")
totMean = 0
framesInVideo = 0
for frame in frames:
framesInVideo += 2
curFrame = frame
nextFrame = next(frames)
curMean = meanRGB(curFrame[:,:,:])
nextMean = meanRGB(nextFrame[:,:,:])
totMean += np.abs(nextMean-curMean)
return totMean/framesInVideo
def main():
startTime = time.time()
input = VideoFileClip("Myggbukta-[2021-05-21_10-47-06]-459.mp4", target_resolution=(270,480))
timestamps = [(1136, 1159)]
for start, stop in timestamps:
input = VideoFileClip("testGjedde19.mp4")
video = input.subclip(start, stop)
video = input
videoDimensions = video.size
listOfDetectionTimes = []
listOfDetectionTimes.append(video)
setFPS = 2
numberOfQuadrants = 25
contrastMagnitudeThreshold = getThresholdByAvg(video)
quads = splitIntoQuadrants(videoDimensions[0], videoDimensions[1], numberOfQuadrants)
frames = video.iter_frames(fps=setFPS, dtype="uint8")
......@@ -67,15 +87,20 @@ def main():
diffs.append( meanRGB(curFrame[quads[i][2]:quads[i][3], quads[i][0]:quads[i][1], :]) - meanRGB(nextFrame[quads[i][2]:quads[i][3], quads[i][0]:quads[i][1], :]) )
magnitude = np.sum(np.abs(diffs))
if magnitude > 4:
if magnitude > contrastMagnitudeThreshold+4:
colorclip = ColorClip(size=videoDimensions, color=(0,255,0))
colorclip = colorclip.set_opacity(0.3)
colorclip = colorclip.set_opacity(0.2)
colorclip = colorclip.set_start(curTime)
colorclip = colorclip.set_end(curTime + 2/setFPS)
listOfDetectionTimes.append(colorclip)
finalVideo = CompositeVideoClip(listOfDetectionTimes)
CompositeVideoClip.write_videofile(finalVideo, "testingDetection.mp4")
endTime = time.time()
print(f"Processing time taken: {endTime-startTime} seconds")
CompositeVideoClip.write_videofile(finalVideo, "testingDetection.mp4")
endTime = time.time()
print(f"Total time taken: {endTime-startTime} seconds")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment