def main(): # Where to look for Cityscapes if 'CITYSCAPES_DATASET' in os.environ: cityscapesPath = os.environ['CITYSCAPES_DATASET'] else: cityscapesPath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'..','..') # how to search for all ground truth searchFine = os.path.join( cityscapesPath , "gtFine" , "*" , "*" , "*_gt*_polygons.json" ) searchCoarse = os.path.join( cityscapesPath , "gtCoarse" , "*" , "*" , "*_gt*_polygons.json" ) # search files filesFine = glob.glob( searchFine ) filesFine.sort() filesCoarse = glob.glob( searchCoarse ) filesCoarse.sort() # concatenate fine and coarse files = filesFine + filesCoarse # files = filesFine # use this line if fine is enough for now. # quit if we did not find anything if not files: printError( "Did not find any files. Please consult the README." ) # a bit verbose print("Processing {} annotation files".format(len(files))) # iterate through files progress = 0 print("Progress: {:>3} %".format( progress * 100 / len(files) ), end=' ') for f in files: # create the output filename dst = f.replace( "_polygons.json" , "_labelTrainIds.png" ) # do the conversion try: json2labelImg( f , dst , "trainIds" ) except: print("Failed to convert: {}".format(f)) raise # status progress += 1 print("\rProgress: {:>3} %".format( progress * 100 / len(files) ), end=' ') sys.stdout.flush()
def convert_json_to_label(json_file): from cityscapesscripts.preparation.json2labelImg import json2labelImg label_file = json_file.replace("_polygons.json", "_labelTrainIds.png") json2labelImg(json_file, label_file, "trainIds")
def main(): # Where to look for Cityscapes ''' __file__ 得到当前文件路径 但是若按绝对路径执行该文件,得到绝对路径 若按相对路径,或者在sys.path下执行,则得到相对路径。 为了保证得到绝对路径,用os.path.realpath() os.path.dirname获得该文件所在的文件夹名称 os.path.join,组合成 目录/../..表示当前目录网上两次。 ''' if 'CITYSCAPES_DATASET' in os.environ: cityscapesPath = os.environ['CITYSCAPES_DATASET'] # else: cityscapesPath = os.path.join( os.path.dirname(os.path.realpath(__file__)), '..', '..') # how to search for all ground truth ''' 在文件往上两个的目录,即cityscapes根目录下,找gtFine的目录。 加上*方便glob ''' searchFine = os.path.join(cityscapesPath, "gtFine", "*", "*", "*_gt*_polygons.json") searchCoarse = os.path.join(cityscapesPath, "gtCoarse", "*", "*", "*_gt*_polygons.json") # search files filesFine = glob.glob(searchFine) filesFine.sort() filesCoarse = glob.glob(searchCoarse) filesCoarse.sort() # concatenate fine and coarse files = filesFine + filesCoarse # files = filesFine # use this line if fine is enough for now. # quit if we did not find anything ''' 在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False 这里这个检查不太好,因为没检查图片为空的情形,只检查了label ''' if not files: printError("Did not find any files. Please consult the README.") # a bit verbose print("Processing {} annotation files".format(len(files))) # iterate through files progress = 0 print("Progress: {:>3} %".format(progress * 100 / len(files)), end=' ') for f in files: # create the output filename dst = f.replace("_polygons.json", "_labelTrainIds.png") ''' 替换并建一个新的list放图片。这里是选其中labelTrainIds这个类型 ''' # do the conversion try: json2labelImg(f, dst, "trainIds") except: print("Failed to convert: {}".format(f)) raise # status 更新进度条 progress += 1 print("\rProgress: {:>3} %".format(progress * 100 / len(files)), end=' ') # 输出刷新 sys.stdout.flush()
def convert_json_to_label(json_file): label_file = json_file.replace('_polygons.json', '_labelTrainIds.png') json2labelImg(json_file, label_file, 'trainIds')
def main(): # # Where to look for Cityscapes # if 'CITYSCAPES_DATASET' in os.environ: # cityscapesPath = os.environ['CITYSCAPES_DATASET'] # else: # cityscapesPath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'..','..') # # how to search for all ground truth # searchFine = os.path.join( cityscapesPath , "gtFine" , "*" , "*" , "*_gt*_polygons.json" ) # i. ex: ~~\gtFine\val\frankfurt\~~polygons.json # searchCoarse = os.path.join( cityscapesPath , "gtCoarse" , "*" , "*" , "*_gt*_polygons.json" ) # # search files # filesFine = glob.glob( searchFine ) # filesFine.sort() # filesCoarse = glob.glob( searchCoarse ) # filesCoarse.sort() # # concatenate fine and coarse # files = filesFine + filesCoarse # # files = filesFine # use this line if fine is enough for now. # Where to look for Cityscapes # i.21.3.11.12:45) 기존의 convertTestJ 폴더에서 panopticSeg_dentPanoJ 로 폴더명 바꿨고, 그안에 gt 및 inputOriPano 이렇게 두개 폴더 다시 만들어줬음. # 따라서 ~~polygons.json 경로 바꼈음. 바뀐 ~~polygons.json 경로 ex: panopticSeg_dentPanoJ\gt\train\imp2_1_polygons.json # MYROOTDIRPATH_J = r"C:\Users\starriet\Downloads\panopticSeg_dentPanoJ" # ~~polygons.json 경로 ex: convertTestJ\train\imp2_1_polygons.json # <-요건 기존경로. # i.21.3.14.22:41) 코랩컴에서의 경로로 수정. 내 구글드라이브에 커스텀데이터 올려놓고, 코랩컴에서 구글드라이브의 압축파일을 (코랩컴의 디렉토리에다가)압축풀어서 사용할거니까. # 즉, 구글코랩에서 구글드라이브 연동해서 돌리는걸 가정한것임. 뭐 사실상 코랩에서만 할테니까 일단은. MYROOTDIRPATH_J = "/content/datasetsJ/panopticSeg_dentPanoJ" # how to search for all ground truth(i. ~~polygons.json) forSearchAllPolygonsJson = os.path.join(MYROOTDIRPATH_J, "gt", "*", "*_polygons.json") # search files polygonsjson_path_list = glob.glob(forSearchAllPolygonsJson) files = polygonsjson_path_list # quit if we did not find anything if not files: printError("j) Did not find any files(~~polygons.json)!!!") # a bit verbose print("Processing {} annotation files".format(len(files))) # iterate through files progress = 0 print("Progress: {:>3} %".format(progress * 100 / len(files)), end=' ') for f in files: # create the output filename dst = f.replace("_polygons.json", "_labelTrainIds.png") # do the conversion try: json2labelImg(f, dst, "trainIds") except: print("Failed to convert: {}".format(f)) raise # status progress += 1 print("\rProgress: {:>3} %".format(progress * 100 / len(files)), end=' ') sys.stdout.flush()