예제 #1
0
    def calResiduals(self):
        ### Compute the affine matrix
        self.getAffineT()

        self.residuals = np.zeros(self.gtShapes.shape)
        num = self.gtShapes.shape[0]
        for i in range(num):
            # Project to meanshape coordinary
            T = self.real2mss[i]
            bndBox = self.bndBoxs[i]
            err = self.gtShapes[i] - self.initShapes[i]
            err = np.divide(err, (bndBox[2], bndBox[3]))
            err = Affine.transPntsForwardWithSameT(err, T)
            self.residuals[i, :] = err
예제 #2
0
    def genFea(self, train_set, pointIdx, sampleIdxs, feaTypes):
        sampleNum = len(sampleIdxs)
        feaNum = feaTypes.shape[0]
        pdFea = np.zeros((sampleNum, feaNum), dtype=np.float32)

        coord_a = np.zeros((feaNum, 2))
        coord_b = np.zeros((feaNum, 2))
        angle_cos = np.cos(feaTypes[:, [1, 3]])
        angle_sin = np.sin(feaTypes[:, [1, 3]])

        ms_x_ratio = angle_cos * feaTypes[:, [0, 2]]
        ms_y_ratio = angle_sin * feaTypes[:, [0, 2]]

        augNum = train_set.augNum
        for i, idx in enumerate(sampleIdxs):
            T = train_set.ms2reals[idx]
            bndBox = train_set.bndBoxs[idx]
            imgData = train_set.imgDatas[int(idx / augNum)]
            initShape = train_set.initShapes[idx]

            # numpy image: H x W x C
            imgH, imgW = imgData.shape
            # print('imgH:{}, imgW:{}'.format(imgH, imgW))
            w, h = bndBox[2:4]
            coord_a[:, 0] = ms_x_ratio[:, 0] * w
            coord_a[:, 1] = ms_y_ratio[:, 0] * h
            coord_b[:, 0] = ms_x_ratio[:, 1] * w
            coord_b[:, 1] = ms_y_ratio[:, 1] * h

            # convert meanshape coord into real coord
            coord_a = Affine.transPntsForwardWithSameT(coord_a, T)
            coord_b = Affine.transPntsForwardWithSameT(coord_b, T)
            coord_a = coord_a + initShape[pointIdx]
            coord_b = coord_b + initShape[pointIdx]

            # TODO use other interpolations
            coord_a = np.floor(coord_a)
            coord_b = np.floor(coord_b)
            # print('coord_a shape: {}'.format(coord_a.shape))

            # Check with the image size
            coord_a[coord_a < 0] = 0

            # print('coord_a[:,0]>imgW-1: {}'.format(coord_a[:,0]>imgW-1))
            # print('coord_a[:,0]: {}'.format(coord_a[:,0]))
            coord_a[coord_a[:, 0] > imgW - 1, 0] = imgW - 1
            coord_a[coord_a[:, 1] > imgH - 1, 1] = imgH - 1
            coord_b[coord_b < 0] = 0
            coord_b[coord_b[:, 0] > imgW - 1, 0] = imgW - 1
            coord_b[coord_b[:, 1] > imgH - 1, 1] = imgH - 1

            # Construct the idx list for get the elements
            idx_a = np.transpose(coord_a).tolist()
            idx_a[0], idx_a[1] = idx_a[1], idx_a[0]
            idx_b = np.transpose(coord_b).tolist()
            # print('idx_a: {}'.format(idx_a))
            idx_b[0], idx_b[1] = idx_b[1], idx_b[0]
            idx_a = np.array(idx_a, dtype=np.int64)
            # print(('idx_aa: {}'.format(idx_a[1])))
            idx_b = np.array(idx_b, dtype=np.int64)

            ### get the diff
            # print('i: {}, ind:{}'.format(i, idx))
            # print('imgData shape', imgData.shape)
            # print('idx_a[0]: {}\n idx_a[1]: {} \n idx_b[0]:{}\n idx_b[1]: {}'.format(idx_a[0], idx_a[1], idx_b[0], idx_b[1]))
            # print('idx_a[0] shape: {}'.format(idx_a[0].shape))
            pdFea[i, :] = np.subtract(imgData[idx_a[0], idx_a[1]],
                                      imgData[idx_b[0], idx_b[1]],
                                      dtype=np.int64)
        return pdFea