def __getitem__(self, index):
        img = self.LoadImage(index)
        pts, c, s = self.GetPartInfo(index)

        pts_s, _, _ = self.GetPartInfo(index)

        r = 0

        if self.split == 'train':
            # s = s * (2 ** I.Rnd(self.maxScale))
            s = s * 1
            r = 0
        inp = I.Crop(img, c, s, r, self.inputRes) / 255.
        out = np.zeros((self.nJoints, self.outputRes, self.outputRes))

        # for 256 x 256 heatmaps
        #------------------------------------------------------------------------------
        out_s = np.zeros((self.nJoints, self.outputRes_s, self.outputRes_s))
        #------------------------------------------------------------------------------
        for i in range(self.nJoints):
            if pts[i][0] > 1:
                pts[i] = I.Transform(pts[i], c, s, r, self.outputRes)
                out[i] = I.DrawGaussian(out[i], pts[i], self.hmGauss,
                                        0.5 if self.outputRes == 32 else -1)

# for 256 x 256 heatmaps
#-----------------------------------------------------------------------------------------------------------
        for i in range(self.nJoints):
            if pts_s[i][0] > 1:
                pts_s[i] = I.Transform(pts_s[i], c, s, r, self.outputRes_s)
                out_s[i] = I.DrawGaussian(out_s[i], pts_s[i], self.hmGauss_s,
                                          0.5 if self.outputRes == 32 else -1)
#-----------------------------------------------------------------------------------------------------------

        if self.split == 'train':
            if np.random.random() < 0.5:
                inp = I.Flip(inp)
                out = I.ShuffleLR(I.Flip(out))
                pts[:, 0] = self.outputRes - pts[:, 0]

                # for 256 x 256 heatmaps
                #--------------------------------------------------------------------------------------------------------------
                out_s = I.ShuffleLR(I.Flip(out_s))
                pts_s[:, 0] = self.outputRes_s - pts_s[:, 0]


#--------------------------------------------------------------------------------------------------------------

            inp[0] = np.clip(inp[0] * (np.random.random() * (0.4) + 0.6), 0, 1)
            inp[1] = np.clip(inp[1] * (np.random.random() * (0.4) + 0.6), 0, 1)
            inp[2] = np.clip(inp[2] * (np.random.random() * (0.4) + 0.6), 0, 1)

            #out_s = np.resize(out_s , (256, 256,3))
            #print(out_s.shape())
        return {
            'image': torch.Tensor(inp),
            'heatmaps': torch.Tensor(out * 1.7),
            'occlusions': torch.Tensor(out_s * 1.7),
        }
Example #2
0
    def __getitem__(self, index):
        img = self.LoadImage(index)
        pts, c, s = self.GetPartInfo(index)
        r = 0

        if self.split == 'train':
            s = s * (2**I.Rnd(self.opts.maxScale))
            r = 0 if np.random.random() < 0.6 else I.Rnd(self.opts.maxRotate)

        inp = I.Crop(img, c, s, r, self.opts.inputRes) / 256.

        out = np.zeros(
            (self.opts.nJoints, self.opts.outputRes, self.opts.outputRes))

        for i in range(self.opts.nJoints):
            if pts[i][0] > 1:
                pts[i] = I.Transform(pts[i], c, s, r, self.opts.outputRes)
                out[i] = I.DrawGaussian(
                    out[i], pts[i], self.opts.hmGauss,
                    0.5 if self.opts.outputRes == 32 else -1)

        if self.split == 'train':
            if np.random.random() < 0.5:
                inp = I.Flip(inp)
                out = I.ShuffleLR(I.Flip(out))
                pts[:, 0] = self.opts.outputRes / 4 - pts[:, 0]

            inp[0] = np.clip(inp[0] * (np.random.random() * (0.4) + 0.6), 0, 1)
            inp[1] = np.clip(inp[1] * (np.random.random() * (0.4) + 0.6), 0, 1)
            inp[2] = np.clip(inp[2] * (np.random.random() * (0.4) + 0.6), 0, 1)

        if self.opts.TargetType == 'heatmap':
            return inp, out, self.stuff1[index], self.stuff2[index]
        elif self.opts.TargetType == 'direct':
            return inp, np.reshape(
                (pts / self.opts.outputRes),
                -1) - 0.5, self.stuff1[index], self.stuff2[index]
