Beispiel #1
0
    def __getitem__(self, index):
        img = self.LoadImage(index)
        pts2d, pts3d, emb, c, s = self.GetPartInfo(index)
        s = min(s, max(img.shape[0], img.shape[1])) * 1.0
        pts3d[:, 2] += s / 2

        r = 0
        if self.split == 'train':
            s = s * (2**Rnd(ref.scale))
            c[1] = c[1] + Rnd(ref.shiftY)
            r = 0 if np.random.random() < 0.6 else Rnd(ref.rotate)
        inp = Crop(img, c, s, r, ref.inputRes)
        inp = inp.transpose(2, 0, 1).astype(np.float32) / 256.

        starMap = np.zeros((1, ref.outputRes, ref.outputRes))
        embMap = np.zeros((3, ref.outputRes, ref.outputRes))
        depMap = np.zeros((1, ref.outputRes, ref.outputRes))
        mask = np.concatenate([
            np.ones((1, ref.outputRes, ref.outputRes)),
            np.zeros((4, ref.outputRes, ref.outputRes))
        ])

        for i in range(pts3d.shape[0]):
            if self.annot['valid'][index][i] > ref.eps:
                if (self.annot['vis'][index][i] > ref.eps):
                    pt3d = Transform3D(pts3d[i], c, s, r,
                                       ref.outputRes).astype(np.int32)
                    pt2d = Transform(pts2d[i], c, s, r,
                                     ref.outputRes).astype(np.int32)
                    if pt2d[0] >= 0 and pt2d[0] < ref.outputRes and pt2d[
                            1] >= 0 and pt2d[1] < ref.outputRes:
                        embMap[:, pt2d[1], pt2d[0]] = emb[i]
                        depMap[0, pt2d[1],
                               pt2d[0]] = 1.0 * pt3d[2] / ref.outputRes - 0.5
                        mask[1:, pt2d[1], pt2d[0]] = 1
                    starMap[0] = np.maximum(
                        starMap[0],
                        DrawGaussian(np.zeros((ref.outputRes, ref.outputRes)),
                                     pt2d, ref.hmGauss).copy())

        out = starMap
        if 'emb' in self.opt.task:
            out = np.concatenate([out, embMap])
        if 'dep' in self.opt.task:
            out = np.concatenate([out, depMap])
        mask = mask[:out.shape[0]].copy()

        if self.split == 'train':
            if np.random.random() < 0.5:
                inp = Flip(inp)
                out = Flip(out)
                mask = Flip(mask)
                if 'emb' in self.opt.task:
                    out[1] = -out[1]
        return inp, out, mask
Beispiel #2
0
    def __getitem__(self, index):
        if self.split == 'train':
            index = np.random.randint(self.nSamples)

        img = self.LoadImage(index)
        pts, c, s, pts_3d, pts_3d_mono = self.GetPartInfo(index)
        pts_3d[7] = (pts_3d[12] +
                     pts_3d[13]) / 2  # neck = average of shoulders

        inp = Crop(img, c, s, 0,
                   ref.inputRes) / 256.  # crop image to input resolution
        outMap = np.zeros((ref.nJoints, ref.outputRes,
                           ref.outputRes))  # nJoints x 64 x 64 heatmap for 2D
        outReg = np.zeros((ref.nJoints, 3))  # Regression target for 3D

        for i in range(ref.nJoints):
            pt = Transform3D(pts_3d[i], c, s, 0, ref.outputRes)
            if pts[i][0] > 1:
                outMap[i] = DrawGaussian(
                    outMap[i], pt[:2],
                    ref.hmGauss)  # Draw 2D heat map for detection

            outReg[i, 2] = pt[2] / ref.outputRes * 2 - 1
        inp = torch.from_numpy(inp)
        return inp, outMap, outReg, pts_3d_mono
