Exemplo n.º 1
0
    def __getitem__(self, idx):
        epiInd = 0  # calculate the epiInd
        while idx >= self.episodeNum[epiInd]:
            # print self.episodeNum[epiInd],
            epiInd += 1
        if epiInd > 0:
            idx -= self.episodeNum[epiInd - 1]

        # random fliping
        flipping = False
        if self.aug and random.random() > 0.5:
            flipping = True
        # print epiInd, idx
        imgseq = []
        for k in range(self.batch):
            img = cv2.imread(self.imgnamelist[epiInd][idx + k])

            if self.aug:
                img = im_hsv_augmentation(img)
                img = im_crop(img)

            outimg = im_scale_norm_pad(img,
                                       outsize=self.imgsize,
                                       mean=self.mean,
                                       std=self.std,
                                       down_reso=True,
                                       flip=flipping)

            imgseq.append(outimg)

        return np.array(imgseq)
Exemplo n.º 2
0
def detect(image, confidence_thres=0.8, display_label=True):
    ### detect and mark a person bb in image, return the marked image
    (h, w) = image.shape[:2]
    # print(h, w)

    blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007843,
                                 (300, 300), 127.5)

    net.setInput(blob)
    detections = net.forward()

    for i in np.arange(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]

        if confidence > confidence_thres:
            idx = int(detections[0, 0, i, 1])
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            if CLASSES[idx] == "person":
                mX = (endX - startX) // 10
                mY = (endY - startY) // 10
                startX = max(startX - mX, 0)
                endX = min(endX + mX, w - 1)
                startY = max(startY - mY, 0)
                endY = min(endY + mY, h - 1)

                # get direction
                subimg = image[startY:endY, startX:endX]
                subimg = utils.im_scale_norm_pad(subimg,
                                                 mean=[127.5, 127.5, 127.5],
                                                 std=[127.5, 127.5, 127.5])

                inp = torch.tensor(np.array([subimg]))
                angle = predictor.forward(inp)

                angle = angle.data[0]
                alpha = np.array(angle)

                # display bounding box
                out_img = utils.img_denormalize(subimg, [127.5, 127.5, 127.5],
                                                [127.5, 127.5, 127.5])
                put_arrow(out_img, alpha)
                out_img = cv2.resize(out_img, (0, 0), fx=3.0, fy=3.0)

                cv2.imshow("Frame", out_img)
                key = cv2.waitKey(5)

                cv2.rectangle(image, (startX, startY), (endX, endY),
                              COLORS[idx], 2)

    return image
Exemplo n.º 3
0
    def augment_image(self, img, flipping):
        # augment image to make "new" data
        if self.aug:
            img = utils.im_hsv_augmentation(img)
            img = utils.im_crop(img, maxscale=self.maxscale)

        out_img = utils.im_scale_norm_pad(
            img,
            self.mean,
            self.std,
            out_size=self.img_size,
            # down_reso=True,
            flip=flipping)

        return out_img
Exemplo n.º 4
0
    def __getitem__(self, idx):
        img = cv2.imread(self.imgnamelist[idx])  # in bgr
        label = np.array(self.labellist[idx], dtype=np.float32)

        # random fliping
        flipping = False
        if self.aug and random.random() > 0.5:
            flipping = True
            label[1] = -label[1]
        if self.aug:
            img = im_hsv_augmentation(img)
            img = im_crop(img, maxscale=self.maxscale)

        outimg = im_scale_norm_pad(img,
                                   outsize=self.imgsize,
                                   mean=self.mean,
                                   std=self.std,
                                   down_reso=True,
                                   flip=flipping)

        return {'img': outimg, 'label': label}
Exemplo n.º 5
0
    def __getitem__(self, idx):
        epiInd = 0  # calculate the epiInd
        while idx >= self.episodeNum[epiInd]:
            # print self.episodeNum[epiInd],
            epiInd += 1
        if epiInd > 0:
            idx -= self.episodeNum[epiInd - 1]

        # random fliping
        flipping = False
        if self.aug and random.random() > 0.5:
            flipping = True

        # print epiInd, idx
        imgseq = []
        labelseq = []
        for k in range(self.batch):
            img = cv2.imread(self.imgnamelist[epiInd][idx + k][0])
            angle = self.imgnamelist[epiInd][idx + k][1]
            direction_angle_cos = np.cos(float(angle))
            direction_angle_sin = np.sin(float(angle))
            label = np.array([direction_angle_sin, direction_angle_cos],
                             dtype=np.float32)

            if self.aug:
                img = im_hsv_augmentation(img)
                img = im_crop(img)
                if flipping:
                    label[1] = -label[1]
            outimg = im_scale_norm_pad(img,
                                       outsize=self.imgsize,
                                       mean=self.mean,
                                       std=self.std,
                                       down_reso=True,
                                       flip=flipping)

            imgseq.append(outimg)
            labelseq.append(label)

        return {'imgseq': np.array(imgseq), 'labelseq': np.array(labelseq)}
Exemplo n.º 6
0
    def __getitem__(self, idx):
        if self.filename[-3:] == 'csv':
            point_info = self.items.iloc[idx]
        else:
            point_info = self.items[idx]
        #print(point_info)
        img_name = point_info['path']
        direction_angle = point_info['direction_angle']

        direction_angle_cos = np.cos(float(direction_angle))
        direction_angle_sin = np.sin(float(direction_angle))
        label = np.array([direction_angle_sin, direction_angle_cos],
                         dtype=np.float32)
        img = cv2.imread(img_name)

        # random fliping
        flipping = False
        if self.aug and random.random() > 0.5:
            flipping = True
            label[1] = -label[1]

        if img is None:
            print 'error image:', img_name
            return
        if self.aug:
            img = im_hsv_augmentation(img, Hscale=10, Sscale=60, Vscale=60)
            img = im_crop(img, maxscale=self.maxscale)

        outimg = im_scale_norm_pad(img,
                                   outsize=self.imgsize,
                                   mean=self.mean,
                                   std=self.std,
                                   down_reso=True,
                                   flip=flipping)

        return {'img': outimg, 'label': label}