Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
UltralyticsAssistant committed May 19, 2024
1 parent 34f2ae1 commit ffe5d86
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions adv_patch_gen/conv_visdrone_2_yolo/conv_visdrone_2_yolo_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@


def get_parsed_args():
parser = argparse.ArgumentParser(
description="VisDrone to YOLO annot format")
parser = argparse.ArgumentParser(description="VisDrone to YOLO annot format")
parser.add_argument(
"--sad",
"--source_annot_dir",
Expand Down Expand Up @@ -97,33 +96,32 @@ def conv_visdrone_2_yolo(
"""
if not all([osp.isdir(source_annot_dir), osp.isdir(source_image_dir)]):
raise ValueError(
f"source_annot_dir({source_annot_dir}) and source_image_dir({source_image_dir}) must be directories")
f"source_annot_dir({source_annot_dir}) and source_image_dir({source_image_dir}) must be directories"
)
src_annot_path = osp.join(source_annot_dir, "*")
src_image_path = osp.join(source_image_dir, "*")
src_annot_paths = sorted(glob.glob(src_annot_path))
src_image_paths = [p for p in sorted(
glob.glob(src_image_path)) if osp.splitext(p)[-1] in IMG_EXT]
assert len(src_image_paths) == len(src_annot_paths), \
f"Num src images: {len(src_image_paths)} & num src annots: {len(src_annot_paths)} do not match"
src_image_paths = [p for p in sorted(glob.glob(src_image_path)) if osp.splitext(p)[-1] in IMG_EXT]
assert len(src_image_paths) == len(
src_annot_paths
), f"Num src images: {len(src_image_paths)} & num src annots: {len(src_annot_paths)} do not match"

os.makedirs(target_annot_dir, exist_ok=True)
low_dim_cutoff = float("-inf") if not low_dim_cutoff else low_dim_cutoff
low_area_cutoff = float("-inf") if not low_area_cutoff else low_area_cutoff
target_img_list_fpath = osp.join(osp.dirname(
target_annot_dir), source_annot_dir.split("/")[-2].lower() + ".txt")
target_img_list_fpath = osp.join(osp.dirname(target_annot_dir), source_annot_dir.split("/")[-2].lower() + ".txt")

with tqdm.tqdm(total=len(src_image_paths)) as pbar, open(target_img_list_fpath, "w") as imgw:
orig_box_count = new_box_count = 0
for src_annot_file, src_image_file in zip(src_annot_paths, src_image_paths):
try:
iw, ih = imagesize.get(src_image_file)
target_annot_file = osp.join(
target_annot_dir, src_annot_file.split("/")[-1])
with open(src_annot_file, "r", encoding="utf-8") as fr, \
open(target_annot_file, "w", encoding="utf-8") as fw:
target_annot_file = osp.join(target_annot_dir, src_annot_file.split("/")[-1])
with open(src_annot_file, "r", encoding="utf-8") as fr, open(
target_annot_file, "w", encoding="utf-8"
) as fw:
for coords in fr:
annots = list(
map(int, coords.strip().strip(",").split(",")))
annots = list(map(int, coords.strip().strip(",").split(",")))
x, y = annots[0], annots[1]
w, h = annots[2], annots[3]
score, class_id, occu = annots[4], annots[5], annots[7]
Expand All @@ -140,21 +138,18 @@ def conv_visdrone_2_yolo(
# only use objects used for eval along and all levels of occlusion (0,1,2)
if score and occu <= 2:
class_id = CLASS_ID_REMAP[class_id] if CLASS_ID_REMAP else class_id
fw.write(
f"{class_id} {xc/iw} {yc/ih} {w/iw} {h/ih}\n")
fw.write(f"{class_id} {xc/iw} {yc/ih} {w/iw} {h/ih}\n")
new_box_count += 1
target_image_path = osp.join(
osp.dirname(osp.dirname(target_annot_file)),
"images",
osp.basename(target_annot_file).split(".")[
0] + osp.splitext(src_image_file)[1],
osp.basename(target_annot_file).split(".")[0] + osp.splitext(src_image_file)[1],
)
imgw.write(f"{osp.abspath(target_image_path)}\n")
except Exception as excep:
print(f"{excep}: Error reading img {src_image_file}")
pbar.update(1)
print(
f"Original Box Count: {orig_box_count}. Converted Box Count {new_box_count}")
print(f"Original Box Count: {orig_box_count}. Converted Box Count {new_box_count}")
print(f"{100 * (new_box_count) / orig_box_count:.2f}% of total boxes kept")


Expand Down

0 comments on commit ffe5d86

Please sign in to comment.