Beispiel #3
0
    def getitem(self, index, meta, whichFrames, frameWiseData):
        img = cv2.imread(ref.posetrackDataDir + "/" + whichFrames[index])
        #print(ref.posetrackDataDir + whichFrames[index])
        frameData = frameWiseData[whichFrames[index]]
        bboxmean, bboxdelta, joints = frameData[meta['personIndex']]

        pts = joints
        c = np.asarray(bboxmean)
        s = bboxdelta * meta['s'] / 100.0
        r = meta['r']

        inp = Crop(img, c, s, r, ref.inputRes) / 256.
        out = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
        Reg = np.zeros((ref.nJoints, 3))
        for i in range(ref.nJoints):
            if pts[i][0] > 1:
                pt = Transform(pts[i], c, s, r, ref.outputRes)
                out[i] = DrawGaussian(out[i], pt, ref.hmGauss)
                Reg[i, :2] = pt
                Reg[i, 2] = 1
        if self.split == 'train':
            if meta['flip']:
                inp = Flip(inp)
                out = ShuffleLR(Flip(out))
                Reg[:, 1] = Reg[:, 1] * -1
                Reg = ShuffleLR(Reg)

            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)

        whatever = (np.zeros((ref.nJoints, 3)))

        return inp, out, Reg, whatever
 def __getitem__(self, index):
   img = self.LoadImage(index)
   pts, c, s = self.GetPartInfo(index)
   r = 0
   
   if self.split == 'train':
     s = s * (2 ** Rnd(ref.scale))
     r = 0 if np.random.random() < 0.6 else Rnd(ref.rotate)
   inp = Crop(img, c, s, r, ref.inputRes) / 256.
   out = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
   Reg = np.zeros((ref.nJoints, 3))
   for i in range(ref.nJoints):
     if pts[i][0] > 1:
       pt = Transform(pts[i], c, s, r, ref.outputRes)
       out[i] = DrawGaussian(out[i], pt, ref.hmGauss) 
       Reg[i, :2] = pt
       Reg[i, 2] = 1
   if self.split == 'train':
     if np.random.random() < 0.5:
       inp = Flip(inp)
       out = ShuffleLR(Flip(out))
       Reg[:, 1] = Reg[:, 1] * -1
       Reg = ShuffleLR(Reg)
     #print 'before', inp[0].max(), inp[0].mean()
     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)
     #print 'after', inp[0].max(), inp[0].mean()
     
   inp = torch.from_numpy(inp)
   if self.returnMeta:
     return inp, out, Reg, np.zeros((ref.nJoints, 3))
   else:
     return inp, out
Beispiel #5
0
    def __getitem__(self, index):
        img = self.LoadImage(index)
        pts, action, c, s = self.GetPartInfo(index)
        nb_pts = len(pts)
        r = 0

        flip = False
        if self.split == 'train':
            s = s * (2**Rnd(ref.scale))
            r = 0 if np.random.random() < 0.6 else Rnd(ref.rotate)
        inp = old_div(Crop(img, c, s, r, ref.inputRes), 256.)
        if self.split == 'train':
            if np.random.random() < 0.5:
                inp = Flip(inp)
                flip = True
            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)
            meta = np.zeros(1)
        else:
            meta = {'index': index, 'center': c, 'scale': s, 'rotate': r}

        output = []
        for k in range(nb_pts):
            out = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
            for i in range(ref.nJoints):
                if pts[k][i][0] > 1:
                    pt = Transform(pts[k][i], c, s, r, ref.outputRes)
                    out[i] = DrawGaussian(out[i], pt, ref.hmGauss)
            if self.split == 'train':
                out = ShuffleLR(Flip(out))
            output.append(out)

        return inp, output, action, meta
Beispiel #6
0
 def __getitem__(self, index):
   img = self.LoadImage(index)
   pts, c, s = self.GetPartInfo(index)
   r = 0
   
   if self.split == 'train':
     s = s * (2 ** Rnd(ref.scale))
     r = 0 if np.random.random() < 0.6 else Rnd(ref.rotate)
   inp = Crop(img, c, s, r, ref.inputRes) / 256.
   out = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
   out32 = np.zeros((ref.nJoints,32,32))
   for i in range(ref.nJoints):
     if pts[i][0] > 1:
       pt = Transform(pts[i], c, s, r, ref.outputRes)
       out[i] = DrawGaussian(out[i], pt, ref.hmGauss)
   for i in range(ref.nJoints):
     if pts[i][0] > 1:
       pt = Transform(pts[i], c, s, r, 32)
       out32[i] = DrawGaussian(out32[i], pt, ref.hmGauss)  
   if self.split == 'train':
     if np.random.random() < 0.5:
       inp = Flip(inp)
       out = ShuffleLR(Flip(out))
       out32 = ShuffleLR(Flip(out32))
     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)
     meta = np.zeros(1)
   else:
     meta = {'index' : index, 'center' : c, 'scale' : s, 'rotate': r}
   
   return inp, out, out32, meta
