Skip to content
Snippets Groups Projects
Commit 98837771 authored by Stephane Chavin's avatar Stephane Chavin
Browse files

add os.path.join

parent eafb3756
Branches
No related tags found
No related merge requests found
......@@ -5,10 +5,16 @@ import labelme
import base64
import pandas as pd
def arg_directory(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f'`{path}` is not a valid path')
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='TODO')
parser.add_argument('-p','--path_to_json', type=str, help = 'Path of the folder that contain the .json',required=True)
parser.add_argument('-i','--path_to_img', type=str, help = 'Path of the folder that contain the .jpg',required=True)
parser.add_argument('-d','--direction', type=str, help = 'Directory to wich modified .json files will be stored',required=True)
parser.add_argument('-p','--path_to_json', type=arg_directory, help = 'Path of the folder that contain the .json',required=True)
parser.add_argument('-i','--path_to_img', type=arg_directory, help = 'Path of the folder that contain the .jpg',required=True)
parser.add_argument('-d','--direction', type=arg_directory, help = 'Directory to wich modified .json files will be stored',required=True)
args = parser.parse_args()
......@@ -27,7 +33,7 @@ liste_file = liste_file.reset_index()
for i, row in liste_file.iterrows():
if len(row.fn) > 30:
data = labelme.LabelFile.load_image_file(str(img_path+row.fn[:-4]+'jpg'))
data = labelme.LabelFile.load_image_file(os.path.join(img_path,str(row.fn[:-4]+'jpg')))
image_data = base64.b64encode(data).decode('utf-8')
else:
continue
......
......@@ -10,10 +10,16 @@ from p_tqdm import p_map
import warnings
warnings.filterwarnings('ignore')
def arg_directory(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f'`{path}` is not a valid path')
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='TODO')
parser.add_argument('-f','--file', type=str,required=True,help = 'Name of the file that contain the recording to print')
parser.add_argument('-p','--path_to_data', type=str, help = 'Path of the folder that contain the recordings',required=True)
parser.add_argument('-d','--direction', type=str, help = 'Directory to wich spectrogram will be stored',required=True)
parser.add_argument('-p','--path_to_data', type=arg_directory, help = 'Path of the folder that contain the recordings',required=True)
parser.add_argument('-d','--direction', type=arg_directory, help = 'Directory to wich spectrogram will be stored',required=True)
parser.add_argument('-m','--mode', type=str,choices=['unique','multiple'], help = 'Direction of the saved spectrogram',required=True)
args = parser.parse_args()
......@@ -29,7 +35,7 @@ elif args.mode == 'unique':
NB_IMG_PER_REC = 1
df = pd.read_csv(str(args.file),low_memory=False)
df = pd.read_csv(args.file,low_memory=False)
df['Path'] = ''
#for w in range (len(df)):
# df.loc[w,'Path']=str(args.path_to_data+ df.code_unique[w]+"_split_" + str(df.chunk_initial_time[w]) + "_" + str(df.chunk_final_time[w]))
......@@ -65,12 +71,12 @@ def process(x):
name = str(i[0].replace('/','_').split('.')[0]+'_'+str(count))
try :
plt.savefig(str(direction+folder+name+'.jpg'))
plt.savefig(os.path.join(direction,folder, str(name+'.jpg')))
except FileNotFoundError:
os.mkdir(str(direction+folder))
plt.savefig(str(direction+folder+name+'.jpg'))
os.mkdir(os.path.join(direction,folder))
plt.savefig(os.path.join(direction,folder, str(name+'.jpg')))
p_map(process, enumerate(df.groupby('Path')), num_cpus=10) #la colonne dir correspond au path
......
......@@ -12,14 +12,19 @@ import matplotlib.patches as patches
from matplotlib.patches import Rectangle
from PIL import Image
from mycolorpy import colorlist as mcp
import yaml
today = date.today()
def arg_directory(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f'`{path}` is not a valid path')
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='TODO')
parser.add_argument('-f','--filename_path', type=str, help = 'Path and name of the file containing the annotations',required=True)
parser.add_argument('-p','--path_to_data', type=str, help = 'Path of the folder that contain the recordings',required=True)
parser.add_argument('-d','--direction', type=str, help = 'Directory to wich spectrograms and .txt files will be stored',required=True)
parser.add_argument('-f','--filename_path', type=arg_directory, help = 'Path and name of the file containing the annotations',required=True)
parser.add_argument('-p','--path_to_data', type=arg_directory, help = 'Path of the folder that contain the recordings',required=True)
parser.add_argument('-d','--direction', type=arg_directory, help = 'Directory to wich spectrograms and .txt files will be stored',required=True)
parser.add_argument('-m','--mode',type=str,choices=['uniform','personalized'],help = 'Choose the mode to calculate the y and height value',required=True)
parser.add_argument('--export',type=str, default=None, help='To export the position of the bounding box on the spectrogram',required=False)
args = parser.parse_args()
......@@ -74,7 +79,6 @@ color = mcp.gen_color(cmap = "Wistia", n= len(list_espece))
def process(x):
count, (f, grp) = x
filename = str(f)
# tqdm.write(filename)
duration = DURATION
while len(grp) != 0:
......@@ -130,32 +134,30 @@ def process(x):
elif height_pxl > y_pxl*2:
y_pxl=y_pxl+0.5*(height_pxl-y_pxl*2)
annotation = pd.DataFrame([[str(data.loc[data.espece == row.Code,'ind'][0]),x_pxl,y_pxl,width_pxl,height_pxl]],columns = ['id','x', 'y', 'width', 'height'])
fin = pd.concat([fin,annotation])
grp = grp.drop(tab.index)
name = str(row.Path.replace('/','_').replace('.','_')+'_'+str(count))
name_file = str(direction+'labels_'+str(today.day)+'_'+str(today.month)+'/'+name+'.txt')
name_file = os.path.join(direction,str('labels_'+str(today.day)+'_'+str(today.month)),str(name+'.txt'))
try :
plt.savefig(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'+row.Code+"/"+name+'.jpg'))
plt.savefig(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month)),row.Code,str(name+'.jpg')))
fin.to_csv(name_file,sep =' ',header=False,index=False)
plt.savefig(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'+"all"+"/"+name+'.jpg'))
plt.savefig(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month)),'all',str(name+'.jpg')))
except :
os.mkdir(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'))
os.mkdir(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month))))
for especes in list_espece.index:
os.mkdir(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'+especes))
os.mkdir(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'+'all/'))
os.mkdir(str(direction+'labels_'+str(today.day)+'_'+str(today.month)+'/'))
os.mkdir(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month)),especes))
os.mkdir(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month)),'all'))
os.mkdir(os.path.join(direction,str('labels_'+str(today.day)+'_'+str(today.month))))
fin.to_csv(name_file,sep =' ',header=False,index=False)
plt.savefig(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'+row.Code+"/"+name+'.jpg'))
plt.savefig(str(direction+"images_"+str(today.day)+'_'+str(today.month)+'/'+"all"+"/"+name+'.jpg'))
plt.savefig(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month)),row.Code,str(name+'.jpg')))
plt.savefig(os.path.join(direction,str('images_'+str(today.day)+'_'+str(today.month)),'all',str(name+'.jpg')))
if args.export != None:
for l in range(len(fin)):
......@@ -168,11 +170,14 @@ def process(x):
except IndexError:
ipdb.set_trace()
try:
plt.savefig(str(direction+"images_annotes_"+str(today.day)+'_'+str(today.month)+'/'+name+'.jpg'))
plt.savefig(os.path.join, str('images_annotes_'+str(today.day)+'_'+str(today.month)),str(name+'.jpg'))
except Exception:
os.mkdir(str(direction+"images_annotes_"+str(today.day)+'_'+str(today.month)+'/'))
plt.savefig(str(direction+"images_annotes_"+str(today.day)+'_'+str(today.month)+'/'+name+'.jpg'))
os.mkdir(os.path.join(direction,str('images_annotes_'+str(today.day)+'_'+str(today.month))))
plt.savefig(os.path.join(direction, str('images_annotes_'+str(today.day)+'_'+str(today.month)),str(name+'.jpg')))
plt.close()
p_map(process, enumerate(df.groupby('Path')), num_cpus=10)
print('saved to ',direction)
\ No newline at end of file
......@@ -7,14 +7,20 @@ import shutil
import argparse
import ipdb
def arg_directory(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f'`{path}` is not a valid path')
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='TODO')
parser.add_argument('-r','--ratio', type=float, default = 0.7,help = 'Train Ratio (val = 1 - ratio)')
parser.add_argument('-p','--path_to_data', type=str, help = 'Path of the folder that contain the .txt (ending with labels/)',required=True)
parser.add_argument('-d','--direction', type=str, help = 'Directory to wich spectrogram and .txt files will be stored (different from -p)',required=True)
parser.add_argument('-p','--path_to_data', type=arg_directory, help = 'Path of the folder that contain the .txt (ending with labels/)',required=True)
parser.add_argument('-d','--direction', type=arg_directory, help = 'Directory to wich spectrogram and .txt files will be stored (different from -p)',required=True)
args = parser.parse_args()
path = str(args.path_to_data)
direction = str(args.direction)
path = args.path_to_data
direction = args.direction
NB_CLASS = 50
......@@ -88,25 +94,25 @@ for i in range (len(val)):
for i in range (len(train)):
train.file.iloc[i] = train.file.iloc[i][:-4]
isExist = os.path.exists(str(direction+'images'))
isExist = os.path.exists(os.path.join(direction,'images'))
if not isExist:
os.mkdir(str(direction+'images'))
os.mkdir(str(direction+'images/train'))
os.mkdir(str(direction+'images/val'))
os.mkdir(str(direction+'labels'))
os.mkdir(str(direction+'labels/train'))
os.mkdir(str(direction+'labels/val'))
os.mkdir(os.path.join(direction,'images'))
os.mkdir(os.path.join(direction,'images/train'))
os.mkdir(os.path.join(direction,'images/val'))
os.mkdir(os.path.join(direction,'labels'))
os.mkdir(os.path.join(direction,'labels/train'))
os.mkdir(os.path.join(direction,'labels/val'))
for i,row in tqdm(val.iterrows(), total=val.shape[0]):
shutil.copy2(str(path+row.file+'.txt'),str(direction+'labels/val/'+row.file+'.txt'))
shutil.copy2(str(path+'../images/all/'+row.file+'.jpg'),str(direction+'images/val/'+row.file+'.jpg'))
shutil.copy2(os.path.join(path, str(row.file+'.txt')), os.path.join(direction, str('labels/val/'+row.file+'.txt')))
shutil.copy2(os.path.join(path, str('../images/all/'+row.file+'.jpg')), os.path.join(direction, str('images/val/'+row.file+'.jpg')))
for i,row in tqdm(train.iterrows(), total=train.shape[0]):
shutil.copy2(str(path+row.file+'.txt'),str(direction+'labels/train/'+row.file+'.txt'))
shutil.copy2(str(path+'../images/all/'+row.file+'.jpg'),str(direction+'images/train/'+row.file+'.jpg'))
shutil.copy2(os.path.join(path, str(row.file+'.txt')), os.path.join(direction, str('labels/train/'+row.file+'.txt')))
shutil.copy2(os.path.join(path, str('../images/all/'+row.file+'.jpg')), os.path.join(direction, str('images/train/'+row.file+'.jpg')))
liste_espece = pd.read_csv(str(path+'../liste_especes.csv'))
......
......@@ -5,17 +5,23 @@ from tqdm import tqdm
import argparse
from datetime import date
def arg_directory(path):
if os.path.isdir(path):
return path
else:
raise argparse.ArgumentTypeError(f'`{path}` is not a valid path')
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='TODO')
parser.add_argument('-p','--path_to_data', type=str, help = 'Path of the folder that contain the .txt files',required=True)
parser.add_argument('-d','--direction', type=str, help = 'Directory to wich the dataframe will be stored',required=True)
parser.add_argument('-p','--path_to_data', type=arg_directory, help = 'Path of the folder that contain the .txt files',required=True)
parser.add_argument('-d','--direction', type=arg_directory, help = 'Directory to wich the dataframe will be stored',required=True)
args = parser.parse_args()
annots = str(args.path_to_data)
annots = args.path_to_data
today = date.today()
out_file = str('YOLO_detection_'+str('_'+today.day+'_'+today.month))
outdir = str(args.direction)
outdir = args.direction
df = pd.DataFrame(columns = ['file','idx','espece','x','y','w','h','conf'])
for i in tqdm(os.listdir(annots)):
......@@ -82,4 +88,4 @@ df['annot'] = 'None'
for j in range (len(df)):
df['annot'].iloc[j] = names[int(df.espece.iloc[j])]
df.to_csv(str(outdir+out_file+'.csv'), index= False)
df.to_csv(os.path.join(outdir,str(out_file+'.csv')), index= False)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment