def play_bboxes(img_dir, gt, dt, obj_class):
    img_list = list_images(img_dir)
    gt_bboxes = read_groundtruth(gt, obj_class)
    dt_bboxes = read_detections(dt, obj_class)
    for img_full_name in img_list:
        img = cv2.imread(img_full_name)
        img_name = os.path.basename(img_full_name)
        img_name, img_ext = os.path.splitext(img_name)
        fid = int(img_name)
        img_gt_bboxes = get_gt_img_bboxes(gt_bboxes, fid)
        img_dt_bboxes = get_dt_img_bboxes(dt_bboxes, fid)
        for gt_bbox in img_gt_bboxes:
            cv2.rectangle(img, (gt_bbox.bbox.ltp.x, gt_bbox.bbox.ltp.y),
                          (gt_bbox.bbox.rbp.x, gt_bbox.bbox.rbp.y),
                          (0, 255, 0), 2)
        for dt_bbox in img_dt_bboxes:
            cv2.rectangle(img, (dt_bbox.bbox.ltp.x, dt_bbox.bbox.ltp.y),
                          (dt_bbox.bbox.rbp.x, dt_bbox.bbox.rbp.y),
                          (0, 0, 255), 2)
        cv2.imshow('Image', img)
        key = cv2.waitKey(0)
        if key == 27:
            break
    cv2.destroyAllWindows()
    return
Esempio n. 2
0
def fppf_rate(gt, dt, objclass, percentage, kimages):
    gt_bboxes = read_groundtruth(gt, objclass)
    dt_bboxes = read_detections(dt, objclass)
    [fp, fn, tp, n] = compute_detection_rates(gt_bboxes, dt_bboxes, percentage)
    print('FP = ' + str(fp))
    print('FN = ' + str(fn))
    print('TP = ' + str(tp))
    print('N = ' + str(n))
    return float(fp) / float(kimages)
def calculate_measurements(groundtruth_directory, detections_directory,
                           object_class, percentage, track_kimages_file,
                           output_file):
    if not os.path.isdir(groundtruth_directory):
        print('Parameter \'{}\' is not a directory.'.format(
            groundtruth_directory))
        return
    if not os.path.isdir(detections_directory):
        print('Parameter \'{}\' is not a directory.'.format(
            detections_directory))
        return
    if not os.path.isfile(track_kimages_file):
        print('Parameter \'{}\' is not a file.'.format(track_kimages_file))
        return

    detection_files = [ f for f in os.listdir(detections_directory) \
        if os.path.isfile(os.path.join(detections_directory, f)) ]
    track_kimages_dict = read_track_frames(track_kimages_file)

    ofile = open(output_file, 'w')
    ofile.write('Track identifier;Model identifier;AP;TPR;FDR;FPperFrame\n')
    for detections_file_name in detection_files:
        matcher = re.match(r'([\w\-]+)_imgs_([\w\-]+)\.txt',
                           detections_file_name)
        if matcher:
            model_name = matcher.group(1)
            track_name = matcher.group(2)
            groundtruth_file_name = os.path.join(groundtruth_directory,
                                                 '{}.txt'.format(track_name))
            if not os.path.isfile(groundtruth_file_name):
                print('There is no groundtruth file \'{0}\' corresponding \
                    to the detection file \'{1}\' in the directory.'.format(
                    groundtruth_file_name, detections_file_name))
                continue
            detections_file_name = os.path.join(detections_directory,
                                                detections_file_name)
            print(groundtruth_file_name)
            gt_bboxes = read_groundtruth(groundtruth_file_name, object_class)
            dt_bboxes = read_detections(detections_file_name)
            kimages = track_kimages_dict[track_name.lower()]
            if kimages == None:
                print('There is no line corresponding to the track {}'.format(
                    track_name))
                continue
            [ap, tpr, fdr,
             fppf] = compute_all_detection_rates(gt_bboxes, dt_bboxes,
                                                 object_class, percentage,
                                                 kimages)
            ofile.write('{0};{1};{2};{3};{4};{5}\n'.format(
                track_name, model_name, ap, tpr, fdr, fppf))
        else:
            print('File \'{}\' does not match a template.'.format(
                detections_file_name))
    ofile.close()
    return
                        help='silent mode (print only TPR)',
                        action='store_true')
    parser.add_argument('-p', '--percentage', help = 'intersection percentage'\
        ' (from 0 to 1, by default 0.5)', type = float, default = 0.5)
    args = parser.parse_args()
    if len(sys.argv) < 3:
        parser.print_help()
        sys.exit()

    # print command line arguments
    if args.quiet:
        print(
            average_precision_curve(args.groundtruth, args.detections,
                                    args.object_class, args.percentage))
    else:
        print('-----------------------------------')
        print('Groundtruth: ' + args.groundtruth)
        print('Detections: ' + args.detections)
        print('Object class: ' + args.object_class)
        print('Intersection percentage: ' + str(args.percentage))
        print('-----------------------------------')
        gt_bboxes = read_groundtruth(args.groundtruth, args.object_class)
        dt_bboxes = read_detections(args.detections, args.object_class)
        [ap, recall,
         precision] = average_precision_curve(gt_bboxes, dt_bboxes,
                                              args.object_class,
                                              args.percentage)
        print('Average precision = ' + str(ap))
        show_precision_recall_curve(ap, recall, precision)
        print('-----------------------------------')
Esempio n. 5
0
def read_bboxes(bboxes_file):
    bboxes = read_groundtruth(bboxes_file)
    if len(bboxes) == 0:
        bboxes = read_detections(bboxes_file)
    return bboxes