def test_three_vertical(self): feature = HaarLikeFeature(FeatureType.THREE_VERTICAL, (0, 0), 24, 24, 100000, 1) left_area = ii.sum_region(self.int_img, (0, 0), (24, 8)) middle_area = ii.sum_region(self.int_img, (0, 8), (24, 16)) right_area = ii.sum_region(self.int_img, (0, 16), (24, 24)) expected = 1 if feature.threshold * feature.polarity > left_area - middle_area + right_area else 0 assert feature.get_vote(self.int_img) == expected
def test_three_horizontal(self): feature = HaarLikeFeature(FeatureType.THREE_HORIZONTAL, (0, 0), 24, 24, 100000, 1) left_area = ii.sum_region(self.int_img, (0, 0), (8, 24)) middle_area = ii.sum_region(self.int_img, (8, 0), (16, 24)) right_area = ii.sum_region(self.int_img, (16, 0), (24, 24)) expected = 1 if feature.threshold * feature.polarity > left_area - middle_area + right_area else 0 assert feature.get_vote(self.int_img) == expected
def test_four(self): feature = HaarLikeFeature(FeatureType.THREE_HORIZONTAL, (0, 0), 24, 24, 100000, 1) top_left_area = ii.sum_region(self.int_img, (0, 0), (12, 12)) top_right_area = ii.sum_region(self.int_img, (12, 0), (24, 12)) bottom_left_area = ii.sum_region(self.int_img, (0, 12), (12, 24)) bottom_right_area = ii.sum_region(self.int_img, (12, 12), (24, 24)) expected = 1 if feature.threshold * feature.polarity > top_left_area - top_right_area - bottom_left_area + bottom_right_area else 0 assert feature.get_vote(self.int_img) == expected
def detect(self, frame): ''' Given grayscale-frame return [bounding-box], where bounding-box is ((x0, y0), (x1, y1)) ''' iimage = IImg.get_integral_image(frame) frameWidth = frame.shape[1] frameHeight = frame.shape[0] rects = utils.detect_faces_non_max_supp(iimage, frameWidth, frameHeight, self.classifiers_stages) rects = utils.non_max_supp(rects) return rects
def get_score(self, int_img): """ Get score for given integral image array. :param int_img: Integral image array :type int_img: numpy.ndarray :return: Score for given feature :rtype: float """ score = 0 if self.type == FeatureType.TWO_VERTICAL: first = ii.sum_region(int_img, self.top_left, (self.top_left[0] + self.width, int(self.top_left[1] + self.height / 2))) second = ii.sum_region( int_img, (self.top_left[0], int(self.top_left[1] + self.height / 2)), self.bottom_right) score = first - second elif self.type == FeatureType.TWO_HORIZONTAL: first = ii.sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 2), self.top_left[1] + self.height)) second = ii.sum_region( int_img, (int(self.top_left[0] + self.width / 2), self.top_left[1]), self.bottom_right) score = first - second elif self.type == FeatureType.THREE_HORIZONTAL: first = ii.sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 3), self.top_left[1] + self.height)) second = ii.sum_region( int_img, (int(self.top_left[0] + self.width / 3), self.top_left[1]), (int(self.top_left[0] + 2 * self.width / 3), self.top_left[1] + self.height)) third = ii.sum_region( int_img, (int(self.top_left[0] + 2 * self.width / 3), self.top_left[1]), self.bottom_right) score = first - second + third elif self.type == FeatureType.THREE_VERTICAL: first = ii.sum_region(int_img, self.top_left, (self.bottom_right[0], int(self.top_left[1] + self.height / 3))) second = ii.sum_region( int_img, (self.top_left[0], int(self.top_left[1] + self.height / 3)), (self.bottom_right[0], int(self.top_left[1] + 2 * self.height / 3))) third = ii.sum_region( int_img, (self.top_left[0], int(self.top_left[1] + 2 * self.height / 3)), self.bottom_right) score = first - second + third elif self.type == FeatureType.FOUR: # top left area first = ii.sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 2), int(self.top_left[1] + self.height / 2))) # top right area second = ii.sum_region( int_img, (int(self.top_left[0] + self.width / 2), self.top_left[1]), (self.bottom_right[0], int(self.top_left[1] + self.height / 2))) # bottom left area third = ii.sum_region( int_img, (self.top_left[0], int(self.top_left[1] + self.height / 2)), (int(self.top_left[0] + self.width / 2), self.bottom_right[1])) # bottom right area fourth = ii.sum_region(int_img, (int(self.top_left[0] + self.width / 2), int(self.top_left[1] + self.height / 2)), self.bottom_right) score = first - second - third + fourth return score
def test_two_vertical_fail(self): feature = HaarLikeFeature(FeatureType.TWO_VERTICAL, (0, 0), 24, 24, 100000, 1) left_area = ii.sum_region(self.int_img, (0, 0), (24, 12)) right_area = ii.sum_region(self.int_img, (0, 12), (24, 24)) expected = 1 if feature.threshold * -1 > left_area - right_area else 0 assert feature.get_vote(self.int_img) != expected
def setUp(self): img_arr = np.array(Image.open('../trainingdata/faces/faces0001.png'), dtype=np.float64) self.int_img = ii.to_integral_image(img_arr)
def test_area_sum(self): assert ii.sum_region(self.int_img, (0, 0), (1, 1)) == self.orig_img[0, 0] assert ii.sum_region(self.int_img, (0, 0), (-1, -1)) == np.sum(self.orig_img)
str((float(correctFacesCount) / len(facesTesting)) * 100) + '%)\nNon Faces: ' + str(correctNonFacesCount) + '/' + str(len(nonFacesTesting)) + ' (' + str((float(correctNonFacesCount) / len(nonFacesTesting)) * 100) + '%)') img = cv2.imread('./ViolaJones/test1.jpg') # frame = cv2.resize(img, (360, 360), interpolation=cv2.INTER_AREA) frame = np.copy(img) frameGrayScale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # frameGrayScale = frameGrayScale.astype('uint8') print("max: ", np.max(frameGrayScale)) print("min: ", np.min(frameGrayScale)) # mean = frameGrayScale.mean() # std = frameGrayScale.std() # normalized_img = (frameGrayScale - mean)/std iimage = IImg.get_integral_image(frameGrayScale) frameWidth = frameGrayScale.shape[1] frameHeight = frameGrayScale.shape[0] rects = utils.detect_faces_non_max_supp(iimage, frameWidth, frameHeight, classifiers_stages) modifiedImage = np.copy(frame) print("rects before non max suppression: ", len(rects)) new_rects = utils.non_max_supp(rects) print("rects after non max suppression: ", len(new_rects)) for rect in new_rects: modifiedImage = cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2) cv2.imshow("Test", modifiedImage) cv2.waitKey(0) cv2.destroyAllWindows()
def compute_feature(box, featureChosen, frameGrayScale, integralImg): # scaling features boxSize = box[0] # area sideLength = int(np.sqrt(boxSize)) areaPos_i = box[1] areaPos_j = box[2] # @ TODO the calFeatures file # featureChosen = features[featureIndex] # features should be from the calFeatures file sampleSize = 24 scale = sideLength / sampleSize # scaling the i and j of the feature i = int(scale * featureChosen.topLeft[1] + 0.5) # topLeft[1] rows j = int(scale * featureChosen.topLeft[0] + 0.5) # topLeft[0] cols # abs_i and abs_j will be used to calculate the integral image result # indicate the feature position inside the real frame abs_i = areaPos_i + i abs_j = areaPos_j + j width = featureChosen.width height = featureChosen.height originArea = width * height # scaling the width and the height of the feature width = int(scale * width + 0.5) height = int(scale * height + 0.5) # scaling the feature pattern one i.e. 1x2 feature if (featureChosen.featureType == FeatureType.TWO_HORIZONTAL): ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' height = height if height < (sideLength - i) else (sideLength - i) width = width if width % 2 == 0 else (width + 1) ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' # we should make sure that width is divisible by 2 after scaling while (width > 2 and width > sideLength - j): width -= 2 # scaling the feature pattern two i.e. 1x3 feature elif (featureChosen.featureType == FeatureType.THREE_HORIZONTAL): ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' height = height if height < (sideLength - i) else (sideLength - i) # we should make sure that width is divisible by 3 after scaling width = width if width % 3 == 0 else ((width + 2 if width % 3 == 1 else width + 1)) ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' while (width > 3 and width > sideLength - j): width -= 3 # scaling the feature pattern one i.e. 2x1 feature elif (featureChosen.featureType == FeatureType.TWO_VERTICAL): ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' width = width if width < (sideLength - j) else (sideLength - j) '''p the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' # we should make sure that height is divisible by 2 after scaling height = height if height % 2 == 0 else height + 1 # we should make sure that height is divisible by 2 after scaling while (height > 2 and height > sideLength - i): height -= 2 # scaling the feature pattern one i.e. 3x1 feature elif (featureChosen.featureType == FeatureType.THREE_VERTICAL): ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' width = width if (width < (sideLength - j)) else (sideLength - j) ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' # we should make sure that height is divisible by 3 after scaling height = height if height % 3 == 0 else ((height + 2 if height % 3 == 1 else height + 1)) while (height > 3 and height > sideLength - i): height -= 3 # scaling the feature pattern one i.e. 2x2 feature else: ''' the width of the feature may exceeds the box's size - j as boxSize - j is the maximum side the feature's width can hold ''' width = width if width % 2 == 0 else width + 1 # we should make sure that width is divisible by 2 after scaling while (width > 2 and width > sideLength - j): width -= 2 ''' the height of the feature may exceeds the box's size - i as boxSize - i is the maximum side the feature's height can hold ''' height = height if height % 2 == 0 else height + 1 # we should make sure that height is divisible by 2 after scaling while (height > 2 and height > sideLength - i): height -= 2 new_feature = Feature(featureChosen.featureType, (abs_j, abs_i), int(width), int(height), featureChosen.weight, featureChosen.threshold, featureChosen.polarity) # print(new_feature) score = new_feature.get_score(integralImg) # rescale the feature to its original scale # multiply the originArea by 2 if (featureChosen.featureType == FeatureType.THREE_HORIZONTAL or featureChosen.featureType == FeatureType.THREE_VERTICAL): mean = IImg.sum_region( integralImg, (areaPos_j, areaPos_i), (areaPos_j + sideLength, areaPos_i + sideLength)) / boxSize score -= mean * width * height / 3 reScale = originArea / (width * height) featureResult = score * reScale return featureResult
def get_score(self, int_img): """ Get score for given integral image array. :param int_img: Integral image array :type int_img: numpy.ndarray :return: Score for given feature :rtype: float """ score = 0 if self.type == FeatureType.TWO_VERTICAL: first = ii.sum_region(int_img, self.top_left, (self.top_left[0] + self.width, int(self.top_left[1] + self.height / 2))) second = ii.sum_region(int_img, (self.top_left[0], int(self.top_left[1] + self.height / 2)), self.bottom_right) score = first - second elif self.type == FeatureType.TWO_HORIZONTAL: first = ii.sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 2), self.top_left[1] + self.height)) second = ii.sum_region(int_img, (int(self.top_left[0] + self.width / 2), self.top_left[1]), self.bottom_right) score = first - second elif self.type == FeatureType.THREE_HORIZONTAL: first = ii.sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 3), self.top_left[1] + self.height)) second = ii.sum_region(int_img, (int(self.top_left[0] + self.width / 3), self.top_left[1]), (int(self.top_left[0] + 2 * self.width / 3), self.top_left[1] + self.height)) third = ii.sum_region(int_img, (int(self.top_left[0] + 2 * self.width / 3), self.top_left[1]), self.bottom_right) score = first - second + third elif self.type == FeatureType.THREE_VERTICAL: first = ii.sum_region(int_img, self.top_left, (self.bottom_right[0], int(self.top_left[1] + self.height / 3))) second = ii.sum_region(int_img, (self.top_left[0], int(self.top_left[1] + self.height / 3)), (self.bottom_right[0], int(self.top_left[1] + 2 * self.height / 3))) third = ii.sum_region(int_img, (self.top_left[0], int(self.top_left[1] + 2 * self.height / 3)), self.bottom_right) score = first - second + third elif self.type == FeatureType.FOUR: # top left area first = ii.sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 2), int(self.top_left[1] + self.height / 2))) # top right area second = ii.sum_region(int_img, (int(self.top_left[0] + self.width / 2), self.top_left[1]), (self.bottom_right[0], int(self.top_left[1] + self.height / 2))) # bottom left area third = ii.sum_region(int_img, (self.top_left[0], int(self.top_left[1] + self.height / 2)), (int(self.top_left[0] + self.width / 2), self.bottom_right[1])) # bottom right area fourth = ii.sum_region(int_img, (int(self.top_left[0] + self.width / 2), int(self.top_left[1] + self.height / 2)), self.bottom_right) score = first - second - third + fourth return score
def get_score(self, integralImg): score = 0 if self.featureType == FeatureType.TWO_VERTICAL: # top part of the feature topPart = IImg.sum_region(integralImg, self.topLeft, (self.topLeft[0] + self.width, int(self.topLeft[1] + self.height / 2))) # bottom part of the feature bottomPart = IImg.sum_region( integralImg, (self.topLeft[0], int(self.topLeft[1] + self.height / 2)), self.bottomRight) score = topPart - bottomPart elif self.featureType == FeatureType.TWO_HORIZONTAL: # left part of the feature leftPart = IImg.sum_region(integralImg, self.topLeft, (int(self.topLeft[0] + self.width / 2), self.topLeft[1] + self.height)) # bottom part of the feature rightPart = IImg.sum_region( integralImg, (int(self.topLeft[0] + self.width / 2), self.topLeft[1]), self.bottomRight) score = leftPart - rightPart elif self.featureType == FeatureType.THREE_HORIZONTAL: # left part of the feature leftPart = IImg.sum_region(integralImg, self.topLeft, (int(self.topLeft[0] + self.width / 3), self.topLeft[1] + self.height)) # middle part of the feature middlePart = IImg.sum_region( integralImg, (int(self.topLeft[0] + self.width / 3), self.topLeft[1]), (int(self.topLeft[0] + 2 * self.width / 3), self.topLeft[1] + self.height)) # right part of the feature rightPart = IImg.sum_region( integralImg, (int(self.topLeft[0] + 2 * self.width / 3), self.topLeft[1]), self.bottomRight) score = leftPart - middlePart + rightPart elif self.featureType == FeatureType.THREE_VERTICAL: # top part of the feature topPart = IImg.sum_region( integralImg, self.topLeft, (self.bottomRight[0], int(self.topLeft[1] + self.height / 3))) # middle part of the feature middlePart = IImg.sum_region( integralImg, (self.topLeft[0], int(self.topLeft[1] + self.height / 3)), (self.bottomRight[0], int(self.topLeft[1] + 2 * self.height / 3))) # bottom part of the feature bottomPart = IImg.sum_region( integralImg, (self.topLeft[0], int(self.topLeft[1] + 2 * self.height / 3)), self.bottomRight) score = topPart - middlePart + bottomPart elif self.featureType == FeatureType.FOUR: # top left part of the feature topLeftPart = IImg.sum_region( integralImg, self.topLeft, (int(self.topLeft[0] + self.width / 2), int(self.topLeft[1] + self.height / 2))) # top right part of the feature topRightPart = IImg.sum_region( integralImg, (int(self.topLeft[0] + self.width / 2), self.topLeft[1]), (self.bottomRight[0], int(self.topLeft[1] + self.height / 2))) # bottom left part of the feature bottomLeftPart = IImg.sum_region( integralImg, (self.topLeft[0], int(self.topLeft[1] + self.height / 2)), (int(self.topLeft[0] + self.width / 2), self.bottomRight[1])) # bottom right part of the feature bottomRightPart = IImg.sum_region( integralImg, (int(self.topLeft[0] + self.width / 2), int(self.topLeft[1] + self.height / 2)), self.bottomRight) score = topLeftPart - topRightPart - bottomLeftPart + bottomRightPart return score