Exemple #1
0
    def image_cb(self, msg):
        img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8')
        resized_img = cv2.resize(img, (self.resized_W, self.resized_H))
        H, W, _ = img.shape
        y_scale = self.resized_H / H
        x_scale = self.resized_W / W

        poses, _ = self.engine.DetectPosesInImage(resized_img.astype(np.uint8))

        poses_msg = PeoplePoseArray()
        poses_msg.header = msg.header
        points = []
        visibles = []
        for pose in poses:
            if pose.score < self.score_thresh:
                continue
            pose_msg = PeoplePose()
            point = []
            visible = []
            for lbl, keypoint in pose.keypoints.items():
                resized_key_y, resized_key_x = keypoint.yx
                key_y = resized_key_y / y_scale
                key_x = resized_key_x / x_scale
                point.append((key_y, key_x))
                if keypoint.score < self.joint_score_thresh:
                    visible.append(False)
                    continue
                pose_msg.limb_names.append(lbl)
                pose_msg.scores.append(keypoint.score)
                pose_msg.poses.append(Pose(position=Point(x=key_x, y=key_y)))
                visible.append(True)
            poses_msg.poses.append(pose_msg)
            points.append(point)
            visibles.append(visible)
        self.pub_pose.publish(poses_msg)

        points = np.array(points, dtype=np.int32)
        visibles = np.array(visibles, dtype=np.bool)

        if self.visualize:
            fig = plt.figure(tight_layout={'pad': 0})
            ax = plt.Axes(fig, [0., 0., 1., 1.])
            ax.axis('off')
            fig.add_axes(ax)
            vis_point(img.transpose((2, 0, 1)), points, visibles, ax=ax)
            fig.canvas.draw()
            w, h = fig.canvas.get_width_height()
            vis_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
            vis_img.shape = (h, w, 3)
            fig.clf()
            plt.close()
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/  # NOQA
            vis_msg.step = int(vis_msg.step)
            vis_msg.header = msg.header
            self.pub_image.publish(vis_msg)
Exemple #2
0
    def image_cb(self, msg):
        img = self.bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8')
        resized_img = cv2.resize(img, (641, 481))
        poses, _ = self.engine.DetectPosesInImage(resized_img.astype(np.uint8))

        poses_msg = PeoplePoseArray()
        poses_msg.header = msg.header
        points = []
        visibles = []
        for pose in poses:
            if pose.score < self.score_thresh:
                continue
            pose_msg = PeoplePose()
            point = []
            visible = []
            for lbl, keypoint in pose.keypoints.items():
                point.append(keypoint.yx)
                if keypoint.score < self.joint_score_thresh:
                    visible.append(False)
                    continue
                pose_msg.limb_names.append(lbl)
                pose_msg.scores.append(keypoint.score)
                pose_msg.poses.append(
                    Pose(position=Point(x=keypoint.yx[1],
                                        y=keypoint.yx[0])))
                visible.append(True)
            poses_msg.poses.append(pose_msg)
            points.append(point)
            visibles.append(visible)
        self.pub_pose.publish(poses_msg)

        points = np.array(points, dtype=np.int32)
        visibles = np.array(visibles, dtype=np.bool)

        if self.visualize:
            vis_img = resized_img.transpose(2, 0, 1)
            vis_point(vis_img, points, visibles)
            fig = plt.gcf()
            fig.canvas.draw()
            w, h = fig.canvas.get_width_height()
            vis_img = np.fromstring(
                fig.canvas.tostring_rgb(), dtype=np.uint8)
            fig.clf()
            vis_img.shape = (h, w, 3)
            plt.close()
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/  # NOQA
            vis_msg.step = int(vis_msg.step)
            vis_msg.header = msg.header
            self.pub_image.publish(vis_msg)
Exemple #3
0
    def test_vis_point(self):
        img = np.random.randint(
            0, 255, size=(3, 32, 32)).astype(np.float32)
        point = np.random.uniform(size=(3, 2)).astype(np.float32)
        ax = vis_point(img, point, self.mask)

        self.assertTrue(isinstance(ax, matplotlib.axes.Axes))
Exemple #4
0
    def test_vis_point(self):
        if optional_modules:
            img = np.random.randint(
                0, 255, size=(3, 32, 32)).astype(np.float32)
            point = np.random.uniform(size=(3, 2)).astype(np.float32)
            ax = vis_point(img, point, self.mask)

            self.assertTrue(isinstance(ax, matplotlib.axes.Axes))
    def visualize_cb(self, event):
        if (not self.visualize or self.img is None or self.points is None
                or self.visibles is None):
            return

        with self.lock:
            img = self.img.copy()
            header = copy.deepcopy(self.header)
            points = self.points.copy()
            visibles = self.visibles.copy()

        fig = plt.figure(tight_layout={'pad': 0})
        ax = plt.Axes(fig, [0., 0., 1., 1.])
        ax.axis('off')
        fig.add_axes(ax)
        vis_point(img.transpose((2, 0, 1)), points, visibles, ax=ax)
        fig.canvas.draw()
        w, h = fig.canvas.get_width_height()
        vis_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8)
        vis_img.shape = (h, w, 3)
        fig.clf()
        plt.close()
        if self.pub_image.get_num_connections() > 0:
            vis_msg = self.bridge.cv2_to_imgmsg(vis_img, 'rgb8')
            # BUG: https://answers.ros.org/question/316362/sensor_msgsimage-generates-float-instead-of-int-with-python3/  # NOQA
            vis_msg.step = int(vis_msg.step)
            vis_msg.header = header
            self.pub_image.publish(vis_msg)
        if self.pub_image_compressed.get_num_connections() > 0:
            # publish compressed http://wiki.ros.org/rospy_tutorials/Tutorials/WritingImagePublisherSubscriber  # NOQA
            vis_compressed_msg = CompressedImage()
            vis_compressed_msg.header = header
            vis_compressed_msg.format = "jpeg"
            vis_img_rgb = cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB)
            vis_compressed_msg.data = np.array(
                cv2.imencode('.jpg', vis_img_rgb)[1]).tostring()
            self.pub_image_compressed.publish(vis_compressed_msg)
        video_id, frame_id = self._get_ids(i)
        anno = self.annos[self.video_names[video_id]]
        poly = np.array(anno['gt_rect'][frame_id])
        if len(poly) == 8:
            poly = poly.reshape((4, 2))[:, ::-1]  # y,x
        elif len(poly) == 4:
            x = poly[0]
            y = poly[1]
            w = poly[2]
            h = poly[3]
            poly = np.array((
                (y, x),
                (y, x + w),
                (y + h, x + w),
                (y + h, x)), dtype=np.float32)
        return poly[None]


if __name__ == '__main__':
    from chainercv.visualizations import vis_bbox
    from chainercv.visualizations import vis_point
    import matplotlib.pyplot as plt

    dataset = VOTTrackingDataset('../../data')
    print(dataset.video_names[14])
    img, bbox, poly = dataset.slice[:, ['img', 'bbox', 'poly']][4322]
    vis_bbox(img, bbox)
    plt.savefig('b.png')
    vis_point(img, poly)
    plt.savefig('a.png')