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
countDownload = 1
countSpectrum = 1
def main():
global videoDir
global spectrumDir
......@@ -41,26 +42,26 @@ def main():
follow = False
SPECTRUM.join()
def downloadTrailer(): #called by main()
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',
'outtmpl': videoDir + '%(id)s.%(ext)s',
'noplaylist': True,
'nocheckcertificate':True,
#'max_filesize' : 10000000,
"nocheckcertificate": True,
'save_path' : videoDir }
'nocheckcertificate': True,
# 'max_filesize' : 10000000,
'save_path': videoDir}
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:
try:
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)
video_ext = info_dict.get("ext", None)
max_filesize=info_dict.get('filesize',None)
#print("max_filesize : {}".format(max_filesize))
max_filesize = info_dict.get('filesize', None)
# print("max_filesize : {}".format(max_filesize))
queue.put(linkDict[key][2] + '.' + video_ext)
except Exception as e: # catch *all* exceptions
exceptDict[linkDict[key][2]] = str(e)
......@@ -70,25 +71,26 @@ def downloadTrailer(): #called by main()
print("{}/{} downloads".format(countDownload, len(linkDict)))
else:
print(spectrumDir+linkDict[key][2]+'.jpg already exists !')
print(spectrumDir + linkDict[key][2] + '.jpg already exists !')
countDownload += 1
def createSpectrum(): #called by main()
def createSpectrum(): # called by main()
"""Function creating the spectrum from the full queue"""
global countSpectrum
global spectrumDir
while (not queue.empty()) or follow:
item = queue.get()
imgSpectrum(videoDir+item, spectrumDir + item[:-4] + '.jpg')
imgSpectrum(videoDir + item, spectrumDir + item[:-4] + '.jpg')
print(item[:-4])
global countDownload
print("{}/{} spectrum".format(countSpectrum, len(linkDict)))
countSpectrum += 1
os.remove(videoDir+item)
os.remove(videoDir + item)
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 """
start = time()
if vidPath[-3:] != '3gp':
......@@ -99,10 +101,11 @@ def imgSpectrum(vidPath, spectrumOut): #called by createSpectrum()
cv2.imwrite(spectrumOut, output)
print()
print("--------------------------------------------------------")
print("Duration for {} : {} s".format(vidPath[:-3],time() - start))
print("Duration for {} : {} s".format(vidPath[:-3], time() - start))
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)"""
try:
clip = mp.VideoFileClip(vidIn)
......@@ -113,12 +116,13 @@ def resizeVideo(vidIn, vidOut): #called by imgSpectrum()
return vidOut
return vidOut
def tableSpectrum(video): #called by imgSpectrum()
def tableSpectrum(video): # called by imgSpectrum()
"""Function creating the array for the spectrum"""
count = 0
ratioFrame = 6
countFrame = 0
vidcrop=cv2.VideoCapture(video)
vidcrop = cv2.VideoCapture(video)
"""Find if the trailer is in 4:3 or 21:9 --> borders to crop"""
success, image = vidcrop.read()
......@@ -145,7 +149,7 @@ def tableSpectrum(video): #called by imgSpectrum()
while success:
if countFrame == ratioFrame:
countFrame = 0
res[0][count] = meanImage(image,cropdim)
res[0][count] = meanImage(image, cropdim)
for i in range(1, res.shape[0]):
res[i][count] = res[0][count]
count += 1
......@@ -156,39 +160,40 @@ def tableSpectrum(video): #called by imgSpectrum()
return res
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()
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]):
for i in range(cropdim[0], cropdim[1]):
for j in range(cropdim[2], cropdim[3]):
res0 = res0 + lab[i][j]
sumpix=(cropdim[1]-cropdim[0]+1)*(cropdim[3]-cropdim[2]+1)
res0[0]= res0[0] / sumpix
......@@ -200,12 +205,13 @@ def meanImage(img,cropdim): #called by tableSpectrum()
return res
def reading(path): #Called by main()
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()
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