Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit 43808d3f authored by Hachemin Pierre-Yves's avatar Hachemin Pierre-Yves

Extract spectrum cleaned+crop

parent 4c7273e6
......@@ -17,16 +17,31 @@ import moviepy.editor as mp
follow = True
queue = Queue()
linkFile = './link-dictionnaries/link-dictionnary20062.txt'
linkFile = './link-dictionnaries/link-dictionnary20062.txt' # Input dict of trailers
linkDict = {}
exceptDict = {}
videoDir = './video/'
spectrumDir = '../spectrumImages/spectrumImages2006/'
videoDir = './video/' # Folder to store temporarely the videos
spectrumDir = '../spectrumImages/spectrumImages2006/' # Output folder to store the spectrums
countDownload = 1
countSpectrum = 1
def main():
global videoDir
global spectrumDir
global linkDict
global linkFile
global follow
def downloadTrailer():
linkDict = reading(linkFile)
TRAILER = Thread(target=downloadTrailer)
SPECTRUM = Thread(target=createSpectrum)
SPECTRUM.start()
TRAILER.start()
TRAILER.join()
follow = False
SPECTRUM.join()
def downloadTrailer(): #called by main()
"""Function downloading the trailer thanks to the youtube-dl library"""
global countDownload
global exceptDict
......@@ -57,7 +72,7 @@ def downloadTrailer():
countDownload += 1
def createSpectrum():
def createSpectrum(): #called by main()
"""Function creating the spectrum from the full queue"""
global countSpectrum
global spectrumDir
......@@ -71,17 +86,22 @@ def createSpectrum():
os.remove(videoDir+item)
queue.task_done()
def imgSpectrum(vidPath, spectrumOut): #called by createSpectrum()
"""Function saving the image thanks to the spectrum array """
start = time()
if vidPath[-3:] != '3gp':
res2 = tableSpectrum(resizeVideo(vidPath, vidPath))
else:
res2 = tableSpectrum(vidPath)
output = cv2.cvtColor(res2, cv2.COLOR_Lab2BGR)
cv2.imwrite(spectrumOut, output)
print()
print("--------------------------------------------------------")
print("Duration for {} : {} s".format(vidPath[:-3],time() - start))
print("--------------------------------------------------------")
def reading(path):
"""Function to create the dictionnary from the text file"""
with open(path, 'r') as f:
s = f.read()
whip = literal_eval(s)
return whip
def resizeVideo(vidIn, vidOut):
"""Function to resize the video in case it is too big"""
def resizeVideo(vidIn, vidOut): #called by imgSpectrum()
"""Function to resize the video in case it is too big (mp4 format)"""
try:
clip = mp.VideoFileClip(vidIn)
clip_resized = clip.resize(height=36) # Make the height 36px
......@@ -91,73 +111,31 @@ def resizeVideo(vidIn, vidOut):
return vidOut
return vidOut
def crop_image_dim(img,tol):
"""Find the dimensions cropped if this is a 4:3 or 21:9 trailer"""
# img is image data
# tol is tolerance
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = img_gray>tol
midV=floor(mask.shape[0]/2)
midH=floor(mask.shape[1]/2)
upBorder=0
downBorder=mask.shape[0]
leftBorder=0
rightBorder=mask.shape[1]
for i in range(1,mask.shape[0]):
if mask[i-1][midH]!=mask[i][midH] and mask[i][midH]==True:
if upBorder==mask[0][midH]:
upBorder=i-1
if mask[i-1][midH]!=mask[i][midH] and mask[i][midH]==False:
downBorder=i
for j in range(1,mask.shape[1]):
if mask[midV][j-1]!=mask[midV][j] and mask[midV][j]==True:
if leftBorder==mask[midV][0]:
leftBorder=j-1
if mask[midV][j-1]!=mask[midV][j] and mask[midV][j]==False:
rightBorder=j
return (upBorder,downBorder, leftBorder, rightBorder)
def meanImage(img,cropdim): # Return the mean LAB array
res0 = [0, 0, 0]
res = [0, 0, 0]
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
for i in range(lab.shape[0]):
for j in range(lab.shape[1]):
res0 = res0 + lab[i][j]
res0 = res0 / (lab.shape[0]*lab.shape[1])
res[0] = int(res0[0])
res[1] = int(res0[1])
res[2] = int(res0[2])
return res
def tableSpectrum(video):
def tableSpectrum(video): #called by imgSpectrum()
"""Function creating the array for the spectrum"""
count = 0
ratioFrame = 6
countFrame = 0
vidcrop=cv2.VideoCapture(video)
"""Find if the trailer is in 4:3 or 21:9 --> borders to crop"""
success, image = vidcrop.read()
success = True
countcrop=0
while success and countcrop<240:
success,image=vidcrop.read()
countcrop+=1
cropdim=crop_image_dim(image,0)
print(image.shape)
cropdim=(0,image.shape[0],0,image.shape[1])
print(cropdim)
print("image size : {}".format(image.shape))
print("cropped dim : {}".format(cropdim))
"""Read every images of the trailer and out the mean color of each"""
vidcap = cv2.VideoCapture(video)
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT) / ratioFrame)
width = int(length * 3 / 16)
res = np.zeros([width, length, 3], dtype=np.uint8)
success, image = vidcap.read()
success = True
while success:
if countFrame == ratioFrame:
countFrame = 0
......@@ -172,37 +150,53 @@ def tableSpectrum(video):
return res
def imgSpectrum(vidPath, spectrumOut):
"""Function saving the image thanks to the spectrum array """
start = time()
if vidPath[-3:] != '3gp':
res2 = tableSpectrum(resizeVideo(vidPath, vidPath))
else:
res2 = tableSpectrum(vidPath)
output = cv2.cvtColor(res2, cv2.COLOR_Lab2BGR)
cv2.imwrite(spectrumOut, output)
print()
print("--------------------------------------------------------")
print("Duration for {} : {} s".format(vidPath[:-3],time() - start))
print("--------------------------------------------------------")
def crop_image_dim(img,tol): #called by tableSpectrum()
"""Find the dimensions cropped if this is a 4:3 or 21:9 trailer"""
#tol is the tolerance : 0 to find black pixels
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = img_gray>tol
midV=floor(mask.shape[0]/2)
midH=floor(mask.shape[1]/2)
upBorder=0
downBorder=mask.shape[0]
leftBorder=0
rightBorder=mask.shape[1]
for i in range(1,mask.shape[0]):
if mask[i-1][midH]!=mask[i][midH] and mask[i][midH]==True:
if upBorder==mask[0][midH]:
upBorder=i-1
if mask[i-1][midH]!=mask[i][midH] and mask[i][midH]==False:
downBorder=i
for j in range(1,mask.shape[1]):
if mask[midV][j-1]!=mask[midV][j] and mask[midV][j]==True:
if leftBorder==mask[midV][0]:
leftBorder=j-1
if mask[midV][j-1]!=mask[midV][j] and mask[midV][j]==False:
rightBorder=j
return (upBorder,downBorder, leftBorder, rightBorder)
def main():
global videoDir
global spectrumDir
global linkDict
global linkFile
global follow
def meanImage(img,cropdim): #called by tableSpectrum()
"""Return the mean LAB array only with pixels in cropped image"""
res0 = [0, 0, 0]
res = [0, 0, 0]
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
for i in range(cropdim[0],cropdim[1]):
for j in range(cropdim[2],cropdim[3]):
res0 = res0 + lab[i][j]
res0 = res0 / ((cropdim[1]-cropdim[0]+1)*(cropdim[3]-cropdim[2]+1))
res[0] = int(res0[0])
res[1] = int(res0[1])
res[2] = int(res0[2])
return res
linkDict = reading(linkFile)
TRAILER = Thread(target=downloadTrailer)
SPECTRUM = Thread(target=createSpectrum)
SPECTRUM.start()
TRAILER.start()
TRAILER.join()
follow = False
SPECTRUM.join()
def reading(path): #Called by main()
"""Function to create the dictionnary from the text file"""
with open(path, 'r') as f:
s = f.read()
whip = literal_eval(s)
return whip
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment