From d39b2357f5742fe347daaddc8584292b4c983a26 Mon Sep 17 00:00:00 2001 From: supernlogn Date: Fri, 22 Dec 2017 22:18:46 +0200 Subject: [PATCH] python3.5 compatibility --- vod_converter/converter.py | 8 +++--- vod_converter/kitti.py | 30 ++++++++++---------- vod_converter/kitti_tracking.py | 20 ++++++------- vod_converter/main.py | 10 +++---- vod_converter/udacity.py | 16 +++++------ vod_converter/voc.py | 50 ++++++++++++++++----------------- 6 files changed, 67 insertions(+), 67 deletions(-) diff --git a/vod_converter/converter.py b/vod_converter/converter.py index 7418fb5..422c509 100644 --- a/vod_converter/converter.py +++ b/vod_converter/converter.py @@ -28,7 +28,7 @@ def validate_schema(data, schema): 'id': {'type': 'string'}, 'path': {'type': 'string'}, 'segmented_path': { - 'anyOf': [ + 'anyO': [ {'type': 'null'}, {'type': 'string'} ] @@ -146,13 +146,13 @@ def validate_image_detections(image_detections): try: validate_schema(image_detection, IMAGE_DETECTION_SCHEMA) except SchemaError as se: - raise Exception(f"at index {i}") from se + raise Exception("at index {}".format(i)) from se image = image_detection['image'] for detection in image_detection['detections']: if detection['right'] >= image['width'] or detection['bottom'] >= image['height']: - raise ValueError(f"Image {image} has out of bounds bounding box {detection}") + raise ValueError("Image {} has out of bounds bounding box {}".format(image, detection)) if detection['right'] <= detection['left'] or detection['bottom'] <= detection['top']: - raise ValueError(f"Image {image} has zero dimension bbox {detection}") + raise ValueError("Image {} has zero dimension bbox {}".format(image, detection)) def convert_labels(*, image_detections, expected_labels, diff --git a/vod_converter/kitti.py b/vod_converter/kitti.py index 45c17f7..219f739 100644 --- a/vod_converter/kitti.py +++ b/vod_converter/kitti.py @@ -45,10 +45,10 @@ def validate(self, path): 'training/label_2' ] for subdir in expected_dirs: - if not os.path.isdir(f"{path}/{subdir}"): - return False, f"Expected subdirectory {subdir} within {path}" - if not os.path.isfile(f"{path}/train.txt"): - return False, f"Expected train.txt file within {path}" + if not os.path.isdir("{}/{}".format(path, subdir)): + return False, "Expected subdirectory {} within {}".format(subdir, path) + if not os.path.isfile("{}/train.txt".format(path)): + return False, "Expected train.txt file within {}".format(path) return True, None def ingest(self, path): @@ -61,20 +61,20 @@ def ingest(self, path): def find_image_ext(self, root, image_id): for image_ext in ['png', 'jpg']: - if os.path.exists(f"{root}/training/image_2/{image_id}.{image_ext}"): + if os.path.exists("{}/training/image_2/{}.{}".format(root, image_id, image_ext)): return image_ext - raise Exception(f"could not find jpg or png for {image_id} at {root}/training/image_2") + raise Exception("could not find jpg or png for {} at {}/training/image_2".format(image_id, root)) def _get_image_ids(self, root): - path = f"{root}/train.txt" + path = "{}/train.txt".format(root) with open(path) as f: return f.read().strip().split('\n') def _get_image_detection(self, root, image_id, *, image_ext='png'): - detections_fpath = f"{root}/training/label_2/{image_id}.txt" + detections_fpath = "{}/training/label_2/{}.txt".format(root, image_id) detections = self._get_detections(detections_fpath) detections = [det for det in detections if det['left'] < det['right'] and det['top'] < det['bottom']] - image_path = f"{root}/training/image_2/{image_id}.{image_ext}" + image_path = "{}/training/image_2/{}.{}".format(root, image_id, image_ext) image_width, image_height = _image_dimensions(image_path) return { 'image': { @@ -126,23 +126,23 @@ def expected_labels(self): } def egest(self, *, image_detections, root): - images_dir = f"{root}/training/image_2" + images_dir = "{}/training/image_2".format(root) os.makedirs(images_dir, exist_ok=True) - labels_dir = f"{root}/training/label_2" + labels_dir = "{}/training/label_2".format(root) os.makedirs(labels_dir, exist_ok=True) - id_file = f"{root}/train.txt" + id_file = "{}/train.txt".format(root) for image_detection in image_detections: image = image_detection['image'] image_id = image['id'] src_extension = image['path'].split('.')[-1] - shutil.copyfile(image['path'], f"{images_dir}/{image_id}.{src_extension}") + shutil.copyfile(image['path'], "{}/{}.{}".format(images_dir, image_id, src_extension)) with open(id_file, 'a') as out_image_index_file: - out_image_index_file.write(f'{image_id}\n') + out_image_index_file.write('{}\n'.format(image_id)) - out_labels_path = f"{labels_dir}/{image_id}.txt" + out_labels_path = "{}/{}.txt".format(labels_dir, image_id) with open(out_labels_path, 'w') as csvfile: csvwriter = csv.writer(csvfile, delimiter=' ', quoting=csv.QUOTE_MINIMAL) diff --git a/vod_converter/kitti_tracking.py b/vod_converter/kitti_tracking.py index 77b0d0f..40f7bdf 100644 --- a/vod_converter/kitti_tracking.py +++ b/vod_converter/kitti_tracking.py @@ -62,22 +62,22 @@ class KITTITrackingIngestor(Ingestor): def validate(self, path): expected_dirs = [ - 'image_02', - 'label_02' + 'label_02', + 'image_02' ] for subdir in expected_dirs: - if not os.path.isdir(f"{path}/{subdir}"): - return False, f"Expected subdirectory {subdir} within {path}" + if not os.path.isdir("{}/{}".format(path, subdir)): + return False, "Expected subdirectory {} within {}".format(subdir, path) return True, None def ingest(self, path): - fs = os.listdir(f"{path}/label_02") + fs = os.listdir("{}/label_02".format(path)) label_fnames = [f for f in fs if LABEL_F_PATTERN.match(f)] image_detections = [] for label_fname in label_fnames: frame_name = label_fname.split(".")[0] - labels_path = f"{path}/label_02/{label_fname}" - images_dir = f"{path}/image_02/{frame_name}" + labels_path = "{}/label_02/{}".format(path, label_fname) + images_dir = "{}/image_02/{}".format(path, frame_name) image_detections.extend( self._get_track_image_detections(frame_name=frame_name, labels_path=labels_path, images_dir=images_dir)) return image_detections @@ -101,9 +101,9 @@ def _get_track_image_detections(self, *, frame_name, labels_path, images_dir): image_detections = [] for frame_id in sorted(detections_by_frame.keys()): frame_dets = detections_by_frame[frame_id] - image_path = f"{images_dir}/{frame_id:06d}.png" + image_path = "{}/{:06d}.png".format(images_dir, frame_id) if not os.path.exists(image_path): - image_path = f"{images_dir}/{frame_id:06d}.jpg" + image_path = "{}/{:06d}.jpg".format(images_dir, frame_id) with Image.open(image_path) as image: image_width = image.width image_height = image.height @@ -117,7 +117,7 @@ def clamp_bbox(det): image_detections.append({ 'image': { - 'id': f"{frame_name}-{frame_id:06d}", + 'id': "{}-{:06d}".format(frame_name, frame_id), 'path': image_path, 'segmented_path': None, 'width': image.width, diff --git a/vod_converter/main.py b/vod_converter/main.py index 68f75af..03ddc1b 100644 --- a/vod_converter/main.py +++ b/vod_converter/main.py @@ -39,9 +39,9 @@ def main(*, from_path, from_key, to_path, to_key, select_only_known_labels, filt select_only_known_labels=select_only_known_labels, filter_images_without_labels=filter_images_without_labels) if success: - print(f"Successfully converted from {from_key} to {to_key}.") + print("Successfully converted from {} to {}.".format(from_key, to_key)) else: - print(f"Failed to convert from {from_key} to {to_key}: {msg}") + print("Failed to convert from {} to {}: {}".format(from_key, to_key, msg)) return 1 @@ -53,12 +53,12 @@ def parse_args(): required.add_argument('--from', dest='from_key', required=True, - help=f'Format to convert from: one of {", ".join(INGESTORS.keys())}', type=str) + help='Format to convert from: one of {}'.format(", ".join(INGESTORS.keys())), type=str) required.add_argument('--from-path', dest='from_path', required=True, - help=f'Path to dataset you wish to convert.', type=str) + help='Path to dataset you wish to convert.', type=str) required.add_argument('--to', dest='to_key', required=True, - help=f'Format to convert to: one of {", ".join(EGESTORS.keys())}', + help='Format to convert to: one of {}'.format(", ".join(EGESTORS.keys())), type=str) required.add_argument( '--to-path', diff --git a/vod_converter/udacity.py b/vod_converter/udacity.py index a74db61..d4a32c4 100644 --- a/vod_converter/udacity.py +++ b/vod_converter/udacity.py @@ -18,13 +18,13 @@ class UdacityCrowdAIIngestor(Ingestor): def validate(self, root): - labels_path = f"{root}/labels.csv" + labels_path = "{}/labels.csv".format(root) if not os.path.isfile(labels_path): - return False, f"Expected to find {labels_path}" + return False, "Expected to find {}".format(labels_path) return True, None def ingest(self, root): - labels_path = f"{root}/labels.csv" + labels_path = "{}/labels.csv".format(root) image_labels = defaultdict(list) with open(labels_path) as labels_file: @@ -34,7 +34,7 @@ def ingest(self, root): image_labels[row[4]].append(row) image_detections = [] - for idx, image_path in enumerate(glob.glob(f"{root}/*.jpg")): + for idx, image_path in enumerate(glob.glob("{}/*.jpg".format(root))): f_name = image_path.split("/")[-1] f_image_labels = image_labels[f_name] fname_id = f_name.split('.')[0] @@ -81,13 +81,13 @@ def valid_bbox(det): class UdacityAuttiIngestor(Ingestor): def validate(self, root): - labels_path = f"{root}/labels.csv" + labels_path = "{}/labels.csv".format(root) if not os.path.isfile(labels_path): - return False, f"Expected to find {labels_path}" + return False, "Expected to find {}".format(labels_path) return True, None def ingest(self, root): - labels_path = f"{root}/labels.csv" + labels_path = "{}/labels.csv".format(root) image_labels = defaultdict(list) with open(labels_path) as labels_file: @@ -97,7 +97,7 @@ def ingest(self, root): image_labels[row[0]].append(row) image_detections = [] - for idx, image_path in enumerate(glob.glob(f"{root}/*.jpg")): + for idx, image_path in enumerate(glob.glob("{}/*.jpg".format(root))): f_name = image_path.split("/")[-1] f_image_labels = image_labels[f_name] fname_id = f_name.split('.')[0] diff --git a/vod_converter/voc.py b/vod_converter/voc.py index 2145170..4a2618a 100644 --- a/vod_converter/voc.py +++ b/vod_converter/voc.py @@ -13,12 +13,12 @@ class VOCIngestor(Ingestor): def validate(self, root): - path = f"{root}/VOC2012" + path = "{}/VOC2012".format(root) for subdir in ["ImageSets", "JPEGImages", "Annotations"]: - if not os.path.isdir(f"{path}/{subdir}"): - return False, f"Expected subdirectory {subdir} within {path}" - if not os.path.isfile(f"{path}/ImageSets/Main/trainval.txt"): - return False, f"Expected main image set ImageSets/Main/trainval.txt to exist within {path}" + if not os.path.isdir("{}/{}".format(path, subdir)): + return False, "Expected subdirectory {} within {}".format(subdir, path) + if not os.path.isfile("{}/ImageSets/Main/trainval.txt".format(path)): + return False, "Expected main image set ImageSets/Main/trainval.txt to exist within {}".format(path) return True, None def ingest(self, path): @@ -26,8 +26,8 @@ def ingest(self, path): return [self._get_image_detection(path, image_name) for image_name in image_names] def _get_image_ids(self, root): - path = f"{root}/VOC2012" - with open(f"{path}/ImageSets/Main/trainval.txt") as f: + path = "{}/VOC2012".format(root) + with open("{}/ImageSets/Main/trainval.txt".format(path)) as f: fnames = [] for line in f.read().strip().split('\n'): cols = line.split() @@ -39,22 +39,22 @@ def _get_image_ids(self, root): return fnames def _get_image_detection(self, root, image_id): - path = f"{root}/VOC2012" - image_path = f"{path}/JPEGImages/{image_id}.jpg" + path = "{}/VOC2012".format(root) + image_path = "{}/JPEGImages/{}.jpg".format(path, image_id) if not os.path.isfile(image_path): - raise Exception(f"Expected {image_path} to exist.") - annotation_path = f"{path}/Annotations/{image_id}.xml" + raise Exception("Expected {} to exist.".format(image_path)) + annotation_path = "{}/Annotations/{}.xml".format(path, image_id) if not os.path.isfile(annotation_path): - raise Exception(f"Expected annotation file {annotation_path} to exist.") + raise Exception("Expected annotation file {} to exist.".format(annotation_path)) tree = ET.parse(annotation_path) xml_root = tree.getroot() size = xml_root.find('size') segmented = xml_root.find('segmented').text == '1' segmented_path = None if segmented: - segmented_path = f"{path}/SegmentationObject/{image_id}.png" + segmented_path = "{}/SegmentationObject/{}.png".format(path, image_id) if not os.path.isfile(segmented_path): - raise Exception(f"Expected segmentation file {segmented_path} to exist.") + raise Exception("Expected segmentation file {} to exist.".format(segmented_path)) image_width = int(size.find('width').text) image_height = int(size.find('height').text) return { @@ -106,10 +106,10 @@ def expected_labels(self): } def egest(self, *, image_detections, root): - image_sets_path = f"{root}/VOC2012/ImageSets/Main" - images_path = f"{root}/VOC2012/JPEGImages" - annotations_path = f"{root}/VOC2012/Annotations" - segmentations_path = f"{root}/VOC2012/SegmentationObject" + image_sets_path = "{}/VOC2012/ImageSets/Main".format(root) + images_path = "{}/VOC2012/JPEGImages".format(root) + annotations_path = "{}/VOC2012/Annotations".format(root) + segmentations_path = "{}/VOC2012/SegmentationObject".format(root) segmentations_dir_created = False for to_create in [image_sets_path, images_path, annotations_path]: @@ -119,19 +119,19 @@ def egest(self, *, image_detections, root): image = image_detection['image'] image_id = image['id'] src_extension = image['path'].split('.')[-1] - shutil.copyfile(image['path'], f"{images_path}/{image_id}.{src_extension}") + shutil.copyfile(image['path'], "{}/{}.{}".format(images_path, image_id, src_extension)) - with open(f"{image_sets_path}/trainval.txt", 'a') as out_image_index_file: - out_image_index_file.write(f'{image_id}\n') + with open("{}/trainval.txt".format(image_sets_path), 'a') as out_image_index_file: + out_image_index_file.write('{}\n'.format(image_id)) if image['segmented_path'] is not None: if not segmentations_dir_created: os.makedirs(segmentations_path) segmentations_dir_created = True - shutil.copyfile(image['segmented_path'], f"{segmentations_path}/{image_id}.png") + shutil.copyfile(image['segmented_path'], "{}/{}.png".format(segmentations_path, image_id)) xml_root = ET.Element('annotation') - add_text_node(xml_root, 'filename', f"{image_id}.{src_extension}") + add_text_node(xml_root, 'filename', "{}.{}".format(image_id, src_extension)) add_text_node(xml_root, 'folder', 'VOC2012') add_text_node(xml_root, 'segmented', int(segmentations_dir_created)) @@ -161,7 +161,7 @@ def egest(self, *, image_detections, root): 'ymax': detection['bottom'] + 1 }) - ET.ElementTree(xml_root).write(f"{annotations_path}/{image_id}.xml") + ET.ElementTree(xml_root).write("{}/{}.xml".format(annotations_path, image_id)) def add_sub_node(node, name, kvs): @@ -173,7 +173,7 @@ def add_sub_node(node, name, kvs): def add_text_node(node, name, text): subnode = ET.SubElement(node, name) - subnode.text = f"{text}" + subnode.text = "{}".format(text) return subnode