Example #3
0
    def __getitem__(self, idx):
        # if validation, offset index
        #idx =3
        if self.mode == 'val':
            idx += int(self.train_split * len(self.files))

        file_name = self.files[idx].data
        dic = {2: "knees", 1.02: "ankles", 8: "femurs"}

        l = random.choice([2, 1.02, 8, 2])

        print("part of the body loading :", dic[l])

        c = np.array([file_name.shape[1] / 2,
                      file_name.shape[0] / l])  #2 #8 #1.02
        s = file_name.shape[0] * 0.50
        if l == 2:
            s = file_name.shape[0] * random.choice([0.50, 1, 0.50, 1])
        print("scale of  the image", file_name.shape[0])
        r = 0
        #file_name =sitk.GetImageFromArray(file_name)
        #image = cv2.imread(file_name)
        if self.mode == 'train':
            r = 0 if np.random.random() < 0.6 else I.Rnd(self.rotate)
            #s = s*(1.1**(-1*abs(I.Rnd(10))))# 1.2**** I.Rnd())

        image = cv2.cvtColor(file_name, cv2.COLOR_BGR2RGB) / 257
        #print("min and max" , image.min() , image.max())
        print(image.shape)
        original = image.copy()
        original = I.Crop(original, c, s, r, 64) / 255
        inp = I.Crop(image, c, s, r, self.inputRes) / 255
        #print("min and max" , inp.min() , inp.max())

        #print(inp)
        #out = np.zeros((self.nJoints, self.outputRes, self.outputRes))

        #file_name = sitk.GetImageFromArray(file_name)

        #print(file_name.GetSize())
        #file_name.SetSpacing([4.1 , 4.1])
        #file_name =sitk.GetArrayFromImage(file_name)
        #print(file_name.GetSpacing())

        #image = cv2.cvtColor(file_name, cv2.COLOR_BGR2RGB)
        #print(image.max())
        #image_n = (image/257.0).astype('uint8')#(image - 128.0)/ 128;
        #image   =(image/257).astype('uint8')

        #crop_image = cv2.resize(image, (self.crop_size, self.crop_size))
        #print("image shape:" , crop_image.shape)

        annot = self.files[idx]
        #print(annot)
        # Read annotations
        #for l in range(0,12):
        #with open(annot, 'r') as json_file:
        #    data = json.load(json_file)
        #print("::::::::::::::::::",data)

        annotations = {
            'ankle_left': [0.0, 0.0],
            'ankle_right': [0.0, 0.0],
            'femur_left': [0.0, 0.0],
            'femur_right': [0.0, 0.0],
            'knee_left': [0.0, 0.0],
            'knee_right': [0.0, 0.0]
        }

        #print(str(idx[1][0]))
        #for i in range(0, 12):
        for name, obj in annot.parts.items():  #[idx[i][0]]['position']:
            #for key in annotations:
            #print(name)
            #print("value of p:", p)
            #print("value of j:", j)
            #annotations[i].append(p[1]['position'])

            if name == 'ankle_left':
                annotations['ankle_left'] = (obj['position'])
            elif name == 'ankle_right':
                annotations['ankle_right'] = (obj['position'])
            elif name == 'femur_left':
                annotations['femur_left'] = (obj['position'])
            elif name == 'femur_right':
                annotations['femur_right'] = (obj['position'])
            elif name == 'knee_left':
                annotations['knee_left'] = (obj['position'])
            elif name == 'knee_right':
                annotations['knee_right'] = (obj['position'])

        #print("Annotations file :",annotations)
        annotations_l = []
        for key, value in annotations.items():

            annotations_l.append(value)
        pts = annotations_l
        #pts_large = annotations_l.copy()
        #print(annotations_l)
        out = np.zeros((6, self.outputRes, self.outputRes))
        out_large = np.zeros((6, 256, 256))

        #for i in range(6):
        #    if annotations_l[i][0]>1:
        #        pts[i] = I.Transform(annotations_l[i], c, s, r, self.outputRes)
        #        out[i] = I.DrawGaussian(out[i], annotations_l[i], 2, 0.5 if self.outputRes==32 else -1)
        #print("annotations :" , pts)
        #-----------------------------------------------------------------------------------------------------------------
        #x = range(256)
        m = range(64)
        #xx, yy = np.meshgrid(x, x)
        mm, nn = np.meshgrid(m, m)
        #print(len(annotations))
        #heatmaps = np.zeros((6, 256, 256)) #len(annotations)
        #occlusion = np.zeros((6, 64,64))

        for joint_id in range(len(annotations_l)):
            if pts[joint_id][0] > 0 and pts[joint_id][1] > 0:
                x_cc, y_cc = annotations_l[joint_id]

                #------------------------------------------------------------------------
                #x_c = x_cc * (256 / image.shape[1])
                #y_c =   y_cc * (256 / image.shape[0])
                #print("vis :",x_c, y_c)
                #------------------------------------------------------------------------

                pts[joint_id] = I.Transform(np.array([x_cc, y_cc]), c, s, r,
                                            self.outputRes)
                #pts_large[joint_id] = I.Transform(np.array([x_cc, y_cc]), c, s, r, 256)

                #print(pts[joint_id])
                #m_c , n_c = pts
                out[joint_id] = I.DrawGaussian(
                    out[joint_id], pts[joint_id], 1,
                    0.5 if self.outputRes == 32 else -1)

                #out_large[joint_id] =I.DrawGaussian(out_large[joint_id], pts_large[joint_id], 1, 0.5 if self.outputRes==32 else -1)

                #------------------------------------------------------------------------
                #np.exp(-5*((m_c - mm)**2 + (n_c - nn)**2)/(2**2))
                #I.DrawGaussian(out[i], pts[i], 1, 0.5 if self.outputRes==32 else -1)
                #------------------------------------------------------------------------

        #if self.mode == 'None':
        #    if np.random.random() < 0.5:
        #            inp = I.Flip(inp)
        #            out = I.ShuffleLR_LEX(I.Flip(out))
        if self.mode == 'train':

            inp[0] = np.clip(inp[0] * (np.random.random() * (0.4) + 0.6), 0, 1)
            inp[1] = np.clip(inp[1] * (np.random.random() * (0.4) + 0.6), 0, 1)
            inp[2] = np.clip(inp[2] * (np.random.random() * (0.4) + 0.6), 0, 1)

        #----Adding random noise to the images --------#
        #if self.mode == 'None':
        #        inp = random_noise(inp , 'salt' , amount = 0.0001)

        #-----------------------------------------------------------------------------------------------------------------
        #    heatmaps[joint_id] = np.exp(-5*((x_c - xx)**2 + (y_c - yy)**2)/(self.heatmap_sigma**2))

        #    m_cc, n_cc= annotations_l[joint_id]
        #    m_c = m_cc * (64*1/image.shape[1])
        #    n_c =   n_cc * (64*1/image.shape[0])
        #    occlusion[joint_id] = np.exp(-5*((m_c - mm)**2 + (n_c - nn)**2)/(self.occlusion_sigma**2))
        #0-----------------------------------------------------------------------------------------------------------------

        return {
            # image is in CHW format
            'image': torch.Tensor(inp.astype(np.float32)),  #,/255.,
            #'kp_2d': torch.Tensor(annotations),
            'heatmaps': torch.Tensor(out * 2),
            'occlusions': torch.Tensor(out * 2),
            'small_image':
            torch.Tensor(original.astype(np.float32))  #.transpose(2 , 0 , 1)
            # TODO: Return heatmaps
        }
