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 !

Helpers.py 3.3 KB
Newer Older
SoleneDc's avatar
SoleneDc committed
1
import ast
SoleneDc's avatar
SoleneDc committed
2
import os
3
import cv2
SoleneDc's avatar
SoleneDc committed
4 5 6

#L = ['Sithad108Og.','fBNpSRtfIUA', '2e-eXJ6HgkQ', 'tN1A2mVnrOM', 'myTaigPrbsg', 'b75lZw8nkvo']

7 8 9 10 11 12 13
genres = [28, 35, 18, 99, 10749, 10752, 10402, 53, 878, 27, 9648, 80, 14, 12, 36, 10769, 16, 10751, 37, 10770]

list_of_eligible_spectrums = []
for file in os.listdir("SpectrumImages2005"):
    if str(file)[-4:] == '.jpg':
        list_of_eligible_spectrums += [file]
print(len(list_of_eligible_spectrums))
SoleneDc's avatar
SoleneDc committed
14

SoleneDc's avatar
SoleneDc committed
15
# def get_genre_from_link():
16
#     path = "./Link-dictionaries/Link-dictionary2005.txt"
SoleneDc's avatar
SoleneDc committed
17 18 19 20 21 22
#     file = open(path, "r").read()
#     dictyear = ast.literal_eval(file)
#     dict_inverse = {}
#     links_to_be_removed = []
#     for movie_id in dictyear.keys():
#         if dictyear[movie_id][1] != []:
23 24 25 26 27 28
#             dict_inverse[str(dictyear[movie_id][2])] = {}
#             for genre in genres:
#                 if genre in dictyear[movie_id][1]:
#                     dict_inverse[str(dictyear[movie_id][2])][genre] = 1
#                 else:
#                     dict_inverse[str(dictyear[movie_id][2])][genre] = 0
SoleneDc's avatar
SoleneDc committed
29
#         else:
30
#             #print(f'careful, link {dictyear[movie_id][2]} needs to be removed from the list')
SoleneDc's avatar
SoleneDc committed
31 32 33 34 35 36
#             links_to_be_removed += [dictyear[movie_id][2]]
#     return dict_inverse, links_to_be_removed
#
#
# def get_output_list(L):
#     dict_inverse, links_to_be_removed = get_genre_from_link()
37
#     eligible_links = []
SoleneDc's avatar
SoleneDc committed
38 39
#     output = []
#     for link in L:
40 41 42 43 44 45 46 47
#         link = str(link)
#         #print(dict_inverse[str(link)])
#         if link[-1] == ".":
#             print("do something! Too many points.......")
#         if link[:-4] not in links_to_be_removed:
#             output += [dict_inverse[link[:-4]]]
#             eligible_links += [link]
#     return output, eligible_links
SoleneDc's avatar
SoleneDc committed
48

49 50

# labels, eligible_links = get_output_list(list_of_eligible_spectrums)
SoleneDc's avatar
SoleneDc committed
51

SoleneDc's avatar
SoleneDc committed
52
def get_genre_from_link():
SoleneDc's avatar
SoleneDc committed
53
    path = "./Link-dictionaries/Link-dictionary2005.txt"
SoleneDc's avatar
SoleneDc committed
54 55 56 57 58 59
    file = open(path, "r").read()
    dictyear = ast.literal_eval(file)
    dict_inverse = {}
    links_to_be_removed = []
    for movie_id in dictyear.keys():
        if dictyear[movie_id][1] != []:
60 61 62 63 64 65
            dict_inverse[str(dictyear[movie_id][2])] = {}
            for genre in genres:
                if genre in dictyear[movie_id][1]:
                    dict_inverse[str(dictyear[movie_id][2])][genre] = 1
                else:
                    dict_inverse[str(dictyear[movie_id][2])][genre] = 0
SoleneDc's avatar
SoleneDc committed
66
        else:
SoleneDc's avatar
SoleneDc committed
67
            #print(f'careful, link {dictyear[movie_id][2]} needs to be removed from the list')
SoleneDc's avatar
SoleneDc committed
68 69 70 71 72
            links_to_be_removed += [dictyear[movie_id][2]]
    return dict_inverse, links_to_be_removed

def get_output_list(L):
    dict_inverse, links_to_be_removed = get_genre_from_link()
SoleneDc's avatar
SoleneDc committed
73
    eligible_links = []
SoleneDc's avatar
SoleneDc committed
74 75
    output = []
    for link in L:
SoleneDc's avatar
SoleneDc committed
76 77
        link = str(link)
        #print(dict_inverse[str(link)])
78
        if link[-1] == ".":
SoleneDc's avatar
SoleneDc committed
79
            print("do something! Too many points.......")
80 81 82 83 84 85 86 87 88 89 90 91
        if link[:-4] not in links_to_be_removed:
            eligible_links += [link[:-4]]
    return dict_inverse, eligible_links


dict_inverse, eligible_links = get_output_list(list_of_eligible_spectrums)

for file in eligible_links:
    img = cv2.imread('SpectrumImages2005/' + file + '.jpg', 1)
    img = img[0:1]
    img = img.reshape((img.shape[1], img.shape[2]))
    dict_inverse[file]['image'] += [img]
SoleneDc's avatar
SoleneDc committed
92

93
print('coucocu')