Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

comparison and copying if different. Using image size comparison (roo…

…t mean square not better on my sample for now)
  • Loading branch information...
commit ae4969fcf013438da93d2f05e3daaffc1ee058ad 1 parent 589123e
@metadirective metadirective authored
Showing with 57 additions and 17 deletions.
  1. +57 −17 videoFileAnalyser.py
View
74 videoFileAnalyser.py
@@ -3,7 +3,7 @@
# videoFileAnalyser (c) Univesity of Strasbourg
# Developper: F. Schnell
#
-# A command-line tool to automatically extract images from a video file (mp4),
+# A script to automatically extract images from a video file (mp4),
# comparing them to keep significant ones
#
###############################################################################
@@ -11,7 +11,15 @@
import subprocess, os
import ImageChops, Image
-import math
+import math, shutil
+
+# Variables
+sizeChangeThreshold=5000 # used only in File Size change comparison method
+picsExtraction = True # extract pictures from video files
+videoFilePath="enregistrement-video.mp4" # name of the video to analyse
+extractionInterval= "0.1" # 1 / time interval : 1= each seconds, 0.1 each 10 secs...
+outputPath="tests" # Output folder
+cleanFiles=True # remove extacted files to only keep different ones renamed D1.jpg, D2.jpg, etc
print "> videoFileAnalyser started..."
@@ -25,25 +33,57 @@ def rmsdiff(im1, im2):
rms = math.sqrt(sum_of_squares/float(im1.size[0] * im1.size[1]))
return rms
-if 1:
+if picsExtraction==True:
print "> extracting images from video..."
- p= subprocess.Popen(["ffmpeg","-i","enregistrement-video.mp4","-r","0.1","-f","image2","tests/%05d.jpg"])
+ p= subprocess.Popen(["ffmpeg","-i",videoFilePath,"-r",extractionInterval,"-f","image2",outputPath+"/%05d.jpg"])
p.communicate() #waiting for ffmpeg to finish before continuing script
if 0: # Test
print "> comparing pictures..."
- im1=Image.open("tests/1.jpg")
- im2=Image.open("tests/7.jpg")
+ im1=Image.open(outputPath+"/00001.jpg")
+ im2=Image.open(outputPath+"/00001.jpg")
print rmsdiff(im1,im2)
+
+if 0: # Square root method not working well on my test samples
+ print "Square root comparison method"
+ previousDiff=rmsdiff(Image.open(outputPath+"/00001.jpg"),Image.open(outputPath+"/00001.jpg"))
-if 1:
- print "to do :"
- print "begin with first picture and make a copy D1.jpg"
- print "go through pic, if rms difference > threshold create n.jpg"
- print "remove others pics when finished"
-
- for fileName in (os.listdir("tests")):
- if fileName != "00001.jpg":
- print fileName
- #currentRms =
-
+if 1: # file size changes comparaison method (very quick)
+ print "File size changes comparaison method (very quick)"
+ previousFileSize=os.path.getsize(outputPath+"/00001.jpg")
+ print "Selected Size Change Threshold is : "+str(sizeChangeThreshold)
+
+previousFileName="00001.jpg"
+slideIndex=1
+shutil.copy2(outputPath+"/"+previousFileName,outputPath+"/D"+str(slideIndex)+".jpg")
+
+for fileName in (os.listdir(outputPath)):
+ if fileName != "00001.jpg":
+
+ if 1: # File Size comparison method
+ currentFileSize=os.path.getsize(outputPath+"/"+fileName)
+ diffSize=abs(currentFileSize-previousFileSize)
+ print fileName +" --- "+ str(currentFileSize) + " abs diff is: "+ str(diffSize)
+ if diffSize > sizeChangeThreshold:
+ slideIndex+=1
+ #print ">>>>>>>>>>>>>> " + str(fileName)
+ shutil.copy2(outputPath+"/"+fileName, outputPath+"/D"+str(slideIndex)+".jpg")
+ previousFileSize=currentFileSize
+
+ if 0: # Root Mean Square comparison method (don't work well on my test sample)
+ currentfileName= fileName
+ diff = rmsdiff(Image.open(outputPath+"/"+previousFileName),Image.open(outputPath+"/"+currentfileName))
+ print "difference betweeen " + previousFileName+ " and "+ currentfileName+ " is "+ str(diff)
+ deltaDiff=math.fabs(diff-previousDiff)
+ print "diff - previousDiff", deltaDiff
+ if deltaDiff > 10:
+ print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> New dia detected :" + currentfileName
+ previousFileName=currentfileName
+ previousDiff=diff
+
+if cleanFiles==True:
+ for fileName in (os.listdir(outputPath)):
+ if "D" in fileName:
+ pass
+ else:
+ os.remove(outputPath+"/"+fileName)
Please sign in to comment.
Something went wrong with that request. Please try again.