# //  Created by Qazi Ammar Arshad on 18/06/2020.
# //  Copyright © 2020 Qazi Ammar Arshad. All rights reserved.
"""
This file contains the code that first draw annotations on images by this code and save annotation into a JSON file.
Then we upload this annotated image on labelbox and draw box on the cell that are not detected and draw an other box
on the annotated box generated by code which is wrong. after this we download annotated file from the labelbox and
the compare with the annotation file generated by the this code. if 2 boxes overlap on each other then we removed these
boxes from both annotation files.
"""

from custom_classes import path, cv_iml
from RedBloodCell_Loclization.seg_dr_waqas_watershed_microscope_single_image import get_detected_segmentaion \
    , get_mean_gray_image
import cv2

# base path of folder where images and annotation are saved.
folder_base_path = path.dataset_path + "/Shalamar_Captured_Malaria/"
# path of folder where all images are save.
original_images_path = folder_base_path + "original_images/"
# save annotaion path.
# save_annotation_path = folder_base_path + "code_annotation_file/"
save_images_path = folder_base_path + "temp_loc/"

# save_annotation_path = folder_base_path + "code_generated_annotation/"
# background_img = cv2.imread(folder_base_path + "background_img.JPG")
# background_img_1 = cv2.imread(folder_base_path + "back.JPG")

all_images_name = path.read_all_files_name_from(original_images_path, '.JPG')
# all_images_name += path.read_all_files_name_from(original_images_path, '.png')

# %%
# Read image
json_dictionary = []

for image_name in all_images_name:
    # reading images form the folder

    img = cv2.imread(original_images_path + image_name)
    # get annotation box on images.
    annotated_img, individual_cell_images, json_object = get_detected_segmentaion(original_images_path + image_name)
    # classify each boxes as healthy or malaria

    cv2.imwrite(save_images_path + image_name, annotated_img)

    # save_annotation_json = save_annotation_path + image_name.split('.')[0] + ".json"
    # with open(save_annotation_json, "a") as outfile:
    #     json.dump(json_object, outfile)

#     json_objects_array = []
#
#     for bbox in json_object:
#
#         x = int(bbox['x'])
#         y = int(bbox['y'])
#         h = int(bbox['h'])
#         w = int(bbox['w'])
#
#         roi = img[y:y + h, x:x + w]
#         # diff imgs
#         dist1 = calculateDistance(background_img, roi)
#         dist2 = calculateDistance(background_img_1, roi)
#
#         if dist1 < 100 or dist2 < 100:
#             continue
#         json_objects_array.append({"bbox": {"x": x, "y": y, "h": h, "w": w},
#                                    "type": "healthy"})
#
#     # save cell location in json file.
#     json_dictionary.append({
#         "image_name": image_name,
#         "objects": json_objects_array
#     })
#
# # save cell json file.
# save_json_image_path = folder_base_path + "code_generated_localization.json"
# with open(save_json_image_path, "w") as outfile:
#     json.dump(json_dictionary, outfile)
