def load_voc_dataset(path='<STR_LIT:data>', dataset='<STR_LIT>', contain_classes_in_person=False):
path = os.path.join(path, '<STR_LIT>')<EOL>def _recursive_parse_xml_to_dict(xml):<EOL><INDENT>"""<STR_LIT>"""<EOL>if xml is not None:<EOL><INDENT>return {xml.tag: xml.text}<EOL><DEDENT>result = {}<EOL>for child in xml:<EOL><INDENT>child_result = _recursive_parse_xml_to_dict(child)<EOL>if child.tag != '<STR_LIT:object>':<EOL><INDENT>result[child.tag] = child_result[child.tag]<EOL><DEDENT>else:<EOL><INDENT>if child.tag not in result:<EOL><INDENT>result[child.tag] = []<EOL><DEDENT>result[child.tag].append(child_result[child.tag])<EOL><DEDENT><DEDENT>return {xml.tag: result}<EOL><DEDENT>import xml.etree.ElementTree as ET<EOL>if dataset == "<STR_LIT>":<EOL><INDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"  <EOL>logging.info("<STR_LIT>")<EOL><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>extracted_filename = "<STR_LIT>"  <EOL>logging.info("<STR_LIT>")<EOL>logging.info(<EOL>"<STR_LIT>"<EOL>)<EOL>import time<EOL>time.sleep(<NUM_LIT:3>)<EOL>if os.path.isdir(os.path.join(path, extracted_filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>")<EOL>logging.info(<EOL>"<STR_LIT>"<EOL>)<EOL>logging.info("<STR_LIT>" % path)<EOL>exit()<EOL><DEDENT><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"<EOL>logging.info("<STR_LIT>")<EOL><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>url = "<STR_LIT>"<EOL>tar_filename = "<STR_LIT>"<EOL>extracted_filename = "<STR_LIT>"<EOL>logging.info("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>if dataset != "<STR_LIT>":<EOL><INDENT>from sys import platform as _platform<EOL>if folder_exists(os.path.join(path, extracted_filename)) is False:<EOL><INDENT>logging.info("<STR_LIT>".format(extracted_filename, path))<EOL>maybe_download_and_extract(tar_filename, path, url, extract=True)<EOL>del_file(os.path.join(path, tar_filename))<EOL>if dataset == "<STR_LIT>":<EOL><INDENT>if _platform == "<STR_LIT:win32>":<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT>else:<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>if _platform == "<STR_LIT:win32>":<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT>else:<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT><DEDENT>elif dataset == "<STR_LIT>":<EOL><INDENT>if _platform == "<STR_LIT:win32>":<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT>else:<EOL><INDENT>os.system("<STR_LIT>".format(path, path))<EOL><DEDENT><DEDENT>del_folder(os.path.join(path, '<STR_LIT>'))<EOL><DEDENT><DEDENT>classes = [<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>",<EOL>"<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT:train>", "<STR_LIT>"<EOL>]<EOL>if contain_classes_in_person:<EOL><INDENT>classes_in_person = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL><DEDENT>else:<EOL><INDENT>classes_in_person = []<EOL><DEDENT>classes += classes_in_person  <EOL>classes_dict = utils.list_string_to_dict(classes)<EOL>logging.info("<STR_LIT>".format(classes_dict))<EOL>folder_imgs = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_file_list = load_file_list(path=folder_imgs, regx='<STR_LIT>', printable=False)<EOL>logging.info("<STR_LIT>".format(len(imgs_file_list)))<EOL>imgs_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_file_list = [os.path.join(folder_imgs, s) for s in imgs_file_list]<EOL>if dataset != "<STR_LIT>":<EOL><INDENT>folder_semseg = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_semseg_file_list = load_file_list(path=folder_semseg, regx='<STR_LIT>', printable=False)<EOL>logging.info("<STR_LIT>".format(len(imgs_semseg_file_list)))<EOL>imgs_semseg_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_semseg_file_list = [os.path.join(folder_semseg, s) for s in imgs_semseg_file_list]<EOL>folder_insseg = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_insseg_file_list = load_file_list(path=folder_insseg, regx='<STR_LIT>', printable=False)<EOL>logging.info("<STR_LIT>".format(len(imgs_semseg_file_list)))<EOL>imgs_insseg_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_insseg_file_list = [os.path.join(folder_insseg, s) for s in imgs_insseg_file_list]<EOL><DEDENT>else:<EOL><INDENT>imgs_semseg_file_list = []<EOL>imgs_insseg_file_list = []<EOL><DEDENT>folder_ann = os.path.join(path, extracted_filename, "<STR_LIT>")<EOL>imgs_ann_file_list = load_file_list(path=folder_ann, regx='<STR_LIT>', printable=False)<EOL>logging.info(<EOL>"<STR_LIT>".format(len(imgs_ann_file_list))<EOL>)<EOL>imgs_ann_file_list.sort(<EOL>key=lambda s: int(s.replace('<STR_LIT:.>', '<STR_LIT:U+0020>').replace('<STR_LIT:_>', '<STR_LIT>').split('<STR_LIT:U+0020>')[-<NUM_LIT:2>])<EOL>)  <EOL>imgs_ann_file_list = [os.path.join(folder_ann, s) for s in imgs_ann_file_list]<EOL>if dataset == "<STR_LIT>":  <EOL><INDENT>imgs_file_list_new = []<EOL>for ann in imgs_ann_file_list:<EOL><INDENT>ann = os.path.split(ann)[-<NUM_LIT:1>].split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL>for im in imgs_file_list:<EOL><INDENT>if ann in im:<EOL><INDENT>imgs_file_list_new.append(im)<EOL>break<EOL><DEDENT><DEDENT><DEDENT>imgs_file_list = imgs_file_list_new<EOL>logging.info("<STR_LIT>" % len(imgs_file_list_new))<EOL><DEDENT>def convert(size, box):<EOL><INDENT>dw = <NUM_LIT:1.> / size[<NUM_LIT:0>]<EOL>dh = <NUM_LIT:1.> / size[<NUM_LIT:1>]<EOL>x = (box[<NUM_LIT:0>] + box[<NUM_LIT:1>]) / <NUM_LIT><EOL>y = (box[<NUM_LIT:2>] + box[<NUM_LIT:3>]) / <NUM_LIT><EOL>w = box[<NUM_LIT:1>] - box[<NUM_LIT:0>]<EOL>h = box[<NUM_LIT:3>] - box[<NUM_LIT:2>]<EOL>x = x * dw<EOL>w = w * dw<EOL>y = y * dh<EOL>h = h * dh<EOL>return x, y, w, h<EOL><DEDENT>def convert_annotation(file_name):<EOL><INDENT>"""<STR_LIT>"""<EOL>in_file = open(file_name)<EOL>out_file = "<STR_LIT>"<EOL>tree = ET.parse(in_file)<EOL>root = tree.getroot()<EOL>size = root.find('<STR_LIT:size>')<EOL>w = int(size.find('<STR_LIT:width>').text)<EOL>h = int(size.find('<STR_LIT>').text)<EOL>n_objs = <NUM_LIT:0><EOL>for obj in root.iter('<STR_LIT:object>'):<EOL><INDENT>if dataset != "<STR_LIT>":<EOL><INDENT>difficult = obj.find('<STR_LIT>').text<EOL>cls = obj.find('<STR_LIT:name>').text<EOL>if cls not in classes or int(difficult) == <NUM_LIT:1>:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>else:<EOL><INDENT>cls = obj.find('<STR_LIT:name>').text<EOL>if cls not in classes:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>cls_id = classes.index(cls)<EOL>xmlbox = obj.find('<STR_LIT>')<EOL>b = (<EOL>float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text),<EOL>float(xmlbox.find('<STR_LIT>').text)<EOL>)<EOL>bb = convert((w, h), b)<EOL>out_file += str(cls_id) + "<STR_LIT:U+0020>" + "<STR_LIT:U+0020>".join([str(a) for a in bb]) + '<STR_LIT:\n>'<EOL>n_objs += <NUM_LIT:1><EOL>if cls in "<STR_LIT>":<EOL><INDENT>for part in obj.iter('<STR_LIT>'):<EOL><INDENT>cls = part.find('<STR_LIT:name>').text<EOL>if cls not in classes_in_person:<EOL><INDENT>continue<EOL><DEDENT>cls_id = classes.index(cls)<EOL>xmlbox = part.find('<STR_LIT>')<EOL>b = (<EOL>float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text),<EOL>float(xmlbox.find('<STR_LIT>').text), float(xmlbox.find('<STR_LIT>').text)<EOL>)<EOL>bb = convert((w, h), b)<EOL>out_file += str(cls_id) + "<STR_LIT:U+0020>" + "<STR_LIT:U+0020>".join([str(a) for a in bb]) + '<STR_LIT:\n>'<EOL>n_objs += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>in_file.close()<EOL>return n_objs, out_file<EOL><DEDENT>logging.info("<STR_LIT>")<EOL>n_objs_list = []<EOL>objs_info_list = []  <EOL>objs_info_dicts = {}<EOL>for idx, ann_file in enumerate(imgs_ann_file_list):<EOL><INDENT>n_objs, objs_info = convert_annotation(ann_file)<EOL>n_objs_list.append(n_objs)<EOL>objs_info_list.append(objs_info)<EOL>with tf.gfile.GFile(ann_file, '<STR_LIT:r>') as fid:<EOL><INDENT>xml_str = fid.read()<EOL><DEDENT>xml = etree.fromstring(xml_str)<EOL>data = _recursive_parse_xml_to_dict(xml)['<STR_LIT>']<EOL>objs_info_dicts.update({imgs_file_list[idx]: data})<EOL><DEDENT>return imgs_file_list, imgs_semseg_file_list, imgs_insseg_file_list, imgs_ann_file_list, classes, classes_in_person, classes_dict, n_objs_list, objs_info_list, objs_info_dicts<EOL>
Pascal VOC 2007/2012 Dataset.

    It has 20 objects:
    aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, diningtable, dog, horse, motorbike, person, pottedplant, sheep, sofa, train, tvmonitor
    and additional 3 classes : head, hand, foot for person.

    Parameters
    -----------
    path : str
        The path that the data is downloaded to, defaults is ``data/VOC``.
    dataset : str
        The VOC dataset version, `2012`, `2007`, `2007test` or `2012test`. We usually train model on `2007+2012` and test it on `2007test`.
    contain_classes_in_person : boolean
        Whether include head, hand and foot annotation, default is False.

    Returns
    ---------
    imgs_file_list : list of str
        Full paths of all images.
    imgs_semseg_file_list : list of str
        Full paths of all maps for semantic segmentation. Note that not all images have this map!
    imgs_insseg_file_list : list of str
        Full paths of all maps for instance segmentation. Note that not all images have this map!
    imgs_ann_file_list : list of str
        Full paths of all annotations for bounding box and object class, all images have this annotations.
    classes : list of str
        Classes in order.
    classes_in_person : list of str
        Classes in person.
    classes_dict : dictionary
        Class label to integer.
    n_objs_list : list of int
        Number of objects in all images in ``imgs_file_list`` in order.
    objs_info_list : list of str
        Darknet format for the annotation of all images in ``imgs_file_list`` in order. ``[class_id x_centre y_centre width height]`` in ratio format.
    objs_info_dicts : dictionary
        The annotation of all images in ``imgs_file_list``, ``{imgs_file_list : dictionary for annotation}``,
        format from `TensorFlow/Models/object-detection <https://github.com/tensorflow/models/blob/master/object_detection/create_pascal_tf_record.py>`__.

    Examples
    ----------
    >>> imgs_file_list, imgs_semseg_file_list, imgs_insseg_file_list, imgs_ann_file_list,
    >>>     classes, classes_in_person, classes_dict,
    >>>     n_objs_list, objs_info_list, objs_info_dicts = tl.files.load_voc_dataset(dataset="2012", contain_classes_in_person=False)
    >>> idx = 26
    >>> print(classes)
    ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
    >>> print(classes_dict)
    {'sheep': 16, 'horse': 12, 'bicycle': 1, 'bottle': 4, 'cow': 9, 'sofa': 17, 'car': 6, 'dog': 11, 'cat': 7, 'person': 14, 'train': 18, 'diningtable': 10, 'aeroplane': 0, 'bus': 5, 'pottedplant': 15, 'tvmonitor': 19, 'chair': 8, 'bird': 2, 'boat': 3, 'motorbike': 13}
    >>> print(imgs_file_list[idx])
    data/VOC/VOC2012/JPEGImages/2007_000423.jpg
    >>> print(n_objs_list[idx])
    2
    >>> print(imgs_ann_file_list[idx])
    data/VOC/VOC2012/Annotations/2007_000423.xml
    >>> print(objs_info_list[idx])
    14 0.173 0.461333333333 0.142 0.496
    14 0.828 0.542666666667 0.188 0.594666666667
    >>> ann = tl.prepro.parse_darknet_ann_str_to_list(objs_info_list[idx])
    >>> print(ann)
    [[14, 0.173, 0.461333333333, 0.142, 0.496], [14, 0.828, 0.542666666667, 0.188, 0.594666666667]]
    >>> c, b = tl.prepro.parse_darknet_ann_list_to_cls_box(ann)
    >>> print(c, b)
    [14, 14] [[0.173, 0.461333333333, 0.142, 0.496], [0.828, 0.542666666667, 0.188, 0.594666666667]]

    References
    -------------
    - `Pascal VOC2012 Website <https://pjreddie.com/projects/pascal-voc-dataset-mirror/>`__.
    - `Pascal VOC2007 Website <https://pjreddie.com/projects/pascal-voc-dataset-mirror/>`__.