コード例 #1
0
    def visualize_heatmaps(self, img, cls_map, reg_map, clusters, prob_thresh=1, nms_thresh=1, iou=None):
        """
        Expect cls_map and reg_map to be of the form HxWxC
        """
        fy, fx, fc = np.where(cls_map >= prob_thresh)

        # print(iou.shape)
        # best_iou = iou.max(axis=3)
        # print(best_iou.shape)
        # fy, fx, fc = np.where(best_iou >= 0.5)  # neg thresh

        cy, cx = fy*self.sty + self.ofy, fx*self.stx + self.ofx
        cw = clusters[fc, 2] - clusters[fc, 0] + 1
        ch = clusters[fc, 3] - clusters[fc, 1] + 1

        # box_ovlp = best_iou[fc, fy, fx]
        num_clusters = clusters.shape[0]

        # refine bounding box
        tx = reg_map[:, :, 0*num_clusters:1*num_clusters]
        ty = reg_map[:, :, 1*num_clusters:2*num_clusters]
        tw = reg_map[:, :, 2*num_clusters:3*num_clusters]
        th = reg_map[:, :, 3*num_clusters:4*num_clusters]

        dcx = cw * tx[fy, fx, fc]
        dcy = ch * ty[fy, fx, fc]

        rx = cx + dcx
        ry = cy + dcy

        rw = cw * np.exp(tw[fy, fx, fc])
        rh = ch * np.exp(th[fy, fx, fc])

        bboxes = np.array([np.abs(rx-rw/2), np.abs(ry-rh/2), rx+rw/2, ry+rh/2]).T

        scores = cls_map[fy, fx, fc]

        dets = np.hstack((bboxes, scores[:, np.newaxis]))
        keep = nms(dets, nms_thresh)
        bboxes = dets[keep][:, 0:4]
        # bbox_iou = best_iou[fy, fx, fc]

        # print("Best bounding box", bboxes)
        # print(bboxes.shape)

        print("Number of bboxes ", bboxes.shape[0])
        for idx, bbox in enumerate(bboxes):
            bbox = np.round(np.array(bbox))
            print(bbox)
            # img = draw_bounding_box(img, bbox, {"name": "car {0}".format(np.around(bbox_iou[idx], decimals=2))})
            img = draw_bounding_box(img, bbox, {"name": "car {0}".format(idx)})

            # if idx == 20:
            #     break

        img.show(title="Heatmap visualized")
コード例 #2
0
    def visualize_bboxes(image, bboxes):
        """

        :param image: PIL image
        :param bboxes:
        :return:
        """
        print("Number of GT bboxes", bboxes.shape[0])
        for idx, bbox in enumerate(bboxes):
            bbox = np.round(np.array(bbox))
            # print(bbox)
            image = draw_bounding_box(image, bbox, {"name": "car {0}".format(idx)})

        image.show(title="GT BBox")
コード例 #3
0
    def render_and_save_bboxes(image, image_id, bboxes, scores, scales, directory="qualitative"):
        """
        Render the bboxes on the image and save the image
        :param image: PIL image
        :param image_id:
        :param bboxes:
        :param scores:
        :param scales:
        :param directory:
        :return:
        """
        for idx, bbox in enumerate(bboxes):
            bbox = np.round(np.array(bbox))
            image = draw_bounding_box(image, bbox, {'score': scores[idx], 'scale': scales[idx]})

        image.save("{0}/{1}.jpg".format(directory, image_id))