Example #4
0
    def __getitem__(self, idx):
        # if validation, offset index
        #idx =3
        if self.mode == 'val':
            idx += int(self.train_split * len(self.files))

        file_name = self.files[idx].data
        #-------------------------------------------------------------------------------------------------------------
        c = np.array([128, 128])
        s = 256  #(randint(2, 3))*150
        s1 = 64
        r = 0
        random = 1**I.Rnd(2)
        print("random :", random)
        s = s * random
        s1 = s1 * random
        r = 0 if np.random.random() < 0.6 else I.Rnd(self.rotate)

        #-------------------------------------------------------------------------------------------------------------

        image = cv2.cvtColor(file_name, cv2.COLOR_BGR2RGB)
        #print("spacing : ", np.spacing(image))

        image_n = (image / 257).astype('uint8')  #(image - 128.0)/ 128;
        #image   =image_n/ 255

        crop_image = cv2.resize(image_n, (self.crop_size, self.crop_size))
        #---------------------------------------------------------------------
        crop_image = I.Crop(crop_image, c, s, r, self.crop_size) / 255
        #---------------------------------------------------------------------

        #print("image shape:" , crop_image.shape)

        annot = self.files[idx]
        #print(annot)
        # Read annotations
        #for l in range(0,12):
        #with open(annot, 'r') as json_file:
        #    data = json.load(json_file)
        #print("::::::::::::::::::",data)

        annotations = {
            'ankle_left': [0.0, 0.0],
            'ankle_right': [0.0, 0.0],
            'femur_left': [0.0, 0.0],
            'femur_right': [0.0, 0.0],
            'knee_left': [0.0, 0.0],
            'knee_right': [0.0, 0.0]
        }
        dummy = [0.0, 0.0]
        #print(str(idx[1][0]))
        #for i in range(0, 12):
        for name, obj in annot.parts.items():  #[idx[i][0]]['position']:
            #for key in annotations:
            #print(key)
            #print("value of p:", p)
            #print("value of j:", j)
            #annotations[i].append(p[1]['position'])
            print(name)
            if name == 'ankle_left':
                annotations['ankle_left'] = (obj['position'])
            elif name == 'ankle_right':
                annotations['ankle_right'] = (obj['position'])
            elif name == 'femur_left':
                annotations['femur_left'] = (obj['position'])
            elif name == 'femur_right':
                annotations['femur_right'] = (obj['position'])
            elif name == 'knee_left':
                annotations['knee_left'] = (obj['position'])
            elif name == 'knee_right':
                annotations['knee_right'] = (obj['position'])

        print("Annotations file :", annotations)
        annotations_l = []
        for key, value in annotations.items():

            annotations_l.append(value)

        print("Annotations file :", annotations_l)

        x = range(256)
        m = range(64)
        xx, yy = np.meshgrid(x, x)
        mm, nn = np.meshgrid(m, m)
        #print(len(annotations))
        heatmaps = np.zeros((6, 256, 256))  #len(annotations)
        occlusion = np.zeros((6, 64, 64))

        for joint_id in range(len(annotations_l)):
            x_cc, y_cc = annotations_l[joint_id]

            x_c = x_cc * (256 * 1 / image.shape[1])
            y_c = y_cc * (256 * 1 / image.shape[0])

            annot_pt = I.Transform(np.array([x_c, y_c]), c, s, r, 256)

            x_c, y_c = annot_pt
            #print("vis :",x_c, y_c)
            heatmaps[joint_id] = np.exp(-5 * ((x_c - xx)**2 + (y_c - yy)**2) /
                                        (self.heatmap_sigma**2))

        for joint_id in range(len(annotations_l)):

            m_cc, n_cc = annotations_l[joint_id]

            m_c = m_cc * (64 * 1 / image.shape[1])
            n_c = n_cc * (64 * 1 / image.shape[0])
            print(m_c, n_c)

            annot_o_pt = I.Transform(np.array([m_c, n_c]), np.array([32, 32]),
                                     s1, r, 64)

            m_c, n_c = annot_o_pt
            print(m_c, n_c)
            occlusion[joint_id] = np.exp(-5 * ((m_c - mm)**2 + (n_c - nn)**2) /
                                         (self.occlusion_sigma**2))

        if np.random.random() < 0.5:
            crop_image = I.Flip(crop_image)
            occlusion = I.ShuffleLR_LEX(I.Flip(occlusion))

        return {
            # image is in CHW format
            'image': torch.Tensor(crop_image),  #.transpose(2, 0, 1)),#,/255.,
            #'kp_2d': torch.Tensor(annotations),
            'heatmaps': torch.Tensor(occlusion * 1.7),
            'occlusions': torch.Tensor(occlusion * 1.7),
            # TODO: Return heatmaps
        }