# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
r"""Runs evaluation using OpenImages groundtruth and predictions.

Example usage:
python models/research/object_detection/metrics/oid_od_challenge_evaluation.py \
    --input_annotations_boxes=/path/to/input/annotations-human-bbox.csv \
    --input_annotations_labels=/path/to/input/annotations-label.csv \
    --input_class_labelmap=/path/to/input/class_labelmap.pbtxt \
    --input_predictions=/path/to/input/predictions.csv \
    --output_metrics=/path/to/output/metric.csv \

CSVs with bounding box annotations and image label (including the image URLs)
can be downloaded from the Open Images Challenge website:
https://storage.googleapis.com/openimages/web/challenge.html
The format of the input csv and the metrics itself are described on the
challenge website.
"""



from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import pandas as pd
from google.protobuf import text_format

from metrics import io_utils
from metrics import oid_od_challenge_evaluation_utils as utils
from protos import string_int_label_map_pb2
from utils import object_detection_evaluation
import cv2

img_dir='/media/chamo/c769deb7-4cd0-4af7-a517-c5601d8313ad/train'

def draw_a_box(img, categories, class_id, ymin, xmin, ymax, xmax, color_id):
    gt_color=(0, 0, 255)
    predict_color = (0, 255, 0)
    class_color_dict=[gt_color, predict_color]
    width = img.shape[1]
    height = img.shape[0]
    xmin = int(xmin * width)
    xmax = int(xmax * width)
    ymin = int(ymin * height)
    ymax = int(ymax * height)
    cv2.rectangle(img, (xmin, ymin), (xmax, ymax), class_color_dict[color_id], 2)
    font = cv2.FONT_HERSHEY_SIMPLEX
    class_name=categories[class_id-1]
    x_sift = int(20 * len(class_name) / 2)
    #img = cv2.putText(img, categories[class_id], (200, 200), font,1 , class_color_dict[color_id], 2)
    img = cv2.putText(img, class_name, (int((xmin + xmax) / 2) - x_sift, int((ymin + ymax) / 2) + 10), font, 1, class_color_dict[color_id], 2)
    return img

def _load_labelmap(labelmap_path):
    label_map = string_int_label_map_pb2.StringIntLabelMap()
    with open(labelmap_path, 'r') as fid:
        label_map_string = fid.read()
        text_format.Merge(label_map_string, label_map)
    labelmap_dict = {}
    categories = []
    for item in label_map.item:
        labelmap_dict[item.name] = item.id
        categories.append({'id': item.id, 'name': item.name})
    return labelmap_dict, categories

def _load_namemap(labelmap_path):
    label_map = string_int_label_map_pb2.StringIntLabelMap()
    with open(labelmap_path, 'r') as fid:
        label_map_string = fid.read()
        text_format.Merge(label_map_string, label_map)
    labelmap_dict = {}
    categories = []
    for item in label_map.item:
        labelmap_dict[item.name] = item.id
        categories.append(item.display_name)
    return labelmap_dict, categories


def main(parsed_args):
    all_box_annotations = pd.read_csv(parsed_args.input_annotations_boxes)
    all_label_annotations = pd.read_csv(parsed_args.input_annotations_labels)
    all_label_annotations.rename(
        columns={'Confidence': 'ConfidenceImageLabel'}, inplace=True)
    all_annotations = pd.concat([all_box_annotations, all_label_annotations])

    class_label_map, categories = _load_namemap(parsed_args.input_class_labelmap)

    predict_dict = {}
    all_predictions = pd.read_csv(parsed_args.input_predictions)
    for _, prediction_data in enumerate(all_predictions.groupby('ImageID')):
        image_id, image_predictions = prediction_data
        prediction_dictionary = utils.build_predictions_dictionary(
            image_predictions, class_label_map)
        predict_dict[image_id] = prediction_dictionary



    for _, groundtruth in enumerate(all_annotations.groupby('ImageID')):
        image_id, image_groundtruth = groundtruth
        #print(image_id)
        groundtruth_dictionary = utils.build_groundtruth_boxes_dictionary(
            image_groundtruth, class_label_map)
        img = cv2.imread(img_dir + '/' + image_id + '.jpg')
        for i in range(len(groundtruth_dictionary['groundtruth_boxes'])):
            box = groundtruth_dictionary['groundtruth_boxes'][i]
            class_id = groundtruth_dictionary['groundtruth_classes'][i]
            img=draw_a_box(img, categories, class_id, box[0], box[1], box[2], box[3], 1)
        l_class=[]
        for class_id in groundtruth_dictionary['groundtruth_image_classes']:
            l_class.append(categories[class_id-1])
        print(','.join(l_class)+'\n')
        if image_id in predict_dict.keys():
            predict= predict_dict[image_id]
            for i in range(len(predict['detection_boxes'])):
                box = predict['detection_boxes'][i]
                class_id = predict['detection_classes'][i]
                img = draw_a_box(img, categories, class_id, box[0], box[1], box[2], box[3], 0)
            print()
            cv2.imshow("Image", img)
            cv2.waitKey(0)

def main1(parsed_args):
    all_box_annotations = pd.read_csv(parsed_args.input_annotations_boxes)
    all_label_annotations = pd.read_csv(parsed_args.input_annotations_labels)
    all_label_annotations.rename(
        columns={'Confidence': 'ConfidenceImageLabel'}, inplace=True)
    all_annotations = pd.concat([all_box_annotations, all_label_annotations])

    class_label_map, categories = _load_labelmap(parsed_args.input_class_labelmap)
    challenge_evaluator = (
        object_detection_evaluation.OpenImagesDetectionChallengeEvaluator(
            categories))

    for _, groundtruth in enumerate(all_annotations.groupby('ImageID')):
        image_id, image_groundtruth = groundtruth
        groundtruth_dictionary = utils.build_groundtruth_boxes_dictionary(
            image_groundtruth, class_label_map)
        challenge_evaluator.add_single_ground_truth_image_info(
            image_id, groundtruth_dictionary)

    all_predictions = pd.read_csv(parsed_args.input_predictions)
    for _, prediction_data in enumerate(all_predictions.groupby('ImageID')):
        image_id, image_predictions = prediction_data
        prediction_dictionary = utils.build_predictions_dictionary(
            image_predictions, class_label_map)
        challenge_evaluator.add_single_detected_image_info(image_id,
                                                           prediction_dictionary)

    metrics = challenge_evaluator.evaluate()

    with open(parsed_args.output_metrics, 'w') as fid:
        io_utils.write_csv(fid, metrics)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Evaluate Open Images Object Detection Challenge predictions.'
    )
    parser.add_argument(
        '--input_annotations_boxes',
        required=True,
        help='File with groundtruth boxes annotations.')
    parser.add_argument(
        '--input_annotations_labels',
        required=True,
        help='File with groundtruth labels annotations')
    parser.add_argument(
        '--input_predictions',
        required=True,
        help="""File with detection predictions; NOTE: no postprocessing is
      applied in the evaluation script.""")
    parser.add_argument(
        '--input_class_labelmap',
        required=True,
        help='Open Images Challenge labelmap.')
    parser.add_argument(
        '--output_metrics', required=True, help='Output file with csv metrics')

    args = parser.parse_args()
    main(args)
