# Path: object_detection/create_dataset.py
# def xml_to_dict(path):
#     with tf.gfile.GFile(path, 'r') as fid:
#         xml_str = fid.read()
#     xml = etree.fromstring(xml_str)
#     return dataset_util.recursive_parse_xml_to_dict(xml)['annotation']


import os
import numpy as np
from PIL import Image as Image
from object_detection.utils.visualization_utils import visualize_boxes_and_labels_on_image_array
from object_detection.create_dataset import xml_to_dict
from object_detection.kitti_constants import *
    '''Draw bboxes from annotations file on an image'''
    image = Image.open(image_path)
    image_np = load_image_into_numpy_array(image)

    data = get_annotations(image_path)
    classes = np.array([name_to_id[x['name'].lower()]
                        for x in data['object']])
    scores = np.ones(len(data['object']))
    if classes.ndim != 1:
        classes = np.squeeze(classes).astype(np.int32)
    if scores.ndim != 1:
        scores = np.squeeze(scores).astype(np.int32)
    box_mat = box_to_matrix(data)
    visualize_boxes_and_labels_on_image_array(
        image_np,
        box_mat,
        classes,
        scores,
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8)
    return image_np


def get_annotations(image_path):
    img_id = os.path.basename(image_path)[:-4]
    annotation_path = os.path.join(
    os.path.split(os.path.dirname(image_path))[0], 'Annotations',
    '{}.xml'.format(img_id)
    )

# Path: object_detection/create_dataset.py
# def xml_to_dict(path):
#     with tf.gfile.GFile(path, 'r') as fid:
#         xml_str = fid.read()
#     xml = etree.fromstring(xml_str)
#     return dataset_util.recursive_parse_xml_to_dict(xml)['annotation']


# Path: object_detection/utils/kitti.py
import os
import numpy as np
from PIL import Image as Image
from object_detection.utils.visualization_utils import visualize_boxes_and_labels_on_image_array
from object_detection.create_dataset import xml_to_dict
from object_detection.kitti_constants import *

    '''Draw bboxes from annotations file on an image'''
    image = Image.open(image_path)
    image_np = load_image_into_numpy_array(image)

    data = get_annotations(image_path)
    classes = np.array([name_to_id[x['name'].lower()]
                        for x in data['object']])
    scores = np.ones(len(data['object']))
    if classes.ndim != 1:
        classes = np.squeeze(classes).astype(np.int32)
    if scores.ndim != 1:
        scores = np.squeeze(scores).astype(np.int32)
    box_mat = box_to_matrix(data)
    visualize_boxes_and_labels_on_image_array(
        image_np,
        box_mat,
        classes,
        scores,
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8)
    return image_np


def get_annotations(image_path):
    img_id = os.path.basename(image_path)[:-4]
    annotation_path = os.path.join(
    os.path.split(os.path.dirname(image_path))[0], 'Annotations',
    '{}.xml'.format(img_id)
    )

    return xml_to_dict(annotation_path)