예제 #1
0
    def test_write_load_json_file(self):

        # prepare toy samples
        self.haar_list.append(
            haar(featureType.TWO_VERTICAL, (0, 0), 19, 19, 100000, 1))
        self.haar_list.append(
            haar(featureType.THREE_VERTICAL, (0, 0), 19, 19, 100000, 1, 1.5))
        self.haar_list.append(
            haar(featureType.FOUR, (0, 0), 19, 19, 100000, 1, .05))

        # write toy samples to a json file
        write_json_file(self.haar_list)
        # load toy sample from a json file
        self.classifiers = load_json_file()

        for c in self.haar_list:
            print(
                "type: %s\tfeature type: %s\tposition: %s\twidth: %d\theight: %d\tthreshold: %d\tparity: %d\tweight: %f"
                % (type(c), c.type.name, str(c.top_left), c.width, c.height,
                   c.threshold, c.parity, c.weight))

        for c in self.classifiers:
            print(
                "type: %s\tfeature type: %s\tposition: %s\twidth: %d\theight: %d\tthreshold: %d\tparity: %d\tweight: %f"
                % (type(c), c.type, str(c.top_left), c.width, c.height,
                   c.threshold, c.parity, c.weight))

        assert len(self.haar_list) == len(self.classifiers)
        for i in range(len(self.haar_list)):
            assert two_haar_equal(self.haar_list[i], self.classifiers[i])
예제 #2
0
def _create_features(img_width, img_height, min_feat_width, max_feat_wid, min_feat_height, max_feat_height):
    # private function to create all possible features
    # return haar_feats: a list of haar-like features
    # return type: np.array(haar.HaarLikeFeature)

    haar_feats = list()

    # iterate according to types of rectangle features
    for feat_type in featureType:

        # min of feature width is set in featureType enum
        feat_start_width = max(min_feat_width, feat_type.value[0])

        # iterate with step 
        for feat_width in range(feat_start_width, max_feat_wid, feat_type.value[0]):

            # min of feature height is set in featureType enum
            feat_start_height = max(min_feat_height, feat_type.value[1])
            
            # iterate with setp
            for feat_height in range(feat_start_height, max_feat_height, feat_type.value[1]):

                # scan the whole image with sliding windows (both vertical & horizontal)
                for i in range(img_width - feat_width):
                    for j in range(img_height - feat_height):
                        haar_feats.append(haar(feat_type, (i,j), feat_width, feat_height)) 
                        # haar_feats.append(haar(feat_type, (i,j), feat_width, feat_height, 0, -1)) # threshold = 0 (no misclassified images)

    return haar_feats
예제 #3
0
    def test_two_horizontal(self):
        # check the Two-rectangle(horizontal) features
        # included fail test in which parity == -1, indicating misclassification
        feature = haar(featureType.TWO_HORIZONTAL, (0, 0), 24, 24, 100000, 1)
        white = get_sum(self.int_img, (0, 0), (24, 12))
        grey = get_sum(self.int_img, (0, 12), (24, 24))
        expected = 1 if feature.threshold * feature.parity > white - grey else 0
        expected_fail = 1 if feature.threshold * -1 > white - grey else 0

        assert feature.get_vote(self.int_img) == expected
        assert feature.get_vote(self.int_img) != expected_fail
예제 #4
0
    def test_three_vertical(self):
        # check the Three-rectangle(vertical) features
        # included fail test in which parity == -1, indicating misclassification
        feature = haar(featureType.THREE_VERTICAL, (0, 0), 24, 24, 100000, 1)
        white = get_sum(self.int_img, (0, 0), (8, 24))
        grey = get_sum(self.int_img, (0, 8), (24, 16))
        white += get_sum(self.int_img, (0, 16), (24, 24))
        expected = 1 if feature.threshold * feature.parity > white - grey else 0
        expected_fail = 1 if feature.threshold * -1 > white - grey else 0

        assert feature.get_vote(self.int_img) == expected
        assert feature.get_vote(self.int_img) != expected_fail
예제 #5
0
    def test_four(self):
        # check the Four-rectangle features
        # included fail test in which parity == -1, indicating misclassification
        feature = haar(featureType.FOUR, (0, 0), 24, 24, 100000, 1)
        white = get_sum(self.int_img, (0, 0), (12, 12))
        grey = get_sum(self.int_img, (12, 0), (24, 12))
        grey += get_sum(self.int_img, (0, 12), (12, 24))
        white += get_sum(self.int_img, (12, 12), (24, 24))
        expected = 1 if feature.threshold * feature.parity > white - grey else 0
        expected_fail = 1 if feature.threshold * -1 > white - grey else 0

        assert feature.get_vote(self.int_img) == expected
        assert feature.get_vote(self.int_img) != expected_fail
예제 #6
0
def load_json_file():
    # load the haar-like classifiers from a json file
    # return: list of classifiers/features
    # rtype: list[haar.HaarLikeFeature]
    classifiers = list()
    with open(path_json_file, 'r') as fp:
        data_list = json.load(fp)
        for i in range(len(data_list)):
            data = data_list[str(i)]
            classifiers.append(
                haar(data["feature type"], data["position"], data["width"],
                     data["height"], data["threshold"], data["parity"],
                     data["weight"]))
    return classifiers
예제 #7
0
파일: utils.py 프로젝트: danmenloz/AdaBoost
def load_features(filename):
    # Read data
    read_features = list()
    with open(path_save_features + filename, newline='') as features:
        features_reader = csv.DictReader(features, delimiter='\t')
        for feature in features_reader:
            read_features.append(feature)

    created_features = [
        haar(featureType(eval(f['type'])),
             eval(f['position']), eval(f['width']), eval(f['height']),
             eval(f['error']), eval(f['threshold']), eval(f['polarity']),
             eval(f['weight'])) for f in read_features
    ]

    return created_features