73 lines
2.7 KiB
Python
73 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Create csv
|
|
|
|
Automatically generated by Colaboratory.
|
|
|
|
Original file is located at
|
|
https://colab.research.google.com/drive/1sCluAC_Woog1maeWw4n-gS3G2rk_0EK2
|
|
"""
|
|
|
|
import cv2
|
|
import os
|
|
import random
|
|
import numpy as np
|
|
import csv
|
|
|
|
import pandas as pd
|
|
|
|
# Création d'un fichier csv à partir de la sortie de yolo
|
|
# file_yolo : path du fichier de sortie concat.txt yolo
|
|
# file_csv : path du fichier à créer csv
|
|
# attributes : liste des caractéristiques utilisées dans l'éxecution de yolo
|
|
|
|
def create_csv(file_yolo, file_csv, attributes):
|
|
#on récupère la sortie du yolo
|
|
yolo = open(file_yolo, 'r')
|
|
lines = yolo.readlines()
|
|
yolo.close()
|
|
|
|
# définition des noms de colonnes
|
|
fields = ['Excluded', 'id', 'x', 'y', 'Smiling', 'Bald', 'Bangs', 'Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Chubby',
|
|
'Eyeglasses', 'Heavy_Makeup', 'Male', 'Mustache', 'No_Beard', 'Pale_Skin',
|
|
'Straight_Hair', 'Wavy_Hair', 'Wearing_Earrings', 'Wearing_Hat', 'Wearing_Necklace',
|
|
'Wearing_Lipstick', 'Young']
|
|
|
|
i = 0
|
|
coords = [] # liste des coordonnées des classes détectés (emplacement des images)
|
|
rows = [] # liste des lignes to add dans le fichier csv
|
|
|
|
for line in lines:
|
|
args = line.split()
|
|
classe = attributes[int(args[0])]
|
|
x = round(float(args[1]), 1)
|
|
y = round(float(args[2]), 1)
|
|
if (x,y) in coords:
|
|
# si on a déjà une ligne correspondant à cette image
|
|
rows[coords.index((x,y))][classe] = 1
|
|
else:
|
|
# on a pas encore rencontré cette image
|
|
coords.append((x,y))
|
|
rows.append({'Excluded' : 0, 'id' : i, 'x' : x, 'y' : y, classe : 1})
|
|
i += 1
|
|
|
|
# Création du csv
|
|
with open(file_csv, 'w') as csvfile:
|
|
# creating a csv dict writer object
|
|
writer = csv.DictWriter(csvfile, fieldnames = fields, restval = 0)
|
|
# writing headers (field names)
|
|
writer.writeheader()
|
|
# writing data rows
|
|
writer.writerows(rows)
|
|
df=pd.read_csv(file_csv,delimiter=',')
|
|
df.rename(columns={'No_Beard':'Beard','Black_Hair':'Black','Blond_Hair':'Blond','Brown_Hair':'Brown','Wavy_Hair':'Wavy','Straight_Hair':'Straight','Wearing_Earrings':'Earrings','Wearing_Hat':'Hat','Wearing_Necklace':'Necklace','Wearing_Lipstick':'Lipstick'},inplace=True)
|
|
df.loc[df.Beard == 1,"Beard"] = 2
|
|
df.loc[df.Beard == 0,"Beard"] = 1
|
|
df.loc[df.Beard == 2,"Beard"] = 0
|
|
df.to_csv(file_csv,index=False)
|
|
|
|
attributes = ['Smiling', 'Bald', 'Bangs', 'Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Chubby',
|
|
'Eyeglasses', 'Heavy_Makeup', 'Male', 'Mustache', 'No_Beard', 'Pale_Skin',
|
|
'Straight_Hair', 'Wavy_Hair', 'Wearing_Earrings', 'Wearing_Hat', 'Wearing_Necklace',
|
|
'Wearing_Lipstick', 'Young']
|
|
|
|
|