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 bfdc8d9e authored by Duchamp Solene's avatar Duchamp Solene

Merge branch 'master' into 'PY/Keras1'

# Conflicts:
#   Extract_spectrum.py
parents 91afec98 6c9f83d4
......@@ -7,6 +7,7 @@ from queue import Queue
from ast import literal_eval
from skimage.transform import resize
from time import time
from math import floor
import numpy as np
import youtube_dl
import cv2
......@@ -16,22 +17,38 @@ import moviepy.editor as mp
follow = True
queue = Queue()
linkFile = './Link-dictionnaries/Link-dictionnary2005-3.txt' #TODO: changer l'année !
linkFile = './link-dictionnaries/link-dictionnary20062.txt' # Input dict of trailers
linkDict = {}
exceptDict = {}
videoDir = './video/'
spectrumDir = '../spectrumImages/spectrumImages2005/'
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
linkDict = reading(linkFile)
TRAILER = Thread(target=downloadTrailer)
SPECTRUM = Thread(target=createSpectrum)
SPECTRUM.start()
TRAILER.start()
TRAILER.join()
follow = False
SPECTRUM.join()
def downloadTrailer():
def downloadTrailer(): #called by main()
"""Function downloading the trailer thanks to the youtube-dl library"""
global countDownload
global exceptDict
ydl_opts = {'format': 'worst/worstvideo',
'outtmpl': videoDir+'%(id)s.%(ext)s',
'noplaylist': True,
'nocheckcertificate':True,
#'max_filesize' : 10000000,
"nocheckcertificate": True,
'save_path' : videoDir }
......@@ -57,7 +74,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 +88,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,38 +113,35 @@ def resizeVideo(vidIn, vidOut):
return vidOut
return vidOut
def meanImage(img): # Return the mean LAB array
res0 = [0, 0, 0]
res = [0, 0, 0]
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
for i in range(0, lab.shape[0]):
for j in range(0, lab.shape[1]):
res0 = res0 + lab[i][j]
res0 = res0 / (img.shape[0] * img.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()
countcrop=0
while success and countcrop<240:
success,image=vidcrop.read()
countcrop+=1
cropdim=crop_image_dim(image,0)
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
res[0][count] = meanImage(image)
res[0][count] = meanImage(image,cropdim)
for i in range(1, res.shape[0]):
res[i][count] = res[0][count]
count += 1
......@@ -133,38 +152,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 : {} s".format(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 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
def main():
global videoDir
global spectrumDir
global linkDict
global linkFile
global follow
if not os.path.isdir(spectrumDir):
os.makedirs(spectrumDir)
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