Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import shutil
- __dir="../dataset/alta"
- _to_dir="../dataset/unilever"
- _ext =[".jpg",".jpeg",".png"]
- def getDirs(root_dir, depth=1, regex=".+"):
- files=os.listdir(root_dir)
- results =[]
- for f in files:
- if os.path.isdir(root_dir+"/"+f) and depth >0:
- sub_dir = getDirs(root_dir+"/"+f, depth-1)
- for sub_file in sub_dir:
- if re.match(regex,sub_file):
- results.append(sub_file)
- elif re.match(regex,root_dir+"/"+f):
- results.append(root_dir+"/"+f)
- return results
- def mkFile(file, fileContent):
- block = file.split("/")
- if len(block)>1:
- _d = "/".join(block[:-1])
- if not os.path.exists(_d):
- os.makedirs(_d)
- with open(file,'w') as wfilenews:
- wfilenews.writelines(fileContent)
- wfilenews.writelines("\n")
- def mkCP(_from, to):
- if os.path.exists(to):
- return
- block = to.split("/")
- if len(block)>1:
- _d = "/".join(block[:-1])
- if not os.path.exists(_d):
- os.makedirs(_d)
- shutil.copy(_from,to)
- _dict ={'0':'0','1':'1','2':'1','3':'0','4':'0','5':'0','6':'1','7':'0','8':'0','9':'1','10':'1','11':'0','12':'0','13':'0','14':'1','15':'1','16':'0','17':'1','18':'1','19':'1','20':'0'}
- # _dict ={'0':'1','1':'1','3':'0','2':'0'}
- files = getDirs(__dir,4, ".*\.txt$")
- for f in files:
- with open(f, 'r') as rbfile:
- lines= rbfile.readlines()
- linesnew=[]
- for l in lines:
- split_l=l.split(' ')
- if len(split_l)>2 and (split_l[0] in _dict):
- split_l[0]= _dict[split_l[0]]
- linesnew.append(' '.join(split_l))
- else:
- print("SKIP ",f)
- if len(linesnew)>0:
- __des = str(f).replace(__dir,_to_dir,1)
- print(linesnew)
- mkFile(__des,linesnew)
- imageFile = str(f).replace("labels","images",1)
- #with open(__des,'w') as wfilenews:
- for e in _ext:
- ff = imageFile.replace(".txt",e,1)
- if os.path.exists(ff):
- mkCP(ff,ff.replace(__dir,_to_dir,1))
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement