Beispiel #1
0
    def db_name_for_hog(self, hog):
        hog_name = utils.name_from_hog_descriptor(hog)
        # Note: MongoDB limits database names to 64 characters, making hog_name too long.
        #       Its hash value encoded in base64 is used instead.
        import base64

        return base64.urlsafe_b64encode(repr(hash(hog_name)))
Beispiel #2
0
 def db_name_for_hog(self, hog):
     hog_name = utils.name_from_hog_descriptor(hog)
     # Note: MongoDB limits database names to 64 characters, making hog_name too long.
     #       Its hash value encoded in base64 is used instead.
     import base64
     return base64.urlsafe_b64encode(repr(hash(hog_name)))
Beispiel #3
0
def create_or_load_descriptors(classifier_yaml, hog, window_dims):
    pos_img_dir = classifier_yaml["dataset"]["directory"]["positive"]
    pos_num = int(classifier_yaml["training"]["svm"]["pos_num"])
    bbinfo_dir = classifier_yaml["dataset"]["directory"]["bbinfo"]
    bak_img_dir = classifier_yaml["dataset"]["directory"]["background"]
    neg_num = int(classifier_yaml["training"]["svm"]["neg_num"])

    # pos_descriptor_file = 'output/pos_descriptors.pickle'
    # neg_descriptor_file = 'output/neg_descriptors.pickle'
    # if not (os.path.isfile(pos_descriptor_file) and os.path.isfile(neg_descriptor_file)):
    store = DataStore()

    print "db_name:", store.db_name_for_hog(hog)
    print "hog_name:", utils.name_from_hog_descriptor(hog)

    # if not store.has_region_descriptors_for_hog(hog):
    # Preprocess samples:
    #   - generate/select sample regions
    #   - compute and descriptors for all samples
    #   - save the regions and descriptors to a file

    curr_pos_num = store.num_region_descriptors(hog, 1)
    req_pos_num = pos_num if curr_pos_num is None else pos_num - curr_pos_num
    print "pos_num:", pos_num
    print "curr_pos_num:", curr_pos_num
    print "req_pos_num:", req_pos_num
    if req_pos_num > 0:
        # if not os.path.isfile(pos_descriptor_file):
        # pos_reg_generator = generate_positive_regions(pos_img_dir, bbinfo_dir, classifier_yaml['dataset']['modifiers'], 0.5*np.array(window_dims))
        pos_reg_generator = generate_samples.generate_positive_regions(
            pos_img_dir, bbinfo_dir, classifier_yaml["dataset"]["modifiers"], window_dims
        )
        # view_image_regions(pos_reg_generator, window_dims, display_scale=3)
        pos_regions = list(itertools.islice(pos_reg_generator, 0, req_pos_num))
        pos_reg_descriptors = compute_hog_descriptors(hog, pos_regions, window_dims, 1)
        # save_region_descriptors(hog, pos_regions, pos_descriptors, 'pos_descriptors')
        store.save_region_descriptors(pos_reg_descriptors, hog)

    curr_neg_num = store.num_region_descriptors(hog, -1)
    req_neg_num = neg_num if curr_neg_num is None else neg_num - curr_neg_num
    print "neg_num:", neg_num
    print "curr_neg_num:", curr_neg_num
    print "req_neg_num:", req_neg_num
    if req_neg_num > 0:
        # if not os.path.isfile(neg_descriptor_file):
        # neg_regions = generate_negative_regions(bak_img_dir, req_neg_num, window_dims)
        exl_info_map = utils.load_opencv_bounding_box_info_directory(bbinfo_dir, suffix="exclusion")
        neg_reg_generator = generate_samples.generate_negative_regions_with_exclusions(
            bak_img_dir, exl_info_map, window_dims, classifier_yaml["dataset"]["modifiers"]
        )
        neg_regions = list(itertools.islice(neg_reg_generator, 0, req_neg_num))
        neg_reg_descriptors = compute_hog_descriptors(hog, neg_regions, window_dims, -1)
        # save_region_descriptors(hog, neg_regions, neg_descriptors, 'neg_descriptors')
        store.save_region_descriptors(neg_reg_descriptors, hog)
    # else:
    # Load all descriptors and the hog object used to generate them:
    # hog, reg_descriptors = store.load_region_descriptors_for_hog(hog)
    hog, pos_reg_descriptors = store.load_region_descriptors_for_hog(hog, pos_num, 1)
    hog, neg_reg_descriptors = store.load_region_descriptors_for_hog(hog, neg_num, -1)
    # hog, reg_descriptors = store.load_region_descriptors('hog_Llht0,2_n64_gcFalse_hnt0_bs8x8_cs8x8_ws4,0_da1')
    #     hog, pos_regions, pos_descriptors = load_region_descriptors(pos_descriptor_file)
    #     hog, neg_regions, neg_descriptors = load_region_descriptors(neg_descriptor_file)

    reg_descriptors = list(pos_reg_descriptors) + list(neg_reg_descriptors)
    return reg_descriptors, hog
Beispiel #4
0
    # # all_images = utils.list_images_in_directory(bak_img_dir)
    # # neg_reg_generator = generate_negative_regions_in_image_with_exclusions(all_images[0], exl_info_map, window_dims)
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (4, 6), (window_dims[1], window_dims[0]))
    # # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (20, 30), (40, 60))
    # for mosaic in mosaic_gen:
    #     cv2.imshow('mosaic', mosaic)
    #     while True:
    #         key = cv2.waitKey(1) & 0xFF
    #         if key != 255:
    #             break
    # view_image_regions(neg_reg_generator, window_dims, display_scale=2)

    # Create the hog object with which to compute the descriptors:
    hog = get_hog_object(window_dims)

    db_name = utils.name_from_hog_descriptor(hog)
    svm_name = strutils.safe_name_from_info_dict(classifier_yaml["training"]["svm"], "svm_")
    svm_save_path = "output/car_detector_{}_{}.yaml".format(svm_name, db_name)
    if not os.path.isfile(svm_save_path):

        reg_descriptors, hog = create_or_load_descriptors(classifier_yaml, hog, window_dims)

        # Create lists of samples and labels:
        descriptors = [rd.descriptor for rd in reg_descriptors]
        labels = [rd.label for rd in reg_descriptors]

        # Train the classifier:
        train_svm(svm_save_path, descriptors, labels)

    # Test the classifier:
    test_classifier(classifier_yaml, svm_save_path, window_dims)
