Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3.5 compatibility #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions vod_converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def validate_schema(data, schema):
'id': {'type': 'string'},
'path': {'type': 'string'},
'segmented_path': {
'anyOf': [
'anyO': [
{'type': 'null'},
{'type': 'string'}
]
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 15 additions & 15 deletions vod_converter/kitti.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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': {
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 10 additions & 10 deletions vod_converter/kitti_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions vod_converter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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',
Expand Down
16 changes: 8 additions & 8 deletions vod_converter/udacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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]
Expand Down
50 changes: 25 additions & 25 deletions vod_converter/voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

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):
image_names = self._get_image_ids(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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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]:
Expand All @@ -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))

Expand Down Expand Up @@ -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):
Expand All @@ -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


Expand Down