Beispiel #7
0
    def __getitem__(self, index):
        if self.split == 'train':
            index = np.random.randint(self.nSamples)
        img = self.LoadImage(index)
        pts, c, s, pts_3d, pts_3d_mono = self.GetPartInfo(index)
        pts_3d[7] = (pts_3d[12] + pts_3d[13]) / 2

        inp = Crop(img, c, s, 0, ref.inputRes) / 256.
        outMap = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
        outReg = np.zeros((ref.nJoints, 3))
        for i in range(ref.nJoints):
            pt = Transform3D(pts_3d[i], c, s, 0, ref.outputRes)
            if pts[i][0] > 1:
                outMap[i] = DrawGaussian(outMap[i], pt[:2], ref.hmGauss)
            outReg[i, 2] = pt[2] / ref.outputRes * 2 - 1

        t_inp = inp.transpose(1, 2, 0)
        out = cv2.resize(t_inp, dsize=(64, 64))
        out = torch.from_numpy(out.transpose(2, 0, 1))
        inp = torch.from_numpy(inp)
        return inp, out, outMap
Beispiel #8
0
  def __getitem__(self, index):
    img = self.LoadImage(index)
    class_id = self.annot['class_id'][index]
    c, s, v = self.GetPartInfo(index)
    s = min(s, max(img.shape[0], img.shape[1])) * 1.0

    r = 0
    if self.split == 'train':
      s = s * (2 ** Rnd(ref.scale))
      c[1] = c[1] + Rnd(ref.shiftY)
      r = 0 if np.random.random() < 0.6 else Rnd(ref.rotate)
      v[2] += r / 180.
      v[2] += 2 if v[2] < -1 else (-2 if v[2] > 1 else 0)
    inp = Crop(img, c, s, r, ref.inputRes)
    inp = inp.transpose(2, 0, 1).astype(np.float32) / 256.
    
    if self.split == 'train':
      if np.random.random() < 0.5:
        inp = Flip(inp)
        v[0] = - v[0]
        v[2] = - v[2]
        v[2] += 2 if v[2] <= -1 else 0
    #https://github.com/shubhtuls/ViewpointsAndKeypoints/blob/master/rcnnVp/rcnnBinnedJointTrainValTestCreate.m#L77
    vv = v.copy()
    if vv[0] < 0:
      v[0] = self.opt.numBins - 1 - np.floor(-vv[0] * self.opt.numBins / 2.)
    else:
      v[0] = np.floor(vv[0] * self.opt.numBins / 2.)
    v[1] = np.ceil(vv[1] * self.opt.numBins / 2. + self.opt.numBins / 2. - 1)
    v[2] = np.ceil(vv[2] * self.opt.numBins / 2. + self.opt.numBins / 2. - 1)
    v = v.astype(np.int32)
    if self.opt.specificView:
      vv = np.ones(3 * len(ref.pascalClassId), dtype = np.int32) * self.opt.numBins
      vv[class_id * 3: class_id * 3 + 3] = v.copy()
      v = vv.copy()

    return inp, v
