import json


def make_detection_res(results_path, file_prefix=''):
    """ Make the detection result into a dictionary of file : result entry

    Args:
        results_path: path to output of API containing the detection results
        file_prefix: this prefix will be taken out of the result entry 'file' field to be consistent
            with the 'file_name' field in a CCT formatted json DB.
    Returns:
        A dictionary of file : API result entry
    """
    with open(results_path) as f:
        res = json.load(f)

    detection_res = {}

    for i in res['images']:
        file_name = i['file'].split(file_prefix)[1].split('.jpg')[0].split('.JPG')[0]
        detection_res[file_name] = i  # all detections on that image is in this dict
    return detection_res