Beispiel #5
0
def create_or_load_descriptors(classifier_yaml, hog, window_dims):
    pos_img_dir = classifier_yaml['dataset']['directory']['positive']
    pos_num = int(classifier_yaml['training']['svm']['pos_num'])
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']
    bak_img_dir = classifier_yaml['dataset']['directory']['background']
    neg_num = int(classifier_yaml['training']['svm']['neg_num'])

    # pos_descriptor_file = 'output/pos_descriptors.pickle'
    # neg_descriptor_file = 'output/neg_descriptors.pickle'
    # if not (os.path.isfile(pos_descriptor_file) and os.path.isfile(neg_descriptor_file)):
    store = DataStore()

    print 'db_name:', store.db_name_for_hog(hog)
    print 'hog_name:', utils.name_from_hog_descriptor(hog)

    # if not store.has_region_descriptors_for_hog(hog):
    # Preprocess samples:
    #   - generate/select sample regions
    #   - compute and descriptors for all samples
    #   - save the regions and descriptors to a file

    curr_pos_num = store.num_region_descriptors(hog, 1)
    req_pos_num = pos_num if curr_pos_num is None else pos_num - curr_pos_num
    print 'pos_num:', pos_num
    print 'curr_pos_num:', curr_pos_num
    print 'req_pos_num:', req_pos_num
    if req_pos_num > 0:
        # if not os.path.isfile(pos_descriptor_file):
        # pos_reg_generator = generate_positive_regions(pos_img_dir, bbinfo_dir, classifier_yaml['dataset']['modifiers'], 0.5*np.array(window_dims))
        pos_reg_generator = generate_samples.generate_positive_regions(
            pos_img_dir, bbinfo_dir, classifier_yaml['dataset']['modifiers'],
            window_dims)
        # view_image_regions(pos_reg_generator, window_dims, display_scale=3)
        pos_regions = list(itertools.islice(pos_reg_generator, 0, req_pos_num))
        pos_reg_descriptors = compute_hog_descriptors(hog, pos_regions,
                                                      window_dims, 1)
        # save_region_descriptors(hog, pos_regions, pos_descriptors, 'pos_descriptors')
        store.save_region_descriptors(pos_reg_descriptors, hog)

    curr_neg_num = store.num_region_descriptors(hog, -1)
    req_neg_num = neg_num if curr_neg_num is None else neg_num - curr_neg_num
    print 'neg_num:', neg_num
    print 'curr_neg_num:', curr_neg_num
    print 'req_neg_num:', req_neg_num
    if req_neg_num > 0:
        # if not os.path.isfile(neg_descriptor_file):
        # neg_regions = generate_negative_regions(bak_img_dir, req_neg_num, window_dims)
        exl_info_map = utils.load_opencv_bounding_box_info_directory(
            bbinfo_dir, suffix='exclusion')
        neg_reg_generator = generate_samples.generate_negative_regions_with_exclusions(
            bak_img_dir, exl_info_map, window_dims,
            classifier_yaml['dataset']['modifiers'])
        neg_regions = list(itertools.islice(neg_reg_generator, 0, req_neg_num))
        neg_reg_descriptors = compute_hog_descriptors(hog, neg_regions,
                                                      window_dims, -1)
        # save_region_descriptors(hog, neg_regions, neg_descriptors, 'neg_descriptors')
        store.save_region_descriptors(neg_reg_descriptors, hog)
    # else:
    # Load all descriptors and the hog object used to generate them:
    # hog, reg_descriptors = store.load_region_descriptors_for_hog(hog)
    hog, pos_reg_descriptors = store.load_region_descriptors_for_hog(
        hog, pos_num, 1)
    hog, neg_reg_descriptors = store.load_region_descriptors_for_hog(
        hog, neg_num, -1)
    # hog, reg_descriptors = store.load_region_descriptors('hog_Llht0,2_n64_gcFalse_hnt0_bs8x8_cs8x8_ws4,0_da1')
    #     hog, pos_regions, pos_descriptors = load_region_descriptors(pos_descriptor_file)
    #     hog, neg_regions, neg_descriptors = load_region_descriptors(neg_descriptor_file)

    reg_descriptors = list(pos_reg_descriptors) + list(neg_reg_descriptors)
    return reg_descriptors, hog
Beispiel #6
0
    # # all_images = utils.list_images_in_directory(bak_img_dir)
    # # neg_reg_generator = generate_negative_regions_in_image_with_exclusions(all_images[0], exl_info_map, window_dims)
    # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (4, 6), (window_dims[1], window_dims[0]))
    # # mosaic_gen = utils.mosaic_generator(pos_reg_generator, (20, 30), (40, 60))
    # for mosaic in mosaic_gen:
    #     cv2.imshow('mosaic', mosaic)
    #     while True:
    #         key = cv2.waitKey(1) & 0xFF
    #         if key != 255:
    #             break
    # view_image_regions(neg_reg_generator, window_dims, display_scale=2)

    # Create the hog object with which to compute the descriptors:
    hog = get_hog_object(window_dims)

    db_name = utils.name_from_hog_descriptor(hog)
    svm_name = strutils.safe_name_from_info_dict(
        classifier_yaml['training']['svm'], 'svm_')
    svm_save_path = 'output/car_detector_{}_{}.yaml'.format(svm_name, db_name)
    if not os.path.isfile(svm_save_path):

        reg_descriptors, hog = create_or_load_descriptors(
            classifier_yaml, hog, window_dims)

        # Create lists of samples and labels:
        descriptors = [rd.descriptor for rd in reg_descriptors]
        labels = [rd.label for rd in reg_descriptors]

        # Train the classifier:
        train_svm(svm_save_path, descriptors, labels)