Advertisement
iceriver102

convert_idx

Mar 16th, 2023
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. import os
  2. import re
  3. import shutil
  4. __dir="../dataset/alta"
  5. _to_dir="../dataset/unilever"
  6.  
  7. _ext =[".jpg",".jpeg",".png"]
  8. def getDirs(root_dir, depth=1, regex=".+"):
  9.     files=os.listdir(root_dir)
  10.     results =[]
  11.     for f in files:
  12.         if os.path.isdir(root_dir+"/"+f) and depth >0:
  13.             sub_dir = getDirs(root_dir+"/"+f, depth-1)
  14.             for sub_file in sub_dir:
  15.                 if re.match(regex,sub_file):
  16.                     results.append(sub_file)
  17.         elif re.match(regex,root_dir+"/"+f):
  18.             results.append(root_dir+"/"+f)
  19.     return results
  20.  
  21. def mkFile(file, fileContent):
  22.     block = file.split("/")
  23.     if len(block)>1:
  24.         _d = "/".join(block[:-1])
  25.         if not os.path.exists(_d):
  26.             os.makedirs(_d)
  27.     with open(file,'w') as wfilenews:
  28.         wfilenews.writelines(fileContent)
  29.         wfilenews.writelines("\n")
  30.        
  31. def mkCP(_from, to):
  32.     if os.path.exists(to):
  33.         return
  34.     block = to.split("/")
  35.     if len(block)>1:
  36.         _d = "/".join(block[:-1])
  37.         if not os.path.exists(_d):
  38.             os.makedirs(_d)
  39.     shutil.copy(_from,to)
  40.  
  41. _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'}
  42. # _dict ={'0':'1','1':'1','3':'0','2':'0'}
  43. files = getDirs(__dir,4, ".*\.txt$")
  44. for f in files:
  45.     with open(f, 'r') as rbfile:
  46.         lines= rbfile.readlines()
  47.         linesnew=[]
  48.         for l in lines:
  49.             split_l=l.split(' ')
  50.            
  51.             if len(split_l)>2 and (split_l[0] in _dict):
  52.                 split_l[0]= _dict[split_l[0]]
  53.                 linesnew.append(' '.join(split_l))
  54.             else:
  55.                 print("SKIP ",f)
  56.         if len(linesnew)>0:
  57.             __des = str(f).replace(__dir,_to_dir,1)
  58.             print(linesnew)
  59.             mkFile(__des,linesnew)
  60.            
  61.             imageFile = str(f).replace("labels","images",1)
  62.            
  63.                 #with open(__des,'w') as wfilenews:
  64.             for e in _ext:
  65.                 ff = imageFile.replace(".txt",e,1)
  66.                 if os.path.exists(ff):
  67.                     mkCP(ff,ff.replace(__dir,_to_dir,1))
  68.                     break
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement