def resize_at_biggest(): """Agrandi les images à la taille de la plus grande""" mt = MyTools() # La liste de tous les font_a rep = "/media/data/3D/projets/darknet-letters/letters/game/textures/pngs" sub_dirs = mt.get_all_sub_directories(rep) # sub_dirs comprend pngs qui est le root del sub_dirs[0] maxi = 0 # Recherche de l'image la plus grande for sb in sub_dirs: # Liste des images du répertoire imgs = mt.get_all_files_list(sb, ".png") for img_file in imgs: #print("img_file", img_file) img = cv2.imread(img_file, cv2.IMREAD_UNCHANGED) h, w = img.shape[0], img.shape[1] if h > maxi: maxi = h print("maxi", maxi) # Retaillage à maxi for sb in sub_dirs: # Liste des images du répertoire imgs = mt.get_all_files_list(sb, ".png") for img_file in imgs: print("img_file", img_file) img = cv2.imread(img_file, cv2.IMREAD_UNCHANGED) img = cv2.resize(img, (maxi, maxi), interpolation=cv2.INTER_AREA) cv2.imwrite(img_file, img)
def get_square_image(): """Agrandi les images en largeur pour les avoir carrées""" mt = MyTools() # La liste de tous les font_a rep = "/media/data/3D/projets/darknet-letters/letters/game/textures/pngs" sub_dirs = mt.get_all_sub_directories(rep) # sub_dirs comprend pngs qui est le root del sub_dirs[0] print(sub_dirs) for sb in sub_dirs: # Liste des images du répertoire imgs = mt.get_all_files_list(sb, ".png") for img_file in imgs: print("img_file", img_file) img = cv2.imread(img_file, cv2.IMREAD_UNCHANGED) h, w = img.shape[0], img.shape[1] print(h, w) # 337 171 top, bottom = 0, 0 b = int((h - w) / 2) left = right = b try: img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0, 0, 0, 0]) except: print("Largeur plus grande que hauteur pour l'image", img_file) cv2.imwrite(img_file, img)
def get_text_str_from_blender(dossier): mt = MyTools() text_file_list = mt.get_all_files_list(dossier, '.txt') print('Liste des fichiers texte =', text_file_list) text_str = get_str(text_file_list) return text_str
def get_text_str_from_this_script(): mt = MyTools() text_file_list = mt.get_all_files_list('./texte', '.txt') print('Liste des fichiers texte =', text_file_list) text_str = get_str(text_file_list) return text_str
def regroupage_font_images(): """Regroupage des images de lettres d'une font, minuscule ou majuscule 20 images 445x445 5 x 445 = 2225 = w 5 x 445 = 2225 = h abcde fghij klmno pqrst 54ème ligne vide """ mt = MyTools() textures = "/media/data/3D/projets/darknet-letters/letters/game/textures/" # La liste de tous les font_a rep = "/media/data/3D/projets/darknet-letters/letters/game/textures/pngs" sub_dirs = mt.get_all_sub_directories(rep) # sub_dirs comprend pngs qui est le root del sub_dirs[0] for sb in sub_dirs: # de /medi...../textures/pngs/font_6, récup de 6=font #font = sb[-6:][-1] font = sb[-1] # dtype=np.uint8 minuscules = np.zeros((2225, 2225, 4)) majuscules = np.zeros((2225, 2225, 4)) # Liste des images du répertoire imgs = mt.get_all_files_list(sb, ".png") for img_file in imgs: #print("img_file", img_file) # position et maj_min position, maj_min = get_position_and_maj_min(img_file) if position != (-1, -1): # l'image img = cv2.imread(img_file, cv2.IMREAD_UNCHANGED) # agglomération des images if maj_min == "min": minuscules = paste_image(minuscules, img, position[0], position[1]) else: majuscules = paste_image(majuscules, img, position[0], position[1]) # Enreg des 2 fichiers dans le dossiers textures minuscule_file = textures + "minuscule_" + str(font) + ".png" cv2.imwrite(minuscule_file, minuscules) print("Enregistrement de:", minuscule_file) majuscules_file = textures + "majuscules_" + str(font) + ".png" cv2.imwrite(majuscules_file, majuscules)
def test(dossier, essai): """ best = 41000 last = 43000 final = 42000 """ mt = MyTools() w_dir = "/media/data/projets/darknet-letters/letters/darknet/data_22/backup/" w_list = mt.get_all_files_list(w_dir, ".weights") #print("\n\nTous les fichiers de poids:", w_list) for w in w_list: # Pas le dossier principal if w: print("\n\nFichier de poids en cours:", w, "\n\n") if w != w_dir: play_letters(dossier, essai, test=1, weights=w)
class FontsChooser: """ /media/data/3D/projets/darknet-letters/letters/ttf_to_png/pngs/CognacRum/Z.png """ def __init__(self): self.tools = MyTools() # Liste des images du répertoire root = "/media/data/3D/projets/darknet-letters/letters/ttf_to_png" imgs = self.tools.get_all_files_list(root + "/pngs", ".png") # [... , CognacRum ,Z.png] for img in imgs: if "a.png" in img or "A.png" in img: name_list = img.split("/") name = name_list[-2] + "_" + name_list[-1] file_name = root + "/fonts_chooser/" + name print(file_name) image = cv2.imread(img, cv2.IMREAD_UNCHANGED) cv2.imwrite(file_name, image)
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- import random from pymultilame import MyTools mt = MyTools() # Dossier des images et txt shot_dir = '/media/data/3D/projets/semaphore_cv_yolo/get_opencv_shot/shot/' # liste de toutes les images files = mt.get_all_files_list(shot_dir, '.jpg') # Rebat les cartes pour prendre les fichiers au hazard dans les sous-dossiers random.shuffle(files) print("Nombre de fichiers", len(files)) train_num = 54000 test_num = 6000 counter = 0 train = "" test = "" a,b,c,d,e = 0,0,0,0,0 for f in files: if counter < train_num: train += f + "\n" if "_a.jpg" in f: a += 1
class TtftoPng: def __init__(self, size): self.size = size self.tools = MyTools() # 10 polices possible seulement self.colors = COLORS self.color_num = 0 # Create pngs directory self.tools.create_directory("./pngs") # Dict des dossiers avec ttf self.ttf_list = self.get_ttfs() # Création des sous dossiers dans ./pngs self.create_sub_directories() def convert_ttfs(self): """Convertit tous les ttfs""" for rep in self.ttf_list: # récup du ttf ttf = self.tools.get_all_files_list(rep, 'ttf') print("Conversion de:", rep) self.convert_ttf(rep, ttf) self.color_num += 1 def convert_ttf(self, rep, ttf): """Convertit un ttf""" for l in LETTRES: l = l.upper() self.convert_letter(l, rep, ttf) l = l.lower() self.convert_letter(l, rep, ttf) # #for c in CHIFFRES: # #self.convert_letter(c, rep, ttf) def convert_letter(self, letter, rep, ttf): """Convertit une lettre Conversion de: ./ttfs/southern/Southern.ttf lettre: A dans: ./pngs/southern/ command convert -background none -fill black -font ./ttfs/southern/Southern.ttf -pointsize 300 -write ./pngs/southern/ label:"A" A.png """ #print("Conversion de {}".format(letter)) filename = "./pngs" + rep[6:] + "/" + letter + ".png" if len(ttf) > 0: font = "./" + ttf[0] # remplacement espace dans le nom, # mais dossier ne doit pas avoir d'espace font = font.replace(' ', '\\ ') color = self.colors[self.color_num] # #print( "Conversion de: ", font, # #" lettre: ", letter, # #" dans: ", filename, # #"color", color) command = 'convert -background none -fill {4} -font {3} \ -pointsize {1} label:"{0}" {2}'.format( letter, self.size, filename, font, color) #print("command", command) subprocess.call(command, shell=True) def create_sub_directories(self): """"Création des sous dossiers dans ./pngs""" for d in self.ttf_list: parts = d.split("/") self.tools.create_directory("./pngs/" + parts[-1]) def get_ttfs(self): # Tous les fichiers ttf ttfs = self.tools.get_all_sub_directories("./ttfs") ttf_list = [] for subdir in ttfs: if subdir != "./ttfs": dd = self.tools.get_all_sub_directories(subdir) for d in dd: if "MACOSX" not in d: if subdir not in ttf_list: ttf_list.append(subdir) return ttf_list
class GetVideoWithSemaphore: def __init__(self, video_in, video_out, lenght, every): """ """ self.video_in = video_in self.video_out = video_out self.lenght = lenght self.every = every * 24 self.y_fix = 0 self.x_pos = 0 self.y_pos = 0 # mes outils perso: pymultilame self.tools = MyTools() self.letters = self.get_letters_images() self.numero_lettre = 0 # Video d'entrèe self.video_in = cv2.VideoCapture(video_in) self.width = int(self.video_in.get(cv2.CAP_PROP_FRAME_WIDTH)) self.height = int(self.video_in.get(cv2.CAP_PROP_FRAME_HEIGHT)) self.FPS = self.video_in.get(cv2.CAP_PROP_FPS) print("Video d'entrèe:", self.width, self.height, self.FPS) fourcc = cv2.VideoWriter_fourcc(*'MPEG') self.video = cv2.VideoWriter(self.video_out, fourcc, float(self.FPS), (self.width, self.height)) # Lance la création de la video self.get_semaphore_video() def get_letters_files(self): letters_files = self.tools.get_all_files_list("lettre", ".png") print("Nombre de letters =", len(letters_files)) return letters_files def get_letters_images(self): """Récupère toutes les images de lettres""" letters = {} for l_file in self.get_letters_files(): # lettre/shot_10_j.png l = l_file[:-4][-1] letters[l] = cv2.imread(l_file, -1) return letters def overlay(self, frame, lettre_img, lettre, change): """ lettre 320x320 variation 100 à 800 frame 1024x1024 position exempe 624: 1024-624 = 400 """ # Variation sur la taille du sémaphore semaphore_mini = int(self.height / 7) w, h = lettre_img.shape[1], lettre_img.shape[0] # Set de la hauteur, puis largeur if change == 1: # changement seulement à every y_size = random.randint(semaphore_mini, int(self.height * 0.8)) self.y_fix = y_size else: y_size = self.y_fix x_size = int(w * y_size / h) # Maxi de taille de lettre_img < self.height y_size = min(y_size, self.height) x_size = min(x_size, self.height) lettre_img = cv2.resize(lettre_img, (x_size, y_size), interpolation=cv2.INTER_CUBIC) # Position posible if change == 1: # changement seulement à every x = random.randint(0, self.height - x_size) self.x_pos = x y = random.randint(0, self.height - y_size) self.y_pos = y else: x = self.x_pos y = self.y_pos img = over_transparent(frame, lettre_img, x, y) # Overlay du caractère l font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, lettre, (10, int(self.height * 0.8)), font, 4, (255, 255, 255), 2, cv2.LINE_AA) return img def get_lettre(self): # Lecture des lettres de l'alphabet en boucle l = list(self.letters.keys())[self.numero_lettre] print("Lettre en cours", l, self.numero_lettre) self.numero_lettre += 1 if self.numero_lettre == 27: self.numero_lettre = 0 lettre_img = self.letters[l] return lettre_img, l def get_semaphore_video(self): """Une lettre ajoutée et changée toutes les every secondes""" how_many = 0 letter_duration = 0 loop = 1 change = 1 # Initialisation lettre_img, lettre = self.get_lettre() while loop: ret, frame = self.video_in.read() letter_duration += 1 if letter_duration == self.every: # lettre_img est l'image de la lettre lettre lettre_img, lettre = self.get_lettre() change = 1 letter_duration = 0 # Overlay d'une lettre frame = self.overlay(frame, lettre_img, lettre, change) change = 0 cv2.imshow('Out', frame) self.video.write(frame) # Jusqu'à la durée demandée how_many += 1 if how_many == self.FPS * self.lenght: loop = 0 # Echap et attente k = cv2.waitKey(2) if k == 27: loop = 0 self.video.release() cv2.destroyAllWindows()
class CreateShot: def __init__(self, size, video, number=1000): """ letters = {'a': image de a, ' ': image de ' ', etc ...} """ self.size = size self.number = number self.tools = MyTools() self.create_directories() self.letters = self.get_letters_images() self.numero_lettre = 0 self.capture = cv2.VideoCapture(video) self.width = int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH)) self.height = int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) print("Taille Video:", self.width, self.height) self.get_shots() def create_directories(self): """Un dossier root=shot, un sous dossier par lettre""" self.tools.create_directory("./shot") for l in LETTRES: if l == " ": l = "space" self.tools.create_directory("./shot/shot_" + l) def get_letters_files(self): letters_files = self.tools.get_all_files_list("lettre_epaisse_alpha", ".png") print("Nombre de letters =", len(letters_files)) return letters_files def get_letters_images(self): """Récupère toutes les images de lettres""" letters = {} for l_file in self.get_letters_files(): # lettre/shot_10_j.png l = l_file[:-4][-1] img = cv2.imread(l_file, -1) img = delete_gray(img) letters[l] = img return letters def frame_resize(self, frame): """Pour 1024: resize puis coupe des 2 cotés de 398 pour 416: 1280x720 devient 739x416 """ # Largeur a = int(self.width * self.size / self.height) # Hauteur b = self.size # Resize frame = cv2.resize(frame, (a, b), interpolation=cv2.INTER_AREA) # Coupe x = int((a - b) / 2) y = 0 w, h = self.size, self.size frame = frame[y:y + h, x:x + w] return frame def aspect_change(self, lettre): """Couleur, flou, rotation lettre est l'image de la lettre """ bg_color = lettre[0][0] R = random.randint(0, 100) G = random.randint(0, 50) B = random.randint(100, 255) lettre[np.all(lettre == [0, 0, 0, 255], axis=2)] = [R, G, B, 255] mask = np.all(lettre == bg_color, axis=2) lettre[mask] = [0, 0, 0, 0] # Rotation some = random.randint(-3, 3) lettre = rotateImage(lettre, some) # Flou de 0 à 4 flou = random.randint(0, 4) if flou != 0: lettre = cv2.blur(lettre, (flou, flou)) return lettre def lettre_image_change(self, lettre): """Adapte une lettre, calcule taille, position, flou, couleur lettre est l'image de la lettre """ # Modification de l'aspect lettre = self.aspect_change(lettre) # Variation sur la taille du sémaphore semaphore_mini = int(self.size / 7) w, h = lettre.shape[1], lettre.shape[0] # Set de la hauteur, puis largeur y_size = random.randint(semaphore_mini, int(self.size * 0.8)) x_size = int(w * y_size / h) # Maxi de taille de lettre < self.size y_size = min(y_size, self.size) x_size = min(x_size, self.size) lettre = cv2.resize(lettre, (x_size, y_size), interpolation=cv2.INTER_CUBIC) # Position posible x = random.randint(0, self.size - x_size) y = random.randint(0, self.size - y_size) return lettre, x_size, y_size, x, y def overlay(self, frame): """Trouve une lettre, supperpose à la frame""" # Lecture des lettres de l'alphabet en boucle l = LETTRES[self.numero_lettre] self.numero_lettre += 1 if self.numero_lettre == 27: self.numero_lettre = 0 # lettre est l'image de lettre lettre = self.letters[l] # Adaptation lettre, x_size, y_size, x, y = self.lettre_image_change(lettre) # Overlay img = over_transparent(frame, lettre, x, y) return img, l, x_size, y_size, x, y def save(self, img, lettre, x_size, y_size, x, y, n): """Enregistrement dans le dossier ./shot/shot_a/ de shot_0_a.jpg et shot_0_a.txt class position(x y) taille(x y) x, y position du coin haut gauche s = taille de la lettre """ # Get indice avant modif liste indice = LETTRES.index(lettre) # pour avoir dossier clair if lettre == " ": lettre = "space" # Avec sous dossiers fichier = "./shot/shot_" + lettre + "/shot_" + str(n) + "_" + lettre # Enregistrement de l'image cv2.imwrite(fichier + ".jpg", img) # Enregistrement des datas # Taille relative de l'image lettre tx = x_size / self.size ty = y_size / self.size # Origine = top left xc = (x + (x_size / 2)) / self.size yc = (y + (y_size / 2)) / self.size # class-position du centre-taille data = (str(indice) + " " + str(xc) + " " + str(yc) + " " + str(tx) + " " + str(ty)) self.tools.write_data_in_file(data, fichier + ".txt", "w") def get_shots(self): n, m = 0, 0 self.loop = 1 while self.loop: ret, frame = self.capture.read() if ret: # pour enregistrer 1 frame sur 10 et varier les fonds if n % 10 == 0: frame = self.frame_resize(frame) # Overlay d'une lettre img, l, x_size, y_size, x, y = self.overlay(frame) print("Shot numéro", m, "Lettre", l) # Enregistrement de l'image et du fichier txt self.save(img, l, x_size, y_size, x, y, m) m += 1 cv2.imshow("image", img) # Jusqu'à number n += 1 if m == self.number: self.loop = 0 # Echap et attente k = cv2.waitKey(10) if k == 27: self.loop = 0 else: # Reset si la video est terminée self.capture = cv2.VideoCapture(video) print("Nombre d'images crées:", m) cv2.destroyAllWindows()
# Pas d'import possible direct du dossier parent # ValueError: attempted relative import beyond top-level package sys.path.append("..") from letters_path import LettersPath lp = LettersPath() letters_dir = lp.letters_dir CONF = lp.conf mt = MyTools() # Dossier des images et txt SHOT_JPG = lp.shot_jpg_dir # liste de toutes les images files = mt.get_all_files_list(SHOT_JPG, '.jpg') # Rebat les cartes pour prendre les fichiers au hazard dans les sous-dossiers random.shuffle(files) nb = len(files) print("Nombre de fichiers", nb) if nb == 0: print("\n\nErreur:") print("Définir les chemins de shot_jpg dans letters.ini\n\n") os._exit(0) train_num = int(0.9 * nb) counter = 0 train = "" test = ""
class BlurAndConvert: def __init__(self): self.mt = MyTools() self.blur_mini = CONF["letters_shot_jpg"]["blur_mini"] self.blur_maxi = CONF["letters_shot_jpg"]["blur_maxi"] self.size = CONF["letters_shot_jpg"]["shot_size"] # Dossiers self.shot = shot_dir print("Dossier shot:", self.shot) self.create_shot_jpg() self.create_sub_directories() # Images png self.all_png_files = self.mt.get_all_files_list(self.shot, '.png') print("Nombre de fichiers à convertir:", len(self.all_png_files)) if len(self.all_png_files) == 0: print("\n\nPas d'images à convertir") print("Créer les images avant !") os._exit(0) # Exécution du script flou puis save self.save_to_jpg() def create_shot_jpg(self): self.shot_jpg = shot_jpg print("Dossier shot_jpg:", self.shot_jpg) # Si le dossier n'existe pas, je le crée self.mt.create_directory(self.shot_jpg) def create_sub_directories(self): """Création de 100 dossiers.""" # Répartition dans 100 sous dossiers for l in range(100): directory = os.path.join(self.shot_jpg, str(l)) self.mt.create_directory(directory) def blur(self, img): # Flou k = random.randint(self.blur_mini, self.blur_maxi) if k != 0: img = cv2.blur(img, (k, k)) return img def save_to_jpg(self): n = 0 for png in self.all_png_files: if n % 100 == 0 and n != 0: a = len(self.all_png_files) print("Nombre de fichiers convertis:", n, "sur", a) n += 1 # Lecture de png img = cv2.imread(png) # Flou img = self.blur(img) # Retaillage avec size de letters.ini img = cv2.resize(img, (self.size, self.size), interpolation=cv2.INTER_AREA) # On travaille avec Path png_path = Path(png) jpg_path = self.get_jpg_name(png_path) # PosixPath # Ecriture de l'image jpg, cv2 prend seulement path en str cv2.imwrite(str(jpg_path), img, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) # Copie du fichier txt de png dans jpg txt, dst = self.get_txt_dst(png_path, jpg_path) copyfile(txt, dst) # On prend son temps sleep(0.01) print("Nombre de fichiers convertis:", n) def get_jpg_name(self, png_path): """ png = str png = str(self.shot) + /25/shot_4126.png = str jpg = str(self.shot_jpg) + /25/shot_4126.jpg = str """ # Soustraction du chemin de shot_dir a = str(png_path).replace(str(shot_dir), "") # Ajout du chemin de jpg_dir b = Path(str(shot_jpg) + a) # Changement de l'extension jpg_path = b.with_suffix(".jpg") return jpg_path def get_txt_dst(self, png_path, jpg_path): txt_path = png_path.with_suffix(".txt") dst_path = jpg_path.with_suffix(".txt") return txt_path, dst_path
class BlurAndConvert: def __init__(self): self.mt = MyTools() self.blur_min = CONF["play_letters_shot"]["blur_min"] self.blur_max = CONF["play_letters_shot"]["blur_max"] self.size = CONF["play_letters_shot"]["shot_size"] # Dossiers self.shot = CONF["play_letters_shot"]["pl_shot"] print("Dossier play_letters:", self.shot) self.shot_jpg = self.shot + "_jpg" print("Dossier play_letters jpg:", self.shot_jpg) self.create_shot_jpg_dir() # Liste des sous-dossiers avec le dossier play_letters_shot self.sub_directories_list = [x[0] for x in os.walk(self.shot)] print("Liste des sous-répertoires:", self.sub_directories_list) self.create_sub_directories() # Copie des txt self.copy_all_txt() # Images png self.all_png_files = self.mt.get_all_files_list(self.shot, '.png') print("Nombre de fichiers à convertir:", len(self.all_png_files)) if len(self.all_png_files) == 0: print("\n\nPas d'images à convertir") print("Créer les images avant !") os._exit(0) # Exécution du script flou puis save self.save_to_jpg() def copy_all_txt(self): """ /bla...bla/play_letters_shot/bob_sherif/instruments.txt to /bla...bla/play_letters_shot_jpg_6/bob_sherif/instruments.txt """ # Tous les txt txts = self.mt.get_all_files_list(self.shot, '.txt') for txt in txts: dst = txt.replace(self.shot, self.shot_jpg) # Copie du fichier txt des png dans les jpg copyfile(txt, dst) def create_shot_jpg_dir(self): print("Dossier pl_shot_jpg:", self.shot_jpg) # Si le dossier n'existe pas, je le crée self.mt.create_directory(self.shot_jpg) def create_sub_directories(self): """Création des sous dossiers.""" for sd in self.sub_directories_list: if sd != self.shot: print("Sous répertoire en png:", sd) # Soustraction du chemin de shot_dir shot_dir = Path(self.shot) a = str(sd).replace(str(shot_dir), "") # Ajout du chemin de jpg_dir b = Path(str(self.shot_jpg) + a) print("Sous répertoire en jpg:", b) self.mt.create_directory(b) def apply_blur(self, img): blur = random.randint(self.blur_min, self.blur_max) if blur != 0: img = cv2.blur(img, (blur, blur)) return img def save_to_jpg(self): n = 0 for png in self.all_png_files: if n % 100 == 0 and n != 0: a = len(self.all_png_files) print("Nombre de fichiers convertis:", n, "sur", a) n += 1 # print(png) # Lecture de png img = cv2.imread(png) # Retaillage avec size de letters.ini img = cv2.resize(img, (self.size, self.size), interpolation=cv2.INTER_AREA) # Flou img = self.apply_blur(img) # On travaille avec Path png_path = Path(png) jpg_path = self.get_jpg_name(png_path) # PosixPath # Ecriture de l'image jpg, cv2 prend seulement path en str cv2.imwrite(str(jpg_path), img, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) # On prend son temps sleep(0.01) print("Nombre de fichiers convertis:", n) def get_jpg_name(self, png_path): """ png = str png = str(self.shot) + /25/shot_4126.png = str jpg = str(self.shot_jpg) + /25/shot_4126.jpg = str """ # Soustraction du chemin de shot_dir a = str(png_path).replace(str(self.shot), "") # Ajout du chemin de jpg_dir b = Path(str(self.shot_jpg) + a) # Changement de l'extension jpg_path = b.with_suffix(".jpg") return jpg_path
class YOLO: def __init__(self, images_directory, essai, save, test=0, weights=""): """ essai = numéro del'eaasi dans ini save = sauvegarde des notes en json test = bidouilles pour passer une série de weights """ # Mes outils self.mt = MyTools() self.lp = LettersPath() self.CONF = self.lp.conf self.essai = essai self.save = save self.test = test self.weights = weights if self.weights: print("Fichier de poids utilisé:", self.weights) self.get_weights_file_indice() if self.weights: self.create_test_subdir() # Paramètres de la conf # Avec ou sans GPU self.gpu = self.CONF['play_letters']['gpu'] # Résolution de l'écran ou du VP: x, y self.screen = self.CONF['play_letters']['screen'] # Boucle opencv self.loop = 1 self.fps = 0 # Récup des images self.images_directory = images_directory self.shot_list = self.get_sorted_shot_list() self.all_shot = self.get_all_shot() # nom du morceau self.filename = self.images_directory.split("/")[-1] # Initialisation de la détection self.set_darknet() # Paramètres de détections self.thresh = int(self.CONF['play_letters']['thresh']) self.hier_thresh = int(self.CONF['play_letters']['hier_thresh']) self.nms = int(self.CONF['play_letters']['nms']) # Windows self.create_windows() # Trackbars self.create_trackbar() self.set_init_tackbar_position() # Midi self.fonts = self.CONF['music_and_letters']['fonts'] self.instruments = [] self.get_instruments() self.notes_en_cours = [] self.players = {} self.set_players() self.all_notes = [] def create_windows(self): cv2.namedWindow('Reglage') cv2.moveWindow('Reglage', 0, 25) self.fullscreen = self.CONF['play_letters']['fullscreen'] if self.fullscreen: cv2.namedWindow('Letters', cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty('Letters', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) else: cv2.namedWindow('Letters') cv2.moveWindow('Letters', 0, 25) self.titre = 0 b = int(416 * self.screen[0] / self.screen[1]) self.black_image = np.zeros((416, b, 3), np.uint8) def get_weights_file_indice(self): """Uniquement pour test weightpath = ../darknet/data_22/backup/yolov3-tiny_3l_22_best.weights début = ../darknet/data_22/backup/yolov3-tiny_3l_22_ fin = .weights """ a = len( "/media/data/projets/darknet-letters/letters/darknet/data_22/backup/yolov3-tiny_3l_22_" ) b = len(".weights") indice = str(self.weights[a:-b]) return indice def create_test_subdir(self): doss = "/media/serge/BACKUP/play_letters_shot/pl_shot_14_jpg/test/" for i in range(43): directory = doss + str((i + 1) * 1000) self.mt.create_directory(directory) def set_darknet(self): """Si weight_influence_test, teste tous les yolov3-tiny_3l_22_xxxxxxx.weights du dossier backup free_network = lib.api_free_network free_network.argtypes = [c_void_p] adapté à ce script: free_network = darknet.lib.api_free_network free_network.argtypes = [c_void_p] """ configPath = self.CONF['play_letters']['configpath'] if not self.test: weightPath = self.CONF['play_letters']['weightpath'] else: weightPath = self.weights metaPath = self.CONF['play_letters']['metapath'] self.netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) self.metaMain = darknet.load_meta(metaPath.encode("ascii")) with open(metaPath) as metaFH: metaContents = metaFH.read() match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") self.altNames = [x.strip() for x in namesList] except TypeError: print("Erreur self.altNames") if self.gpu: self.free_network = darknet.lib.api_free_network # Create an image we reuse for each detect self.darknet_image = darknet.make_image(\ darknet.network_width(self.netMain), darknet.network_height(self.netMain), 3) def get_instruments(self): """Récupère les infos de instruments.txt dans self.instruments.""" file_name = self.images_directory + "/instruments.txt" data = self.mt.read_file(file_name) if data: lines = data.splitlines() for line in lines: line_list = line.split(" ") self.instruments.append(line_list) else: print("Pas de fichier txt donc pas d'instruments !") print(" Morceau suivant ....\n\n") self.instruments = None self.loop = 0 def set_players(self): """Crée les players pour chaque canal Les drums ne sont pas sur le channel 9, complique et sert à rien la font i est sur le canal i for i in range(len(self.instruments)): self.instruments = [[0, 12, 5], [0, 0, 1]] [bank, bank_number = instrument, font] """ if self.instruments: i = 0 for instrument in self.instruments: channel = int(instrument[2]) bank = int(instrument[0]) bank_number = int(instrument[1]) self.players[channel] = OneInstrumentPlayer( self.fonts, channel, bank, bank_number) i += 1 print("Nombre de player:", len(self.players)) def create_trackbar(self): """ thresh min 0 max 1 hier_thresh min 0 max 1 nms min 0 max 1 """ self.reglage_img = np.zeros((100, 600, 3), np.uint8) # #self.reglage_img = put_text(self.reglage_img, # #self.filename, # #(10, 50), # #size=0.8, # #thickness=2) cv2.createTrackbar('threshold_', 'Reglage', 0, 100, self.onChange_thresh) cv2.createTrackbar('hier_thresh', 'Reglage', 0, 100, self.onChange_hier_thresh) cv2.createTrackbar('nms_____', 'Reglage', 0, 100, self.onChange_nms) def set_init_tackbar_position(self): """setTrackbarPos(trackbarname, winname, pos) -> None""" cv2.setTrackbarPos('threshold_', 'Reglage', self.thresh) cv2.setTrackbarPos('hier_thresh', 'Reglage', self.hier_thresh) cv2.setTrackbarPos('nms_____', 'Reglage', self.nms) def onChange_thresh(self, thresh): """min=1 max=100 step=1 default=0.5""" if thresh == 0: thresh = 5 if thresh == 100: thresh = 95 self.thresh = int(thresh) self.save_change('play_letters', 'thresh', self.thresh) def onChange_hier_thresh(self, hier_thresh): """min=1 max=100 step=1 default=0.5""" if hier_thresh == 0: hier_thresh = 5 if hier_thresh == 100: hier_thresh = 95 self.hier_thresh = int(hier_thresh) self.save_change('play_letters', 'hier_thresh', self.hier_thresh) def onChange_nms(self, nms): """min=1 max=100 step=1 default=0.5""" if nms == 0: nms = 5 if nms == 100: nms = 95 self.nms = int(nms) self.save_change('play_letters', 'nms', self.nms) def save_change(self, section, key, value): lp.save_config(section, key, value) def get_sorted_shot_list(self): images = self.mt.get_all_files_list(self.images_directory, ".jpg") shot_list = [0] * len(images) # Récup du numéro le plus petit, les numéros ensuite se suivent mini = 10000000 for image in images: nbr = int(image.split("/")[-1].split("_")[-1][:-4]) if nbr < mini: mini = nbr # Tri des images n = 0 for image in images: # Si de 500 à 1500 # ../play_letters/s_j_to_i_1243.jpg devient s_j_to_i_1243.jpg nbr = int(image.split("/")[-1].split("_")[-1][:-4]) # 1243 shot_list[nbr - mini] = image return shot_list def get_all_shot(self): """Charge en mémoire toutes les images du dossiers à lire par l'IA""" print("\n\nChargement de toutes les images en RAM. Patience ...\n\n") all_shot = [] for shot_file in self.shot_list: img = cv2.imread(shot_file) all_shot.append(img) return all_shot def notes_cleaning(self, notes): # Suppression des doublons clean_notes = [] for note in notes: if note not in clean_notes: clean_notes.append(note) # Validation des notes new_notes = [] for font, note, vol in notes: # self.instruments = [[0, 12, 5], [0, 0, 1]] all_fonts = [] for inst in self.instruments: all_fonts.append(int(inst[2])) if font not in all_fonts: font = None # note 1 à 127 if note < 1 or note > 127: note = None # ## Volume 0 à 127: # #if vol > 127: vol = 127 # #if vol < 0: vol = 0 # Le volume est forcé à 127 vol = 127 if font is not None: if note: new_notes.append([font, note, vol]) return new_notes def play_notes(self, notes): """new_notes = [(police, note, volume), ...] = [(5, 124, 127), ... ] la police n'est pas le player self.players[i].thread_dict[key] = 0 """ new_notes = self.notes_cleaning(notes) # Notes en en_cours ****************************************** en_cours = [] # key=note, val=thread en cours 0 ou 1 for k, v in self.players.items(): for key, val in self.players[k].thread_dict.items(): if val != 0: en_cours.append((k, key)) # #print("en_cours:", en_cours) # Fin des notes qui ne sont plus en en_cours ***************** # notes = [(player, note, volume), ...] # en_cours = [(player, note), ... ] for ec in en_cours: player, note = ec ssss = [player, note, 127] # list et non tuple !! if ssss not in new_notes: self.players[player].thread_dict[note] = 0 # #print("Fin de:", player, note) # Lancement des nouvelles notes ****************************** for player, note, vol in new_notes: if (player, note) not in en_cours: # #print("nouvelle", player, note) self.players[player].thread_play_note(note, 127) # vol) def save_all_notes(self): """ /bla...bla/play_letters_shot_jpg_3/bob_sheriff to /bla...bla/play_letters_shot_jpg_3/bob_sheriff_data.json """ if not self.weights: json_name = self.images_directory + "_" + str(self.essai) + ".json" else: # Bidouille non générale a = len("/media/serge/BACKUP/play_letters_shot/pl_shot_14_jpg/") name = self.images_directory[a:] + "_" # les fichiers sont dans 1 sous dossier indice indice = str(self.get_weights_file_indice()) doss = "/media/serge/BACKUP/play_letters_shot/pl_shot_14_jpg/test/" json_name = doss + indice + "/" + name + str(self.essai) + ".json" with open(json_name, 'w') as f_out: json.dump(self.all_notes, f_out) f_out.close() print('Enregistrement de:', json_name) def put_titre(self, image): """Insère le titre si i""" if self.titre: filename = self.filename filename = filename.replace("f_", "") filename = filename.replace("_", " ") filename = filename.replace("-", " ") image = put_text(image, filename, (10, 50), size=0.5, thickness=1) return image def apply_k(self, k, i): # Space pour morceau suivant et attente if k == 32: self.loop = 0 # Affichage du titre si "i" if k == ord('i'): # 105: if not self.titre: self.titre = 1 else: self.titre = 0 # Echap pour finir le script python if k == 27: os._exit(0) # Gestion de la fin du morceaux if i == len(self.shot_list): self.loop = 0 def detect(self): """FPS = 40 sur GTX1060""" i = 0 fps = 0 t_init = time.time() tempo = 1 t_tempo = time.time() # Si pas d'image, on passe la boucle if not self.all_shot: self.loop = 0 while self.loop: black_image = self.black_image.copy() # Récup d'une image img = self.all_shot[i] # ## Capture des positions des sliders # #self.thresh = cv2.getTrackbarPos('threshold_','Reglage') # #self.hier_thresh = cv2.getTrackbarPos('hier_thresh','Reglage') # #self.nms = cv2.getTrackbarPos('nms','Reglage') # #img_resized = cv2.resize(img, # #(darknet.network_width(self.netMain), # #darknet.network_height(self.netMain)), # #interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(self.darknet_image, img.tobytes()) detections_l = darknet.detect_image(self.netMain, self.metaMain, self.darknet_image, self.thresh / 100, self.hier_thresh / 100, self.nms / 100) # Application des détections dans l'image image, letters = cvDrawBoxes(detections_l, img) notes = letters_to_notes(letters) self.play_notes(notes) # Ajout des notes pour enregistrement à la fin if self.save: self.all_notes.append(notes) # Insertion du titre image = self.put_titre(image) if not self.fullscreen: img = cv2.resize(image, (600, 600), interpolation=cv2.INTER_LINEAR) else: # gray[y1:y2, x1:x2] 162:578 # 1440/900 = 1.6 # #a = (self.screen[0]/self.screen[1] -1) / 2 # #x1 = int(a*416) # #x2 = x1 + 416 # #y1 = 0 # #y2 = 416 # #black_image[y1:y2, x1:x2] = image img = image # black_image img = put_text(img, str(self.fps), (10, 100), size=0.5, thickness=1) # Affichage cv2.imshow('Letters', img) # image) # Affichage des trackbars cv2.imshow('Reglage', self.reglage_img) # Comptage i += 1 # prochaine image fps += 1 ta = time.time() # Pour fps = 40 soit ta - t_tempo = 0.025 # 0.035 pour fps = 40, 0.052 pour fps = 30 tempo = int(1000 * (0.052 - (ta - t_tempo))) if tempo < 1: tempo = 1 t_tempo = ta if ta > t_init + 1: self.fps = fps # #print("FPS =", round(fps, 1)) t_init = time.time() fps = 0 k = cv2.waitKey(tempo) self.apply_k(k, i) cv2.destroyAllWindows() # Libération de la mémoire GPU if self.gpu: self.free_network(self.netMain) # Enregistrement des notes if self.save: self.save_all_notes() # Fin des fluidsynth for k, v in self.players.items(): self.players[k].stop_audio() time.sleep(0.3)
class ResizeTrainingShot: def __init__(self, root, size): self.root = root # soit ..../semaphore self.size = int(size) # Mes outils personnels self.tools = MyTools() # Renommage de training_shot en training_shot_copy self.rename_training_shot() # Re-création des dossiers self.create_training_shot_dir() self.create_sub_folders() # Liste des images self.shot_list = self.get_shot_list() def rename_training_shot(self): ori = os.path.join(self.root, "training_shot") dest = os.path.join(self.root, "training_shot_copy") os.rename(ori, dest) def create_training_shot_dir(self): directory = os.path.join(self.root, "training_shot") print("Dossier training_shot:", directory) self.tools.create_directory(directory) def create_sub_folders(self): """Création de n dossiers shot_000""" # Nombre de dossiers nécessaires d = os.path.join(self.root, "training_shot_copy") n = len(self.tools.get_all_sub_directories(d)) - 1 print("Nombre de sous répertoires", n) for i in range(n): directory = os.path.join(self.root, 'training_shot', 'shot_' + str(i).zfill(3)) self.tools.create_directory(directory) print("Sous répertoires créés") def get_shot_list(self): """Liste des images""" # Liste shot = os.path.join(self.root, "training_shot_copy") shot_list = self.tools.get_all_files_list(shot, ".png") print("Dossier des images NB:", shot) print("Nombre d'images:", len(shot_list)) return shot_list def change_resolution(self, img, x, y): """Une image peut-être ratée""" try: res = cv2.resize(img, (x, y), interpolation=cv2.INTER_AREA) except: res = np.zeros([self.size, self.size, 1], dtype=np.uint8) return res def get_new_name(self, shot): return shot.replace("/training_shot_copy/", "/training_shot/") def create_training_shot_resized_dir(self): directory = os.path.join(self.root, "training_shot_resized") print("Dossier training_shot_resized:", directory) self.tools.create_directory(directory) def batch(self): """Lecture, resize, save""" i = 0 # Pour chaque image for shot in self.shot_list: # Lecture img = cv2.imread(shot, 0) # Resize img_out = self.change_resolution(img, self.size, self.size) i += 1 # Save new_shot = self.get_new_name(shot) print(new_shot) cv2.imwrite(new_shot, img_out)
class GetVideoWithSemaphore: def __init__(self, video_in, video_out, lenght, every): """ """ self.video_in = video_in self.video_out = video_out self.lenght = lenght self.every = every * 24 # [[y_size, x_pos, y_pos]] self.size_pos_list = [] # mes outils perso: pymultilame self.tools = MyTools() self.letters = self.get_letters_images() self.numero_lettre = 0 # Video d'entrèe self.video_in = cv2.VideoCapture(video_in) self.width = int(self.video_in.get(cv2.CAP_PROP_FRAME_WIDTH)) self.height = int(self.video_in.get(cv2.CAP_PROP_FRAME_HEIGHT)) self.FPS = self.video_in.get(cv2.CAP_PROP_FPS) print("Video d'entrèe:", self.width, self.height, self.FPS) fourcc = cv2.VideoWriter_fourcc(*'MPEG') self.video = cv2.VideoWriter(self.video_out, fourcc, float(self.FPS), (self.width, self.height)) # Lance la création de la video self.get_semaphore_video() def get_letters_files(self): letters_files = self.tools.get_all_files_list("lettre_epaisse_alpha", ".png") print("Nombre de letters =", len(letters_files)) return letters_files def get_letters_images(self): """Récupère toutes les images de lettres""" letters = {} for l_file in self.get_letters_files(): # lettre/shot_10_j.png l = l_file[:-4][-1] letters[l] = cv2.imread(l_file, -1) return letters def overlay(self, frame, lettre_img_list, lettre_list, change): """ Conservation dans self.size_pos_list = [[y_size, x_pos, y_pos]] lettre_img_list: liste des images des lettres [image_de_a, image_de_f, ...] lettre_list: liste des lettres ['a', 'f', ...] change = changement de lettre, soit affichage des lettres en cours """ for i in range(len(lettre_list)): # changement seulement à every if change == 1: # Plage de taille possible h_mini_semaphore = int(self.height / 10) h_maxi_semaphore = int(self.height / 1.2) # Choix y_size = random.randint(h_mini_semaphore, h_maxi_semaphore) # x découle de y x_size = int(lettre_img_list[i].shape[1] * \ y_size/lettre_img_list[i].shape[0]) # Application de la taille à la lettre en cours img = cv2.resize(lettre_img_list[i], (x_size, y_size), interpolation=cv2.INTER_CUBIC) # Position x_pos = random.randint(50, self.width - x_size - 50) y_pos = random.randint(0, self.height - y_size) # Storage self.size_pos_list.append([y_size, x_pos, y_pos]) print("Ajout de", [y_size, x_pos, y_pos]) else: # change == 0 y_size = self.size_pos_list[i][0] x_size = int(lettre_img_list[i].shape[1] * \ y_size/lettre_img_list[i].shape[0]) x_pos = self.size_pos_list[i][1] y_pos = self.size_pos_list[i][2] img = cv2.resize(lettre_img_list[i], (x_size, y_size), interpolation=cv2.INTER_CUBIC) # Modification de l'aspect de la lettre img = aspect_change(img) # Overlay de lettre_img déjà retaillée à position x, y frame = over_transparent(frame, img, x_pos, y_pos) return frame def add_lettre(self, frame, lettre_list): k = 0 for l in lettre_list: if l == " ": l = "space" # Overlay du caractère l font = cv2.FONT_HERSHEY_SIMPLEX frame = cv2.putText(frame, l, (10, int(self.height * (0.95 - k))), font, 1, (255, 255, 255), 2, cv2.LINE_AA) k += 0.06 return frame def get_lettre_list(self): """Retourne une liste d'image de lettres et une liste de ces lettres""" # Get some lettres au hasard lettre_list = [] lettre_img_list = [] # Get n lettres n = random.randint(1, 3) for i in range(n): k = random.randint(0, 26) lettre_list.append(list(self.letters.keys())[k]) print("\nListe des lettre:", lettre_list, "\n") for u in range(len(lettre_list)): lettre_img_list.append(self.letters[lettre_list[u]]) return lettre_img_list, lettre_list def get_semaphore_video(self): """Une lettre ajoutée et changée toutes les every secondes""" how_many = 0 letter_duration = 0 loop = 1 change = 1 # Initialisation lettre_img_list, lettre_list = self.get_lettre_list() while loop: ret, frame = self.video_in.read() letter_duration += 1 # Changement de lettres if letter_duration == self.every: lettre_img_list, lettre_list = self.get_lettre_list() change = 1 # Reset self.size_pos_list = [] letter_duration = 0 # Overlay des images de lettres frame = self.overlay(frame, lettre_img_list, lettre_list, change) # Overlay des textes de lettres frame = self.add_lettre(frame, lettre_list) # On reste sur les mêmes lettres pendant every change = 0 cv2.imshow('Out', frame) self.video.write(frame) # Jusqu'à la durée demandée how_many += 1 if how_many == self.FPS * self.lenght: loop = 0 # Echap et attente k = cv2.waitKey(33) if k == 27: loop = 0 self.video.release() cv2.destroyAllWindows()
class ResizeBlur: def __init__(self, root, size, blur, imshow=1): """ root = dossier semaphore size = taille des images pour l'ia blur = 0 à 10 pour flouter les images pour l'ia imshow = 0 ou 1 pour affichage d'image ou non pendant l'exécution """ self.root = root # soit ..../semaphore self.size = int(size) self.size = max(20, self.size) self.size = min(800, self.size) self.blur = blur self.imshow = imshow # Mes outils personnels self.tools = MyTools() # Le terrain de jeu self.create_training_shot_resized_dir() # Liste self.shot_list = self.get_shot_list() self.create_sub_folders() def create_training_shot_resized_dir(self): directory = os.path.join(self.root, "training_shot_resized") print("Dossier training_shot_resized:", directory) self.tools.create_directory(directory) def create_sub_folders(self): """Création de n dossiers shot_000""" # Nombre de dossiers nécessaires d = os.path.join(self.root, "training_shot") n = len(self.tools.get_all_sub_directories(d)) - 1 print("Nombre de sous répertoires", n) for i in range(n): directory = os.path.join(self.root, 'training_shot_resized', 'shot_' + str(i).zfill(3)) self.tools.create_directory(directory) def get_new_name(self, shot): """ de /media/data/3D/projets/semaphore/training_shot/shot_000/shot_0_a.png à /media/data/3D/projets/semaphore/training_shot_resized/shot_000/shot_0_a.png j'ai /media/data/3D/projets/semaphore/training_shot_resized/shot_000/shot_3921_h.png """ t = shot.partition("training_shot") # t = ('/media/data/3D/projets/semaphore/', 'training_shot', # '/shot_000/shot_1054_s.png') name = os.path.join(self.root, "training_shot_resized", t[2][1:]) return name def change_resolution(self, img, x, y): """Une image peut-être ratée""" try: res = cv2.resize(img, (x, y), interpolation=cv2.INTER_AREA) except: res = np.zeros([self.size, self.size, 1], dtype=np.uint8) return res def apply_blur(self, img, k): """TODO: Utiliser GaussianBlur img = cv2.GaussianBlur(img, (5, 5), 0) """ if self.blur: img = cv2.blur(img, (k, k)) return img def random_noise(self, img): """Entre 0 et 20 pixels au hasard""" nb = random.randint(0, 10) for i in range(nb): x = random.randint(0, 39) y = random.randint(0, 39) img[x][y] = 1 return img def some_dirty(self, img): """0 à 3 paquets de 4 pixels 0 1 0 1 1 1 0 1 0 """ nb = random.randint(0, 2) for i in range(nb): x = random.randint(0, 34) y = random.randint(0, 34) # 1ère ligne a = x + 1 b = y + 0 img[a][b] = 1 # 2ème ligne for u in range(3): a = x + u b = y + 1 img[a][b] = 1 # 3ème ligne a = x + 1 b = y + 2 img[a][b] = 1 return img def batch(self): """Liste des images, lecture, conversion, save""" i = 0 if self.imshow: cv2.namedWindow('Image In') cv2.namedWindow('Image Out') # Pour chaque image for shot in self.shot_list: # Lecture img = cv2.imread(shot, 0) # ResizeBlur img_out = self.change_resolution(img, self.size, self.size) # Flou img_out = self.apply_blur(img_out, self.blur) # Noise # L'ajout de bruit est appris par le réseau et va dégrader la # reconnaissance, mais Jo dit que non, ça dépend de ??? # #img_out = self.random_noise(img_out) # #img_out = self.some_dirty(img_out) # ## Affichage if self.imshow: if i % 500 == 0: #print(i) imgB = self.change_resolution(img_out, 600, 600) cv2.imshow('Image In', img) cv2.imshow('Image Out', imgB) cv2.waitKey(1) i += 1 # Save new_shot = self.get_new_name(shot) cv2.imwrite(new_shot, img_out) cv2.destroyAllWindows() def get_shot_list(self): """Liste des images""" # Liste shot = os.path.join(self.root, "training_shot") shot_list = self.tools.get_all_files_list(shot, ".png") print("Dossier des images NB:", shot) print("Nombre d'images:", len(shot_list)) return shot_list