예제 #1
0
    def detect(self, img, bndbox, initShape, affineT):
        # Extract features
        fea = self.extractFea(img, bndbox,
                              initShape, affineT)
        pntNum = initShape.shape[0]
        # Get the residules
        for i in range(pntNum):
            regX = self.regs[2 * i]
            regY = self.regs[2 * i + 1]

            x = regX.predict(fea)
            y = regY.predict(fea)
            delta = np.squeeze(np.dstack((x, y)))
            delta = Affine.transPntForward(delta, affineT)
            delta = np.multiply(delta, (bndbox[2], bndbox[3]))
            initShape[i, :] = initShape[i, :] + delta
예제 #2
0
    def genBinaryFea(self, imgData, bndBox, affineT, point):
        tree = self.tree
        imgH, imgW = imgData.shape
        w, h = bndBox[2:4]
        point_a = np.zeros(2, dtype=point.dtype)
        point_b = np.zeros(2, dtype=point.dtype)
        while 'leafIdx' not in tree:
            feaType = tree["feaType"]
            feaRange = tree["feaRange"]
            th = tree["threshold"]

            angle_cos = np.cos(feaType[[1, 3]])
            angle_sin = np.sin(feaType[[1, 3]])
            ms_x_ratio = angle_cos * feaType[[0, 2]]
            ms_y_ratio = angle_sin * feaType[[0, 2]]

            point_a[0] = ms_x_ratio[0] * w
            point_a[1] = ms_y_ratio[0] * h
            point_b[0] = ms_x_ratio[1] * w
            point_b[1] = ms_y_ratio[1] * h

            # convert meanshape coord into real coord
            point_a = Affine.transPntForward(point_a, affineT)
            point_b = Affine.transPntForward(point_b, affineT)
            point_a = point_a + point
            point_b = point_b + point

            # TODO use other interpolations
            point_a = np.around(point_a)
            point_b = np.around(point_b)

            # Check with the image size
            point_a[point_a < 0] = 0
            point_b[point_b < 0] = 0
            if point_a[0] > imgW - 1:
                point_a[0] = imgW - 1
            if point_a[1] > imgH - 1:
                point_a[1] = imgH - 1
            if point_b[0] > imgW - 1:
                point_b[0] = imgW - 1
            if point_b[1] > imgH - 1:
                point_b[1] = imgH - 1

            # print('point_a[1]:{}\n point_a[0]:{}\n point_b[1]:{}\n point_b[0]:{}\n'.format(point_a[1],
            #                                                                                point_a[0],
            #                                                                                point_b[1],
            #                                                                                point_b[0]))
            # print('int(point_a[1]):{}\n int(point_a[0]):{}\n int(point_b[1]):{}\n int(point_b[0]):{}\n'.format(int(point_a[1]),
            #                                                                                                    int(point_a[0]),
            #                                                                                                    int(point_b[1]),
            #                                                                                                    int(point_b[0])))
            # print('point_b dtype: {}'.format(point_b.dtype))
            # Construct the idx list for get the elements
            fea = np.subtract(imgData[int(point_a[1]),
                                      int(point_a[0])],
                              imgData[int(point_b[1]),
                                      int(point_b[0])],
                              dtype=np.float32)

            # get the diff
            fea = (fea - feaRange[0]) / feaRange[2]
            if fea <= th:
                tree = tree["left"]
            else:
                tree = tree["right"]

        leafIdx = tree["leafIdx"]
        return leafIdx, self.leafNum