def read_txt_file(filename):
    """
    Reads text files in the keras-yolo3 format, one file per line, with
    all objects space-separated within each line, object details comma-separated:

    path-to-file1.jpg xmin,ymin,xmax,ymax,class_id[,confidence] xmin,ymin,xmax,ymax,class_id[,confidence]
    path-to-file2.jpg xmin,ymin,xmax,ymax,class_id[,confidence]

    If file is detailing ground truths, each object info has 5 elements,
    that is, 4 coordinates + class index; if detailing model predictions,
    the prediction confidence is the last element for each box.

    Args:
      filename: path to text file
    Returns:
      img_list: list of paths to images
      bbox_list: list of bounding boxes
    """
    with open(filename, 'r') as file:
        img_list = []
        bbox_list = []
        for line in file.read().splitlines():
            img, bbox = line.split(' ')[0],  line.split(' ')[1:]
            img_list.append(img)

            bbox = [ bb for bb in bbox if bb != '' ]

            # skip if no predictions made
            if len(bbox)==0:
                bbox_list.append([])
                continue

            if len(bbox[0].split(','))==5:
                bbox = [[int(x) for x in bb.split(',')] for bb in bbox]
            elif len(bbox[0].split(','))==6:
                bbox = [[int(x) for x in bb.split(',')[:-1]] + [float(bb.split(',')[-1])] for bb in bbox]
            else:
                print(bbox[0])
                # raise Exception('Error: bounding boxes should be defined by either 5 or 6 comma-separated entries!')

            # sort objects by prediction confidence
            bbox = sorted(bbox, key = lambda x: x[-1], reverse=True)
            bbox_list.append(bbox)
        return img_list, bbox_list