Esempio n. 1
0
def main():
    # random.seed(123454321) # Use deterministic samples.

    # Parse arguments:
    parser = argparse.ArgumentParser(
        description='Train a HOG + Linear SVM classifier.')
    parser.add_argument(
        'classifier_yaml',
        type=str,
        nargs='?',
        default='template.yaml',
        help='Filename of the YAML file describing the classifier to train.')
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.classifier_yaml)
    output_dir = args.classifier_yaml.split('.yaml')[0]

    window_dims = tuple(
        map(int, classifier_yaml['training']['svm']['window_dims']))
    print 'window_dims:', window_dims

    print 'Preview negative generation:'
    print '  [ESC]  Stop viewing negatives'
    print '  [ S ]  Save negative regions to disk'
    neg_num = int(classifier_yaml['training']['svm']['neg_num'])
    neg_output_dir = classifier_yaml['dataset']['directory']['generation'][
        'output']['negative']

    def get_neg_reg_gen():
        # return generate_samples.load_negative_region_generator(classifier_yaml)
        # return generate_samples.load_exclusion_region_generator(classifier_yaml)
        return generate_samples.load_hard_negative_region_generator(
            classifier_yaml)

    # # TODO: REMOVE THIS CODE:
    # save_regions(get_neg_reg_gen(), neg_num, window_dims, neg_output_dir)
    # sys.exit(1)

    # neg_reg_generator = generate_samples.generate_negative_regions_in_image_with_exclusions(all_images[0], exl_info_map, window_dims)
    # print len(list(neg_reg_generator))
    mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (5, 5), (100, 100))
    # mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (10, 15), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_neg_reg_gen(), neg_num, window_dims,
                             neg_output_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break

    print 'Preview positive generation:'
    print '  [ESC]  Stop viewing positives'
    print '  [ S ]  Save positive regions to disk'
    pos_num = int(classifier_yaml['training']['svm']['pos_num'])
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    pos_output_dir = classifier_yaml['dataset']['directory']['generation'][
        'output']['positive']

    def get_pos_reg_gen():
        return generate_samples.load_positive_region_generator(classifier_yaml)

    mosaic_gen = utils.mosaic_generator(get_pos_reg_gen(), (4, 6),
                                        (window_dims[1], window_dims[0]))
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (20, 30), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_pos_reg_gen(), pos_num, window_dims,
                             pos_output_dir)
                save_generated_bbinfo(pos_num, window_dims, pos_output_dir,
                                      bbinfo_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break
Esempio n. 2
0
def main():
    # random.seed(123454321) # Use deterministic samples.

    # Parse arguments:
    parser = argparse.ArgumentParser(description='Train a HOG + Linear SVM classifier.')
    parser.add_argument('classifier_yaml', type=str, nargs='?', default='template.yaml', help='Filename of the YAML file describing the classifier to train.')
    args = parser.parse_args()

    # Read classifier training file:
    classifier_yaml = fileutils.load_yaml_file(args.classifier_yaml)
    output_dir = args.classifier_yaml.split('.yaml')[0]

    window_dims = tuple(map(int, classifier_yaml['training']['svm']['window_dims']))
    print 'window_dims:', window_dims

    print 'Preview negative generation:'
    print '  [ESC]  Stop viewing negatives'
    print '  [ S ]  Save negative regions to disk'
    neg_num = int(classifier_yaml['training']['svm']['neg_num'])
    neg_output_dir = classifier_yaml['dataset']['directory']['generation']['output']['negative']
    def get_neg_reg_gen():
        # return generate_samples.load_negative_region_generator(classifier_yaml)
        # return generate_samples.load_exclusion_region_generator(classifier_yaml)
        return generate_samples.load_hard_negative_region_generator(classifier_yaml)

    # # TODO: REMOVE THIS CODE:
    # save_regions(get_neg_reg_gen(), neg_num, window_dims, neg_output_dir)
    # sys.exit(1)

    # neg_reg_generator = generate_samples.generate_negative_regions_in_image_with_exclusions(all_images[0], exl_info_map, window_dims)
    # print len(list(neg_reg_generator))
    mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (5, 5), (100, 100))
    # mosaic_gen = utils.mosaic_generator(get_neg_reg_gen(), (10, 15), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_neg_reg_gen(), neg_num, window_dims, neg_output_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break

    print 'Preview positive generation:'
    print '  [ESC]  Stop viewing positives'
    print '  [ S ]  Save positive regions to disk'
    pos_num = int(classifier_yaml['training']['svm']['pos_num'])
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    pos_output_dir = classifier_yaml['dataset']['directory']['generation']['output']['positive']
    def get_pos_reg_gen():
        return generate_samples.load_positive_region_generator(classifier_yaml)
    mosaic_gen = utils.mosaic_generator(get_pos_reg_gen(), (4, 6), (window_dims[1], window_dims[0]))
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (20, 30), (40, 60))
    stop = False
    for mosaic in mosaic_gen:
        print 'mosaic'
        cv2.imshow('mosaic', mosaic)
        while True:
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                stop = True
                break
            if key == ord('s'):
                save_regions(get_pos_reg_gen(), pos_num, window_dims, pos_output_dir)
                save_generated_bbinfo(pos_num, window_dims, pos_output_dir, bbinfo_dir)
                stop = True
                break
            if key != 255:
                break
        if stop:
            break
Esempio n. 3
0
    pos_reg_generator = get_pos_generator()
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (10, 10), (window_dims[1], window_dims[0]))

    output_dir = 'output'
    if not os.path.isdir(output_dir):
        print 'ERROR: Output directory "{}" does not exist.'.format(output_dir)
        sys.exit(1)
    else:
        print 'Using output directory "{}".'.format(output_dir)
    base_fname = os.path.join(output_dir, 'cluster')

    # Get the HOG object to use for clustering:
    hog = trainhog.get_hog_object(window_dims)

    # Find the clusters:
    num_clusters = 5
    clusters = alignment.find_sample_clusters(pos_reg_generator, window_dims, hog, num_clusters)

    # Save the clusters:
    for i, cluster in enumerate(clusters):
        mosaic_fname = '{}_{}_mosaic.png'.format(base_fname, i)
        mosaic_gen = utils.mosaic_generator(cluster, (10, 10), window_shape)
        cv2.imwrite(mosaic_fname, mosaic_gen.next())

        avg_fname = '{}_{}_avg.png'.format(base_fname, i)
        avg_img = utils.average_image(cluster, window_shape, avg_num=None)
        cv2.imwrite(avg_fname, avg_img)

    # sys.exit(0)