Beispiel #9
0
def main():
    opt = opts().parse()
    #if opt.loadModel != 'none':
    model = AlexNet(ref.nJoints).cuda()

    model.load_state_dict(torch.load("save.model"))

    for (i, filename) in enumerate(os.listdir("./testimages/")):

        img = cv2.imread("./testimages/" + filename)
        c = np.ones(2) * ref.h36mImgSize / 2
        s = ref.h36mImgSize * 1.0
        img2 = Crop(img, c, s, 0, ref.inputRes) / 256.

        input = torch.from_numpy(img2)

        input = input.contiguous().view(1, input.size(0), input.size(1),
                                        input.size(2))

        print(input.size())

        input_var = torch.autograd.Variable(input).float().cuda()
        output = model(input_var)
        print(output.size())
        reg = (output.data).cpu().numpy()  #.reshape(pred.shape[0], 1)

        four = lambda t: t * 4.57
        fourfunc = np.vectorize(four)
        reg = fourfunc(reg)

        print(reg)

        debugger = Debugger()
        debugger.addImg(
            (input[0].numpy().transpose(1, 2, 0) * 256).astype(np.uint8))
        debugger.addPoint2D(reg, (255, 0, 0))

        #debugger.addPoint3D(np.concatenate([pred, (reg + 1) / 2. * 256], axis = 1))

        debugger.saveImg(path="./result/" + filename)

        np.set_printoptions(threshold=np.inf, linewidth=np.inf)

        with open("./result/" + filename[:-4] + ".out", 'w') as f:
            f.write(np.array2string(reg, separator=', '))
        """
Beispiel #10
0
  def __getitem__(self, index):
    if self.split == 'train':
      index = np.random.randint(self.nSamples)
    img = self.LoadImage(index)
    pts, c, s, pts_3d, pts_3d_mono = self.GetPartInfo(index)
    pts_3d[7] = (pts_3d[12] + pts_3d[13]) / 2

#    print(img.shape)

    inp = Crop(img, c, s, 0, ref.inputRes) / 256.
    outMap = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
    outReg = np.zeros((ref.nJoints, 3))
    for i in range(ref.nJoints):
      pt = Transform3D(pts_3d[i], c, s, 0, ref.outputRes)
      if pts[i][0] > 1:
        outMap[i] = DrawGaussian(outMap[i], pt[:2], ref.hmGauss)
      outReg[i,:2] = pt[:2]
      outReg[i, 2] = pt[2] / ref.outputRes * 2 - 1

    inp = torch.from_numpy(inp)
    return inp, outMap, outReg#, pts_3d_mono
    def LoadFrameAndData(self, path, frameName):
        frame = cv2.imread(path + frameName)
        pts_2d, pts_3d, pts_3d_mono = pickle.load(open(
            path + "data.pkl", 'rb'))[int(frameName[-10:-4])]

        pts_2d = pts_2d
        pts_3d = pts_3d
        pts_3d_mono = pts_3d_mono

        c = np.ones(2) * ref.h36mImgSize / 2
        s = ref.h36mImgSize * 1.0

        pts_3d = pts_3d - pts_3d[self.root]

        s2d, s3d = 0, 0
        for e in ref.edges:
            s2d += ((pts_2d[e[0]] - pts_2d[e[1]])**2).sum()**0.5
            s3d += ((pts_3d[e[0], :2] - pts_3d[e[1], :2])**2).sum()**0.5
        scale = s2d / s3d

        for j in range(ref.nJoints):
            pts_3d[j, 0] = pts_3d[j, 0] * scale + pts_2d[self.root, 0]
            pts_3d[j, 1] = pts_3d[j, 1] * scale + pts_2d[self.root, 1]
            pts_3d[j, 2] = pts_3d[j, 2] * scale + ref.h36mImgSize / 2

        pts_3d[7, :] = (pts_3d[12, :] + pts_3d[13, :]) / 2

        frame = Crop(frame, c, s, 0, ref.inputRes) / 256.

        outMap = np.zeros((ref.nJoints, ref.outputRes, ref.outputRes))
        outReg = np.zeros((ref.nJoints, 3))
        for i in range(ref.nJoints):
            pt = Transform3D(pts_3d[i], c, s, 0, ref.outputRes)
            if pts_2d[i][0] > 1:
                outMap[i] = DrawGaussian(outMap[i], pt[:2], ref.hmGauss)

            outReg[i, 2] = pt[2] / ref.outputRes * 2 - 1

        return frame, outMap, pts_2d, outReg, pts_3d_mono
Beispiel #12
0
    def LoadFrameAndData(self, path, frameName):
        frame = cv2.imread(path + frameName)
        dict = pickle.load(open(path + "data.pkl", 'rb'))

        pts_2d = pts_2d['2d'][int(frameName[:-4]), :]
        pts_3d = pts_3d['3d'][int(frameName[:-4]), :]
        pts_3d_mono = pts_3d_mono['3d'][int(frameName[:-4]), :]

        c = np.ones(2) * ref.ntuImgSize / 2
        s = ref.ntuImgSize * 1.0

        pts_3d = pts_3d - pts_3d[self.root]

        s2d, s3d = 0, 0
        for e in ref.edges:
            s2d += ((pts_2d[e[0]] - pts_2d[e[1]])**2).sum()**0.5
            s3d += ((pts_3d[e[0], :2] - pts_3d[e[1], :2])**2).sum()**0.5
        scale = s2d / s3d

        for j in range(ref.nJoints):
            pts_3d[j, 0] = pts_3d[j, 0] * scale + pts_2d[self.root, 0]
            pts_3d[j, 1] = pts_3d[j, 1] * scale + pts_2d[self.root, 1]
            pts_3d[j, 2] = pts_3d[j, 2] * scale + ref.h36mImgSize / 2

        pts_3d[7, :] = (pts_3d[12, :] + pts_3d[13, :]) / 2

        frame = Crop(frame, c, s, 0, ref.inputRes) / 256.

        outReg = np.zeros((ref.nJoints, 3))
        for i in range(ref.nJoints):
            pt = Transform3D(pts_3d[i], c, s, 0, ref.outputRes)
            outReg[i, 2] = pt[2] / ref.outputRes * 2 - 1

        frame = torch.from_numpy(frame)
        pts_2d = torch.from_numpy(pts_2d)
        outReg = torch.from_numpy(outReg)
        pts_3d_mono = torch.from_numpy(pts_3d_mono)

        return frame, pts_2d, outReg, pts_3d_mono
Beispiel #13
0
def _test_crop(img_shape,
               desired_side=256,
               rot=0,
               img_gen=imread_img,
               new_crop_kw={}):
    """
    @param img_shape: (height, width)
    """
    img = img_gen(img_shape)
    center = np.array([img.shape[1] // 2, img.shape[0] // 2])
    max_side = max(img.shape[:2])
    new_img = OldCrop(img, center, max_side, 0, desired_side)
    proposed_img = Crop(img, center, max_side, 0, desired_side, **new_crop_kw)
    if DEBUG and not (new_img != proposed_img).sum() / np.prod(
            new_img.shape) < 0.02:
        cv2.imshow("Crop", new_img)
        cv2.imshow("NewCrop", proposed_img)
        cv2.imshow("diff", new_img - proposed_img)
        cv2.waitKey(-1)
        LOG.warning("Images did not match with %d pixels" %
                    np.sum(new_img != proposed_img))
        #assert False
    else:
        assert (new_img != proposed_img).sum() / np.prod(new_img.shape) < 0.02
def main():

    # use the model trained with dropout enabled
    model_path = '/home/erl/moshan/orcvio_gamma/orcvio_gamma/pytorch_models/starmap/trained_models/with_dropout/model_cpu.pth'
    img_path = './images/car.png'
    det_name = './det/car.png'

    # by default img size is 256
    inputRes = 256
    outputRes = 64
    CUDA = torch.cuda.is_available()

    model = torch.load(model_path)

    img = cv2.imread(img_path)
    s = max(img.shape[0], img.shape[1]) * 1.0
    c = np.array([img.shape[1] / 2., img.shape[0] / 2.])

    # img = cv2.resize(img, (320, 240))
    # print(img.shape)

    # crop only change h, w, c to c, h, w for images with size 256 x 256
    img = Crop(img, c, s, 0, inputRes).astype(np.float32).transpose(2, 0,
                                                                    1) / 256.
    input = torch.from_numpy(img.copy()).float()

    # change to b, c, h, w
    input = input.view(1, input.size(0), input.size(1), input.size(2))
    input_var = torch.autograd.Variable(input).float()

    if CUDA:
        model.cuda()
        input_var = input_var.cuda()

    output = model(input_var)
    hm = output[-1].data.cpu().numpy()

    # convert to bgr, uint8 for display
    img = (input[0].numpy().transpose(1, 2, 0) * 256).astype(np.uint8).copy()
    inp = img.copy()

    # hm[0, 0] is an image, since 1st dim is batch
    star = (cv2.resize(hm[0, 0], (inputRes, inputRes)) * 255)

    # clip the values to 0-255
    star[star > 255] = 255
    star[star < 0] = 0

    # tile Construct an array by repeating A the number of times given by reps.
    # convert to 3 channels, for bgr
    star = np.tile(star, (3, 1, 1)).transpose(1, 2, 0)
    trans = 0.8
    star = (trans * star + (1. - trans) * img).astype(np.uint8)

    # select peaks and perform nms

    # set nms threshold
    heat_thresh = 0.25

    ps = parseHeatmap(hm[0], heat_thresh)
    canonical, pred, color, score = [], [], [], []

    # mc dropout
    f1 = plt.figure()
    ax1 = f1.add_subplot(111)
    ax1.imshow(img)
    uncertainty_test(model, input_var, heat_thresh, ax1)

    for k in range(len(ps[0])):
        # camviewfeature
        x, y, z = ((hm[0, 1:4, ps[0][k], ps[1][k]] + 0.5) * outputRes).astype(
            np.int32)
        dep = ((hm[0, 4, ps[0][k], ps[1][k]] + 0.5) * outputRes).astype(
            np.int32)
        canonical.append([x, y, z])

        pred.append([ps[1][k], outputRes - dep, outputRes - ps[0][k]])
        # kp confidence score
        score.append(hm[0, 0, ps[0][k], ps[1][k]])

        color.append(
            (1.0 * x / outputRes, 1.0 * y / outputRes, 1.0 * z / outputRes))

        # cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → img
        # -1 means that a filled circle is to be drawn
        cv2.circle(img, (ps[1][k] * 4, ps[0][k] * 4), 6, (0, 0, 255), -1)
        cv2.circle(img, (ps[1][k] * 4, ps[0][k] * 4), 2,
                   (int(z * 4), int(y * 4), int(x * 4)), -1)

        # plot cov
        # pos = kps_mean[k]
        # covar = kps_cov[k]
        # draw_ellipse(pos, covar, ax1)

    plt.axis('off')
    ax1.get_xaxis().set_visible(False)
    ax1.get_yaxis().set_visible(False)
    plt.show()
    f1.savefig('kp_cov.png', bbox_inches='tight', pad_inches=0)
    # plt.pause(5)

    pred = np.array(pred).astype(np.float32)
    canonical = np.array(canonical).astype(np.float32)

    pointS = canonical * 1.0 / outputRes
    pointT = pred * 1.0 / outputRes

    # calculate viewpoint
    R, t, s = horn87(pointS.transpose(), pointT.transpose(), score)

    rotated_pred = s * np.dot(
        R, canonical.transpose()).transpose() + t * outputRes
Beispiel #15
0
    def __getitem__(self, index):
        seqIdx = self.getSeq(index)
        """
        input: predSeqLen x 3 x inputRes x inputRes   Input image After Crop and transform
        hmap:  predSeqLen x numJoints x outputRes x outputRes
        gtpts: predSeqLen x numJoints x 2             Joints Positions BEFORE crop and transform
        proj:  predSeqLen x numJoints x 2             Joints Positions AFTER crop and transform
        """
        input = np.zeros((self.nPhase, 3, self.inputRes, self.inputRes))
        hmap = np.zeros(
            (self.nPhase, self.nJoints, self.outputRes, self.outputRes))
        gtpts = np.zeros((self.nPhase, self.nJoints, 2))
        repos, trans, focal, proj = {}, {}, {}, {}
        for i in range(len(seqIdx)):
            sid = seqIdx[i]
            im = self.LoadImage(int(sid))

            if i == 0:
                center, scale = self.getCenterScale(im)
            inp = Crop(im, center, scale, 0, self.inputRes)
            pts = self.part[int(sid)]

            pj = np.zeros(np.shape(pts))
            for j in range(len(pts)):
                if pts[j][0] != 0 and pts[j][1] != 0:
                    pj[j] = Transform(pts[j], center, scale, 0, self.outputRes,
                                      False)

            hm = np.zeros((np.shape(pts)[0], self.outputRes, self.outputRes))
            for j in range(len(pts)):
                if pts[j][0] != 0 and pts[j][1] != 0:
                    DrawGaussian(hm[j], np.round(pj[j]), 2)

            inp = inp.transpose(2, 1, 0)
            input[i] = inp
            repos[i] = np.zeros((np.size(1), 3))
            trans[i] = np.zeros(3)
            focal[i] = np.zeros(1)
            hmap[i] = hm
            proj[i] = pj
            gtpts[i] = pts

        if self.split == 'train':
            m1 = np.random.uniform(0.8, 1.2)
            m2 = np.random.uniform(0.8, 1.2)
            m3 = np.random.uniform(0.8, 1.2)
            for i in range(len(input)):
                input[i][:, :, 0] = input[i][:, :, 0] * m1
                np.clip(input[i][:, :, 0], 0, 1, out=input[i][:, :, 0])

                input[i][:, :, 1] = input[i][:, :, 1] * m2
                np.clip(input[i][:, :, 1], 0, 1, out=input[i][:, :, 1])

                input[i][:, :, 2] = input[i][:, :, 2] * m3
                np.clip(input[i][:, :, 2], 0, 1, out=input[i][:, :, 2])

            if np.random.uniform() <= 0.5:
                for i in range(len(input)):
                    input[i] = cv2.flip(input[i], 1)
                    hmap[i] = Flip(ShuffleLR(hmap[i]))
                    proj[i] = ShuffleLR(proj[i])
                    ind = np.where(proj[i] == 0)
                    proj[i][:, 0] = self.outputRes - proj[i][:, 0] + 1
                    if len(ind[0]) != 0:
                        proj[i][ind[0][0]] = 0

        return {
            'input': input,
            'label': hmap,
            'gtpts': gtpts,
            'center': center,
            'scale': scale,
            'proj': proj
        }
Beispiel #16
0
def main():
    opt = opts().parse()
    model = torch.load(opt.loadModel)
    img = cv2.imread(opt.demo)
    s = max(img.shape[0], img.shape[1]) * 1.0
    c = np.array([img.shape[1] / 2., img.shape[0] / 2.])
    img = Crop(img, c, s, 0, ref.inputRes) / 256.
    input = torch.from_numpy(img.copy()).float()
    input = input.view(1, input.size(0), input.size(1), input.size(2))
    input_var = torch.autograd.Variable(input).float()
    if opt.GPU > -1:
        model = model.cuda(opt.GPU)
        input_var = input_var.cuda(opt.GPU)

    output = model(input_var)
    hm = output[-1].data.cpu().numpy()

    debugger = Debugger()
    img = (input[0].numpy().transpose(1, 2, 0) * 256).astype(np.uint8).copy()
    inp = img.copy()
    star = (cv2.resize(hm[0, 0], (ref.inputRes, ref.inputRes)) * 255)
    star[star > 255] = 255
    star[star < 0] = 0
    star = np.tile(star, (3, 1, 1)).transpose(1, 2, 0)
    trans = 0.8
    star = (trans * star + (1. - trans) * img).astype(np.uint8)

    ps = parseHeatmap(hm[0], thresh=0.1)
    canonical, pred, color, score = [], [], [], []
    for k in range(len(ps[0])):
        x, y, z = ((hm[0, 1:4, ps[0][k], ps[1][k]] + 0.5) *
                   ref.outputRes).astype(np.int32)
        dep = ((hm[0, 4, ps[0][k], ps[1][k]] + 0.5) * ref.outputRes).astype(
            np.int32)
        canonical.append([x, y, z])
        pred.append([ps[1][k], ref.outputRes - dep, ref.outputRes - ps[0][k]])
        score.append(hm[0, 0, ps[0][k], ps[1][k]])
        color.append((1.0 * x / ref.outputRes, 1.0 * y / ref.outputRes,
                      1.0 * z / ref.outputRes))
        cv2.circle(img, (ps[1][k] * 4, ps[0][k] * 4), 4, (255, 255, 255), -1)
        cv2.circle(img, (ps[1][k] * 4, ps[0][k] * 4), 2,
                   (int(z * 4), int(y * 4), int(x * 4)), -1)

    pred = np.array(pred).astype(np.float32)
    canonical = np.array(canonical).astype(np.float32)

    pointS = canonical * 1.0 / ref.outputRes
    pointT = pred * 1.0 / ref.outputRes
    R, t, s = horn87(pointS.transpose(), pointT.transpose(), score)

    rotated_pred = s * np.dot(
        R, canonical.transpose()).transpose() + t * ref.outputRes

    debugger.addImg(inp, 'inp')
    debugger.addImg(star, 'star')
    debugger.addImg(img, 'nms')
    debugger.addPoint3D(canonical / ref.outputRes - 0.5, c=color, marker='^')
    debugger.addPoint3D(pred / ref.outputRes - 0.5, c=color, marker='x')
    debugger.addPoint3D(rotated_pred / ref.outputRes - 0.5,
                        c=color,
                        marker='*')

    debugger.showAllImg(pause=True)
    debugger.show3D()
import ref
import cv2
import torch
import numpy as np

from utils.img import Crop, DrawGaussian, Transform3D

torch.set_printoptions(precision=5)

c = np.ones(2) * ref.h36mImgSize / 2
s = ref.h36mImgSize * 1.0

img1 = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000111.jpg'
)
img1 = Crop(img1, c, s, 0, ref.inputRes) / 256.
img2 = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000112.jpg'
)
img2 = Crop(img2, c, s, 0, ref.inputRes) / 256.
img3 = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000113.jpg'
)
img3 = Crop(img3, c, s, 0, ref.inputRes) / 256.0

img1 = torch.from_numpy(img1).cuda().float().unsqueeze(1)
img2 = torch.from_numpy(img2).cuda().float().unsqueeze(1)
img3 = torch.from_numpy(img3).cuda().float().unsqueeze(1)

img = torch.cat((img1, img2, img3), 1).contiguous()
img.unsqueeze_(0)
    def poseEstimation(self, tracked_person):
        person_id = tracked_person.person_id

        try:
            curImage = self.bridge.imgmsg_to_cv2(self.frameInfo.image_frame)
            person_image = curImage[
                int(tracked_person.bbox.top):int(tracked_person.bbox.top +
                                                 tracked_person.bbox.height),
                int(tracked_person.bbox.left):int(tracked_person.bbox.left +
                                                  tracked_person.bbox.width)]
        except CvBridgeError as e:
            rospy.logerr(e)

        # Resize input image
        rospy.logdebug("person image shape: {}".format(person_image.shape))
        if person_image.shape != self.image_shape:
            h, w = person_image.shape[0], person_image.shape[1]
            center = torch.FloatTensor((w / 2, h / 2))
            scale = 1.0 * max(h, w)
            res = 256
            input_image = Crop(person_image, center, scale, 0, res)
        else:
            input_image = person_image

        # Feed input image to model
        rospy.loginfo("feeding image to model")
        input = torch.from_numpy(input_image.transpose(2, 0, 1)).float() / 256.
        input = input.view(1, input.size(0), input.size(1), input.size(2))
        input_var = torch.autograd.Variable(input).float().cuda()

        # lock when using model to estimate pose
        self.lock.acquire()
        try:
            output = self.model(input_var)
        finally:
            self.lock.release()

        rospy.logdebug("got output from model")

        # Get 2D pose
        rospy.logdebug("Rendering 2D pose")
        pose2D = getPreds((output[-2].data).cpu().numpy())[0] * 4

        # Get 3D pose
        rospy.logdebug("Rendering 3D pose")
        reg = (output[-1].data).cpu().numpy().reshape(pose2D.shape[0], 1)
        pose3D = np.concatenate([pose2D, (reg + 1) / 2. * 256], axis=1)
        rospy.logdebug("pose 3d shape: {}".format(pose3D.shape))

        for pose in pose3D:
            joint = Point()
            joint.x = pose[0]
            joint.y = pose[1]
            joint.z = pose[2]
            tracked_person.person_pose.append(joint)

        # publish person
        if self.publish_person:
            self.person_pub.publish(tracked_person)

        self.lock.acquire()
        try:
            self.frameInfo.persons.append(tracked_person)
        finally:
            self.lock.release()

        rospy.logdebug("pose3D: \n {}".format(pose3D))

        # Save pose image
        if self.save_pose_image:
            cv2.imwrite(
                pkg_path + '/scripts/debug/original/ogImg_' +
                str(self.frame_id) + '.png', self.cv_image)
            cv2.imwrite(
                pkg_path + '/scripts/debug/input/inputImg_' +
                str(self.frame_id) + '.png', input_image)
            self.debugger.addImg(input_image, imgId=self.frame_id)
            self.debugger.addPoint2D(pose2D, (255, 0, 0), imgId=self.frame_id)
            self.debugger.saveImg(pkg_path + '/scripts/debug/pose/poseImg_' +
                                  str(self.frame_id) + '.png',
                                  imgId=self.frame_id)

        if self.save_pose_file:
            file_name = pkg_path + '/pose_file/pose_{:04d}.txt'.format(
                self.frame_id)
            with file(file_name, 'w') as outfile:
                np.savetxt(outfile, pose3D, fmt='%-7.2f')

        rospy.loginfo("Person {} processing finished".format(person_id))
Beispiel #19
0
import ref
import cv2
import torch
import numpy as np

from utils.img import Crop, DrawGaussian, Transform3D

c = np.ones(2) * ref.h36mImgSize / 2
s = ref.h36mImgSize * 1.0

img = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000111.jpg'
)

img = Crop(img, c, s, 0, ref.inputRes) / 256.
img.shape

img = torch.from_numpy(img).unsqueeze(0).cuda()

out3d = img[:, :, None, :, :].expand(1, 3, 32, 256, 256).cuda()
model3d = torch.load('inflatedModel.pth').cuda()

out2d = img.expand(32, 3, 256, 256).cuda()
import pickle
from functools import partial
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
pickle.load = partial(pickle.load, encoding="latin1")
model = torch.load('models/hgreg-3d.pth').cuda()

out2d = model.conv1_(out2d)
out2d = model.bn1(out2d)
import numpy as np
torch.set_printoptions(precision=5)

import pickle
from functools import partial
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
pickle.load = partial(pickle.load, encoding="latin1")
from utils.img import Crop, DrawGaussian, Transform3D

c = np.ones(2) * ref.h36mImgSize / 2
s = ref.h36mImgSize * 1.0

img1 = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000111.jpg'
)
img1 = Crop(img1, c, s, 0, ref.inputRes) / 256.

img2 = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000112.jpg'
)
img2 = Crop(img2, c, s, 0, ref.inputRes) / 256.

img3 = cv2.imread(
    '../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000113.jpg'
)
img3 = Crop(img3, c, s, 0, ref.inputRes) / 256.

img1 = torch.from_numpy(img1).cuda().float()
img2 = torch.from_numpy(img2).cuda().float()
img3 = torch.from_numpy(img3).cuda().float()
import ref
import cv2
import torch
import numpy as np

from utils.img import Crop, DrawGaussian, Transform3D

c = np.ones(2) * ref.h36mImgSize / 2
s = ref.h36mImgSize * 1.0

img = cv2.imread('../data/h36m/s_01_act_02_subact_01_ca_03/s_01_act_02_subact_01_ca_03_000111.jpg')

img = Crop(img, c, s, 0, ref.inputRes) / 256.
img.shape


img = torch.from_numpy(img).unsqueeze(0).cuda()



out = img[:,:,None,:,:].expand(1,3,32,256,256).cuda()
model3d = torch.load('inflatedModel.pth').cuda()



print("Script2D")

out = model3d(out)[2]
print(out[0,:,0,:])
print("")