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 5b60da5c authored by Hachemin Pierre-Yves's avatar Hachemin Pierre-Yves

Merge branch 'master' into 'PY/InputYears'

# Conflicts:
#   Extract_spectrum.py
parents 8afa5c9a 839450a6
...@@ -25,6 +25,7 @@ spectrumDir = '../spectrumImages/spectrumImages2008/' # Output folder to store t ...@@ -25,6 +25,7 @@ spectrumDir = '../spectrumImages/spectrumImages2008/' # Output folder to store t
countDownload = 1 countDownload = 1
countSpectrum = 1 countSpectrum = 1
def main(): def main():
global videoDir global videoDir
global spectrumDir global spectrumDir
...@@ -41,26 +42,26 @@ def main(): ...@@ -41,26 +42,26 @@ def main():
follow = False follow = False
SPECTRUM.join() SPECTRUM.join()
def downloadTrailer(): #called by main()
def downloadTrailer(): # called by main()
"""Function downloading the trailer thanks to the youtube-dl library""" """Function downloading the trailer thanks to the youtube-dl library"""
global countDownload global countDownload
global exceptDict global exceptDict
ydl_opts = {'format': 'worst/worstvideo', ydl_opts = {'format': 'worst/worstvideo',
'outtmpl': videoDir+'%(id)s.%(ext)s', 'outtmpl': videoDir + '%(id)s.%(ext)s',
'noplaylist': True, 'noplaylist': True,
'nocheckcertificate':True, 'nocheckcertificate': True,
#'max_filesize' : 10000000, # 'max_filesize' : 10000000,
"nocheckcertificate": True, 'save_path': videoDir}
'save_path' : videoDir }
for key in linkDict.keys(): for key in linkDict.keys():
if not os.path.isfile(spectrumDir+linkDict[key][2]+'.jpg'): if not os.path.isfile(spectrumDir + linkDict[key][2] + '.jpg'):
with youtube_dl.YoutubeDL(ydl_opts) as ydl: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try: try:
ydl.download(['https://www.youtube.com/watch?v=' + linkDict[key][2]]) ydl.download(['https://www.youtube.com/watch?v=' + linkDict[key][2]])
info_dict = ydl.extract_info('https://www.youtube.com/watch?v=' + linkDict[key][2], download=False) info_dict = ydl.extract_info('https://www.youtube.com/watch?v=' + linkDict[key][2], download=False)
video_ext = info_dict.get("ext", None) video_ext = info_dict.get("ext", None)
max_filesize=info_dict.get('filesize',None) max_filesize = info_dict.get('filesize', None)
#print("max_filesize : {}".format(max_filesize)) # print("max_filesize : {}".format(max_filesize))
queue.put(linkDict[key][2] + '.' + video_ext) queue.put(linkDict[key][2] + '.' + video_ext)
except Exception as e: # catch *all* exceptions except Exception as e: # catch *all* exceptions
exceptDict[linkDict[key][2]] = str(e) exceptDict[linkDict[key][2]] = str(e)
...@@ -70,25 +71,26 @@ def downloadTrailer(): #called by main() ...@@ -70,25 +71,26 @@ def downloadTrailer(): #called by main()
print("{}/{} downloads".format(countDownload, len(linkDict))) print("{}/{} downloads".format(countDownload, len(linkDict)))
else: else:
print(spectrumDir+linkDict[key][2]+'.jpg already exists !') print(spectrumDir + linkDict[key][2] + '.jpg already exists !')
countDownload += 1 countDownload += 1
def createSpectrum(): #called by main() def createSpectrum(): # called by main()
"""Function creating the spectrum from the full queue""" """Function creating the spectrum from the full queue"""
global countSpectrum global countSpectrum
global spectrumDir global spectrumDir
while (not queue.empty()) or follow: while (not queue.empty()) or follow:
item = queue.get() item = queue.get()
imgSpectrum(videoDir+item, spectrumDir + item[:-4] + '.jpg') imgSpectrum(videoDir + item, spectrumDir + item[:-4] + '.jpg')
print(item[:-4]) print(item[:-4])
global countDownload global countDownload
print("{}/{} spectrum".format(countSpectrum, len(linkDict))) print("{}/{} spectrum".format(countSpectrum, len(linkDict)))
countSpectrum += 1 countSpectrum += 1
os.remove(videoDir+item) os.remove(videoDir + item)
queue.task_done() queue.task_done()
def imgSpectrum(vidPath, spectrumOut): #called by createSpectrum()
def imgSpectrum(vidPath, spectrumOut): # called by createSpectrum()
"""Function saving the image thanks to the spectrum array """ """Function saving the image thanks to the spectrum array """
start = time() start = time()
if vidPath[-3:] != '3gp': if vidPath[-3:] != '3gp':
...@@ -99,10 +101,11 @@ def imgSpectrum(vidPath, spectrumOut): #called by createSpectrum() ...@@ -99,10 +101,11 @@ def imgSpectrum(vidPath, spectrumOut): #called by createSpectrum()
cv2.imwrite(spectrumOut, output) cv2.imwrite(spectrumOut, output)
print() print()
print("--------------------------------------------------------") print("--------------------------------------------------------")
print("Duration for {} : {} s".format(vidPath[:-3],time() - start)) print("Duration for {} : {} s".format(vidPath[:-3], time() - start))
print("--------------------------------------------------------") print("--------------------------------------------------------")
def resizeVideo(vidIn, vidOut): #called by imgSpectrum()
def resizeVideo(vidIn, vidOut): # called by imgSpectrum()
"""Function to resize the video in case it is too big (mp4 format)""" """Function to resize the video in case it is too big (mp4 format)"""
try: try:
clip = mp.VideoFileClip(vidIn) clip = mp.VideoFileClip(vidIn)
...@@ -113,12 +116,13 @@ def resizeVideo(vidIn, vidOut): #called by imgSpectrum() ...@@ -113,12 +116,13 @@ def resizeVideo(vidIn, vidOut): #called by imgSpectrum()
return vidOut return vidOut
return vidOut return vidOut
def tableSpectrum(video): #called by imgSpectrum()
def tableSpectrum(video): # called by imgSpectrum()
"""Function creating the array for the spectrum""" """Function creating the array for the spectrum"""
count = 0 count = 0
ratioFrame = 6 ratioFrame = 6
countFrame = 0 countFrame = 0
vidcrop=cv2.VideoCapture(video) vidcrop = cv2.VideoCapture(video)
"""Find if the trailer is in 4:3 or 21:9 --> borders to crop""" """Find if the trailer is in 4:3 or 21:9 --> borders to crop"""
success, image = vidcrop.read() success, image = vidcrop.read()
...@@ -145,7 +149,7 @@ def tableSpectrum(video): #called by imgSpectrum() ...@@ -145,7 +149,7 @@ def tableSpectrum(video): #called by imgSpectrum()
while success: while success:
if countFrame == ratioFrame: if countFrame == ratioFrame:
countFrame = 0 countFrame = 0
res[0][count] = meanImage(image,cropdim) res[0][count] = meanImage(image, cropdim)
for i in range(1, res.shape[0]): for i in range(1, res.shape[0]):
res[i][count] = res[0][count] res[i][count] = res[0][count]
count += 1 count += 1
...@@ -156,39 +160,40 @@ def tableSpectrum(video): #called by imgSpectrum() ...@@ -156,39 +160,40 @@ def tableSpectrum(video): #called by imgSpectrum()
return res return res
def crop_image_dim(img,tol): #called by tableSpectrum() def crop_image_dim(img, tol): # called by tableSpectrum()
"""Find the dimensions cropped if this is a 4:3 or 21:9 trailer""" """Find the dimensions cropped if this is a 4:3 or 21:9 trailer"""
#tol is the tolerance : 0 to find black pixels # tol is the tolerance : 0 to find black pixels
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = img_gray>tol mask = img_gray > tol
midV=floor(mask.shape[0]/2) midV = floor(mask.shape[0] / 2)
midH=floor(mask.shape[1]/2) midH = floor(mask.shape[1] / 2)
upBorder=0 upBorder = 0
downBorder=mask.shape[0] downBorder = mask.shape[0]
leftBorder=0 leftBorder = 0
rightBorder=mask.shape[1] rightBorder = mask.shape[1]
for i in range(1,mask.shape[0]): for i in range(1, mask.shape[0]):
if mask[i-1][midH]!=mask[i][midH] and mask[i][midH]==True: if mask[i - 1][midH] != mask[i][midH] and mask[i][midH] == True:
if upBorder==mask[0][midH]: if upBorder == mask[0][midH]:
upBorder=i-1 upBorder = i - 1
if mask[i-1][midH]!=mask[i][midH] and mask[i][midH]==False: if mask[i - 1][midH] != mask[i][midH] and mask[i][midH] == False:
downBorder=i downBorder = i
for j in range(1,mask.shape[1]): for j in range(1, mask.shape[1]):
if mask[midV][j-1]!=mask[midV][j] and mask[midV][j]==True: if mask[midV][j - 1] != mask[midV][j] and mask[midV][j] == True:
if leftBorder==mask[midV][0]: if leftBorder == mask[midV][0]:
leftBorder=j-1 leftBorder = j - 1
if mask[midV][j-1]!=mask[midV][j] and mask[midV][j]==False: if mask[midV][j - 1] != mask[midV][j] and mask[midV][j] == False:
rightBorder=j rightBorder = j
return (upBorder,downBorder, leftBorder, rightBorder) return (upBorder, downBorder, leftBorder, rightBorder)
def meanImage(img,cropdim): #called by tableSpectrum()
def meanImage(img, cropdim): # called by tableSpectrum()
"""Return the mean LAB array only with pixels in cropped image""" """Return the mean LAB array only with pixels in cropped image"""
res0 = [0, 0, 0] res0 = [0, 0, 0]
res = [0, 0, 0] res = [0, 0, 0]
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
for i in range(cropdim[0],cropdim[1]): for i in range(cropdim[0], cropdim[1]):
for j in range(cropdim[2],cropdim[3]): for j in range(cropdim[2], cropdim[3]):
res0 = res0 + lab[i][j] res0 = res0 + lab[i][j]
sumpix=(cropdim[1]-cropdim[0]+1)*(cropdim[3]-cropdim[2]+1) sumpix=(cropdim[1]-cropdim[0]+1)*(cropdim[3]-cropdim[2]+1)
res0[0]= res0[0] / sumpix res0[0]= res0[0] / sumpix
...@@ -200,12 +205,13 @@ def meanImage(img,cropdim): #called by tableSpectrum() ...@@ -200,12 +205,13 @@ def meanImage(img,cropdim): #called by tableSpectrum()
return res return res
def reading(path): #Called by main() def reading(path): # Called by main()
"""Function to create the dictionnary from the text file""" """Function to create the dictionnary from the text file"""
with open(path, 'r') as f: with open(path, 'r') as f:
s = f.read() s = f.read()
whip = literal_eval(s) whip = literal_eval(s)
return whip return whip
if __name__ == '__main__': if __name__ == '__main__':
main() main()
This diff is collapsed.
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