1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
"""
$python xmlTotxt.py -n $FOLDER -u $LABELME_USER
--classes $CLASS_1 $CLASS_2 $CLASS_3...
FOLDER: LabelMe project folder name
LABELME_USER: LabelMe user name
CLASS_i: Class labels defined on LabelMe
"""
import argparse
import logging
import os
import xml.etree.ElementTree as ET
from os.path import join
from shutil import copyfile
def convert(size, in_x, in_y):
dw = 1./size[0]
dh = 1./size[1]
x = (in_x[0] + in_x[1])/2.0
y = (in_y[0] + in_y[1])/2.0
w = in_x[1] - in_x[0]
h = in_y[1] - in_y[0]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x, y, w, h)
def convert_annotation(in_dir, out_dir, image_id, out_id, classes):
in_file = open("%s/%s.xml" % (in_dir, image_id))
o_file = open(out_dir + "/%s.txt" % out_id, "w")
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find("imagesize")
h = float(size.find("nrows").text)
w = float(size.find("ncols").text)
for obj in root.iter("object"):
X = []
Y = []
cls = obj.find("name").text
if cls not in classes:
logging.debug("%s is not in the selected class" % cls)
continue
cls_id = classes.index(cls)
for pt in obj.find("polygon").findall("pt"):
X.append(float(pt.find("x").text))
Y.append(float(pt.find("y").text))
if (len(X) < 2 or len(Y) < 2):
logging.warning("%s doesn't have sufficient info, ignore" % cls)
continue
X = list(set(X))
X.sort()
Y = list(set(Y))
Y.sort()
bb = convert((w, h), X, Y)
o_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + "\n")
def find_output_id(out_img_dir, image_id, suffix):
counter = 0
out_img_path = join(out_img_dir, image_id + "." + suffix)
out_id = image_id
while os.path.exists(out_img_path):
logging.info("%s exists" % out_img_path)
counter += 1
out_id = image_id + "_" + str(counter)
out_img_path = join(out_img_dir, out_id + "." + suffix)
return out_id, out_img_path
def main():
parser = argparse.ArgumentParser(
description="Simple tool to make the scene image better."
)
parser.add_argument(
"-v", "--verbosity", action="count",
help="increase output verbosity"
)
parser.add_argument(
"-r", "--root", type=str, default=None,
help="Specify the root directory (default: PWD)"
)
parser.add_argument(
"-n", "--name", type=str, default="youtube_09",
help="Specify the name of the original video (default: youtube_09)"
)
parser.add_argument(
"-u", "--user", type=str, default="V",
help="Specify the username (default: V)"
)
parser.add_argument(
"-o", "--outdir", type=str, default="test2017",
help="Output dir in root, must end with 2017 (default: test2017)"
)
parser.add_argument(
"--classes", nargs="+", type=str, default=["fighting", "dog"],
help="Classes to be trained. Default: [fighting, dog]"
)
parser.add_argument(
"-d", "--delete", action="store_true",
help="Use this option to clean up train.txt"
)
args = parser.parse_args()
log_level = logging.WARNING
if args.verbosity == 1:
log_level = logging.INFO
elif args.verbosity >= 2:
log_level = logging.DEBUG
logging.basicConfig(level=log_level,
format="[xmlTotxt: %(levelname)s] %(message)s")
logging.info(args.classes)
if args.root is None:
args.root = os.getcwd()
in_dir = join(args.root, "Annotations", "users",
args.user, args.name)
in_img_dir = join(args.root, "Images", "users",
args.user, args.name)
out_dir = join(args.root, args.outdir, "labels")
out_img_dir = join(args.root, args.outdir, "JPEGImages")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
if not os.path.exists(out_img_dir):
os.makedirs(out_img_dir)
foutput = join(args.root, "train.txt")
if args.delete:
output = open(foutput, "w")
else:
output = open(foutput, "a")
for _img_path in os.listdir(in_img_dir):
img_path = os.path.join(in_img_dir, _img_path)
suffix = os.path.basename(_img_path).split(".")[1]
logging.debug("Find %s" % img_path)
image_id = os.path.basename(_img_path).split(".")[0]
out_id, out_img_path = find_output_id(out_img_dir, image_id, suffix)
convert_annotation(in_dir, out_dir, image_id, out_id, args.classes)
logging.info("Copy %s to %s" % (img_path, out_img_path))
copyfile(img_path, out_img_path)
output.write(out_img_path + "\n")
if __name__ == '__main__':
main()
|