Exemplo n.º 1
0
def imshow_bboxes_custom(img,
                         bboxes,
                         colors='green',
                         top_k=-1,
                         thickness=1,
                         show=True,
                         win_name='',
                         wait_time=0,
                         out_file=None):
    """Draw bboxes on an image.
    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (list or ndarray): A list of ndarray of shape (k, 4).
        colors (list[str or tuple or Color]): A list of colors.
        top_k (int): Plot the first k bboxes only if set positive.
        thickness (int): Thickness of lines.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str, optional): The filename to write the image.
    """
    img = imread(img)

    if isinstance(bboxes, np.ndarray):
        bboxes = [bboxes]
    if not isinstance(colors, list):
        colors = [colors for _ in range(len(bboxes))]
    colors = [color_val(c) for c in colors]
    assert len(bboxes) == len(colors)

    for i, _bboxes in enumerate(bboxes):
        label_text = _bboxes[:, 4]
        color_text = color_val('green')
        _bboxes = _bboxes.astype(np.int32)
        if top_k <= 0:
            _top_k = _bboxes.shape[0]
        else:
            _top_k = min(top_k, _bboxes.shape[0])
        for j in range(_top_k):
            left_top = (_bboxes[j, 0], _bboxes[j, 1])
            right_bottom = (_bboxes[j, 2], _bboxes[j, 3])
            cv2.rectangle(img,
                          left_top,
                          right_bottom,
                          colors[i],
                          thickness=thickness)
            cv2.putText(img, '{:.02f}'.format(label_text[j]),
                        (_bboxes[j, 0], _bboxes[j, 3] - 2),
                        cv2.FONT_HERSHEY_COMPLEX, 1, color_text)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
Exemplo n.º 2
0
def show_bbox(img,
              labels,
              bboxes,
              class_names=None,
              show_threshold=0.1,
              bbox_color='green',
              text_color='green',
              no_name=False):
    # labels >= 1
    thickness = 2
    font_scale = 1
    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)
    if show_threshold > 0 and bboxes.shape[1] == 5:
        labels = labels[bboxes[:, -1] > show_threshold]
        bboxes = bboxes[bboxes[:, -1] > show_threshold]

    for bbox, label in zip(bboxes, labels):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        cv2.rectangle(img,
                      left_top,
                      right_bottom,
                      bbox_color,
                      thickness=thickness)
        if no_name:
            if len(bbox) > 4:
                if bbox_int[1] > 20:
                    txt_loac = (bbox_int[0], bbox_int[1])
                else:
                    txt_loac = (bbox_int[0], bbox_int[3])
                cv2.putText(img, '{:.02f}'.format(bbox[-1]), txt_loac,
                            cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)
        else:
            assert class_names is not None
            label_text = class_names[int(label.item(
            ))] if class_names is not None else 'cls {}'.format(label)
            if len(bbox) > 4:
                label_text += '|{:.02f}'.format(bbox[-1])
            cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                        cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)
    return img
Exemplo n.º 3
0
Arquivo: base.py Projeto: daniel616/DL
def show_det_bboxes(img,
                    bboxes,
                    labels,
                    out_file,
                    gt_bboxes=None,
                    class_names=None,
                    score_thr=0.05,
                    bbox_color='yellow',
                    gt_color='green',
                    top_k=5):

    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)
    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    topinds = np.argsort(bboxes[:, 4])
    topinds = topinds[:top_k] if len(topinds) >= top_k else topinds
    bboxes = bboxes[topinds, :]
    labels = labels[topinds]

    bbox_color = color_val(bbox_color)
    gt_color = color_val(gt_color)

    for bbox, label in zip(bboxes, labels):
        addBox(img, bbox, bbox_color, label, class_names=class_names)

    if gt_bboxes is not None:
        assert len(gt_bboxes) == 1
        gt_bboxes = gt_bboxes[0]
        for gt_box in gt_bboxes:
            gt_box = gt_box.numpy()
            addBox(img, gt_box, gt_color, 0, class_names=class_names)

    imwrite(img, out_file)
def show_result(img, result, class_names):
    global mask_rcnn_flag
    img_mask = img.copy()
    mask_temp = img.copy()
    bbox_result, segm_result = result
    bboxes = np.vstack(bbox_result)
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]

    labels = np.concatenate(labels)
    bbox_color = 'green'
    text_color = 'green'
    thickness = 1
    font_scale = 3
    show = True
    win_name = ''
    wait_time = 0
    out_file = None
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    prev_point = [0, 0]

    for i in range(0, len(bboxes)):
        label_text = class_names[
            labels[i]] if class_names is not None else 'cls {}'.format(
                labels[i])
        if len(bboxes[i]) > 4:
            label_text += '|{:.02f}'.format(bboxes[i][-1])
        left_top = (int(bboxes[i][0]), int(bboxes[i][1]))
        right_bottom = (int(bboxes[i][2]), int(bboxes[i][3]))
        if bboxes[i][-1] < 0.5:
            pass
        else:
            img = cv2.putText(img, str(label_text),
                              (int(bboxes[i][0]), int(bboxes[i][1])),
                              cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
            #print(bboxes[i])
            img = cv2.rectangle(img, (int(bboxes[i][0]), int(bboxes[i][1])),
                                (int(bboxes[i][2]), int(bboxes[i][3])),
                                (255, 0, 0), 1)
    mask_temp = np.zeros((720, 1280), dtype=np.uint8)
    if segm_result is not None:

        segms = mmcv.concat_list(segm_result)
        inds = np.where(bboxes[:, -1] > score_thr)[0]
        mask_temp = mask_temp * 0
        inds_len = 0
        for i in inds:
            label_text = class_names[
                labels[i]] if class_names is not None else 'cls {}'.format(
                    labels[i])
            if label_text == 'banana':
                color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8)

                mask = maskUtils.decode(segms[i]).astype(np.bool)

                mask_temp[mask] = 255
                #print(color_mask.shape)
                #print(color_mask)
                img_mask[mask] = img_mask[mask] * 0.5 + color_mask * 0.5
                img_mask = cv2.putText(
                    img_mask, str(label_text),
                    (int(bboxes[i][0]), int(bboxes[i][1])),
                    cv2.FONT_HERSHEY_SIMPLEX, 1,
                    (int(color_mask[0, 0]), int(
                        color_mask[0, 1]), int(color_mask[0, 2])), 2)
                init_rect = [
                    int(bboxes[i][0]),
                    int(bboxes[i][1]),
                    int(bboxes[i][2]) - int(bboxes[i][0]),
                    int(bboxes[i][3]) - int(bboxes[i][1])
                ]
                #print(init_rect)
                tracker.init(color_img, init_rect)
                inds_len = inds_len + 1
                #print("banana")

                continue

    mask_rcnn_flag = 0
    return (img_mask, img, mask_temp)
Exemplo n.º 5
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0,
                      bbox_color='green',
                      text_color='green',
                      thickness=1,
                      font_scale=0.5,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None,
                      ret=False):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])

        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)
        if 'person' not in label_text and 'car' not in label_text:
            continue
        if 'person' in label_text:
            bbox_color = color_val('green')
            text_color = color_val('green')
        else:
            bbox_color = color_val('yellow')
            text_color = color_val('yellow')
        if len(bbox) > 4:
            label_text += '|{:.02f}'.format(bbox[-1])
        cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)
        cv2.rectangle(img,
                      left_top,
                      right_bottom,
                      bbox_color,
                      thickness=thickness)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
    if ret:
        return img
Exemplo n.º 6
0
def imshow_mesh_3d(img,
                   vertices,
                   faces,
                   camera_center,
                   focal_length,
                   colors=(76, 76, 204)):
    """Render 3D meshes on background image.

    Args:
        img(np.ndarray): Background image.
        vertices (list of np.ndarray): Vetrex coordinates in camera space.
        faces (list of np.ndarray): Faces of meshes.
        camera_center ([2]): Center pixel.
        focal_length ([2]): Focal length of camera.
        colors (list[str or tuple or Color]): A list of mesh colors.
    """

    H, W, C = img.shape

    if not has_pyrender:
        warnings.warn('pyrender package is not installed.')
        return img

    if not has_trimesh:
        warnings.warn('trimesh package is not installed.')
        return img

    try:
        renderer = pyrender.OffscreenRenderer(viewport_width=W,
                                              viewport_height=H)
    except (ImportError, RuntimeError):
        warnings.warn('pyrender package is not installed correctly.')
        return img

    if not isinstance(colors, list):
        colors = [colors for _ in range(len(vertices))]
    colors = [color_val(c) for c in colors]

    depth_map = np.ones([H, W]) * np.inf
    output_img = img
    for idx in range(len(vertices)):
        color = colors[idx]
        color = [c / 255.0 for c in color]
        color.append(1.0)
        vert = vertices[idx]
        face = faces[idx]

        material = pyrender.MetallicRoughnessMaterial(metallicFactor=0.2,
                                                      alphaMode='OPAQUE',
                                                      baseColorFactor=color)

        mesh = trimesh.Trimesh(vert, face)
        rot = trimesh.transformations.rotation_matrix(np.radians(180),
                                                      [1, 0, 0])
        mesh.apply_transform(rot)
        mesh = pyrender.Mesh.from_trimesh(mesh, material=material)

        scene = pyrender.Scene(ambient_light=(0.5, 0.5, 0.5))
        scene.add(mesh, 'mesh')

        camera_pose = np.eye(4)
        camera = pyrender.IntrinsicsCamera(fx=focal_length[0],
                                           fy=focal_length[1],
                                           cx=camera_center[0],
                                           cy=camera_center[1],
                                           zfar=1e5)
        scene.add(camera, pose=camera_pose)

        light = pyrender.DirectionalLight(color=[1.0, 1.0, 1.0], intensity=1)
        light_pose = np.eye(4)

        light_pose[:3, 3] = np.array([0, -1, 1])
        scene.add(light, pose=light_pose)

        light_pose[:3, 3] = np.array([0, 1, 1])
        scene.add(light, pose=light_pose)

        light_pose[:3, 3] = np.array([1, 1, 2])
        scene.add(light, pose=light_pose)

        color, rend_depth = renderer.render(scene,
                                            flags=pyrender.RenderFlags.RGBA)

        valid_mask = (rend_depth < depth_map) * (rend_depth > 0)
        depth_map[valid_mask] = rend_depth[valid_mask]
        valid_mask = valid_mask[:, :, None]
        output_img = (valid_mask * color[:, :, :3] +
                      (1 - valid_mask) * output_img)

    return output_img
Exemplo n.º 7
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0.3,
                      bbox_color='green',
                      text_color='white',
                      thickness=2,
                      font_scale=0.5,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None,
                      image_id=0):
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)
    # results[image_id] = []

    # do tracking
    bbox_xyxy = bboxes[:,:4]
    cls_conf = bboxes[..., 4]
    w = bbox_xyxy[..., 2] - bbox_xyxy[..., 0]
    h = bbox_xyxy[..., 3] - bbox_xyxy[..., 1]
    bbox_xyxy[:, 2] = w
    bbox_xyxy[:, 3] = h

    index = 0
    outbb = np.copy(bbox_xyxy)
    dd = []
    for bbox, label in zip(bboxes, labels):
        if label != 0:
            dd.append(index)
        index += 1
    outbb = np.delete(outbb, dd, 0)
    cls_conf = np.delete(cls_conf, dd, 0)
    labels = np.delete(labels, dd, 0)

    if image_id ==1:
        for i in range(2):
            outputs = deepsort.update(outbb, cls_conf, img)
    else:
        outputs = deepsort.update(outbb, cls_conf, img)

    if len(outputs) > 0:
        bboxes = outputs[:, :4]
        ids = outputs[:, -1]

        for bbox, label, id in zip(bboxes, labels, ids):
            if label == 0:
                bbox_int = bbox.astype(np.int32)
                left_top = (bbox_int[0], bbox_int[1])
                right_bottom = (bbox_int[2], bbox_int[3])
                cv2.rectangle(
                    img, left_top, right_bottom, bbox_color, thickness=thickness)
                label_text = 'ID {}'.format(id)
                if len(bbox) > 4:
                    label_text += '|{:.02f}'.format(bbox[-1])

                _height_half = int((bbox_int[3]-bbox_int[1])/2)
                cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] + _height_half),
                            cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

                mybbox = bbox.astype(np.float).tolist()
                results.append((image_id, mybbox, id))



        if show:
            imshow(img, win_name, wait_time)
        if out_file is not None:
            imwrite(img, out_file)
def imshow_det_rbboxes_360(img,
                           bboxes,
                           labels,
                           class_names=None,
                           score_thr=0,
                           bbox_color='green',
                           text_color='red',
                           thickness=1,
                           font_scale=0.5,
                           show=True,
                           win_name='',
                           wait_time=0,
                           out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 5) or
            (n, 6).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 5 or bboxes.shape[1] == 6
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 6
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        xc, yc, w, h, ag, p = bbox.tolist()
        wx, wy = w / 2 * math.cos(ag), w / 2 * math.sin(ag)
        hx, hy = -h / 2 * math.sin(ag), h / 2 * math.cos(ag)
        p1 = (xc - wx - hx, yc - wy - hy)
        p2 = (xc + wx - hx, yc + wy - hy)
        p3 = (xc + wx + hx, yc + wy + hy)
        p4 = (xc - wx + hx, yc - wy + hy)
        ps = np.int0(np.array([p1, p2, p3, p4]))
        cv2.drawContours(img, [ps], -1, bbox_color, thickness=thickness)

        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)

        ##############
        # transfer 4 directions to 360
        ag = ag / math.pi * 180

        if "RUP" in label_text:
            ag += 90
        elif "RDOWN" in label_text:
            ag += 180
        elif "LDOWN" in label_text:
            ag += 270
        else:
            ag += 360

        if ag < 0:
            ag += 360
            print("negative:{},{}".format(ag, out_file))

        # Draw direction line
        p1_x, p1_y = xc, yc
        p2_x, p2_y = xc, yc - 80
        rotateMat = cv2.getRotationMatrix2D((p1_x, p1_y), -ag, 1)
        [[p1_x], [p1_y]] = np.dot(rotateMat, np.array([[p1_x], [p1_y], [1]]))
        [[p2_x], [p2_y]] = np.dot(rotateMat, np.array([[p2_x], [p2_y], [1]]))
        cv2.line(img, (int(p1_x), int(p1_y)), (int(p2_x), int(p2_y)),
                 text_color,
                 thickness=thickness)

        label_text = label_text.split('-')[0]
        ##############

        if len(bbox) > 5:
            label_text += '|{:.02f}, {:.02f}'.format(bbox[-1], ag)

        cv2.putText(img, label_text, (int(p1[0]), int(p1[1])),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
def imshow_det_rbboxes(img,
                       bboxes,
                       labels,
                       class_names=None,
                       score_thr=0,
                       bbox_color='green',
                       text_color='green',
                       thickness=1,
                       font_scale=0.5,
                       show=True,
                       win_name='',
                       wait_time=0,
                       out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 5) or
            (n, 6).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 5 or bboxes.shape[1] == 6
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 6
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox, label in zip(bboxes, labels):
        xc, yc, w, h, ag, p = bbox.tolist()
        wx, wy = w / 2 * math.cos(ag), w / 2 * math.sin(ag)
        hx, hy = -h / 2 * math.sin(ag), h / 2 * math.cos(ag)
        p1 = (xc - wx - hx, yc - wy - hy)
        p2 = (xc + wx - hx, yc + wy - hy)
        p3 = (xc + wx + hx, yc + wy + hy)
        p4 = (xc - wx + hx, yc - wy + hy)
        ps = np.int0(np.array([p1, p2, p3, p4]))
        cv2.drawContours(img, [ps], -1, bbox_color, thickness=thickness)
        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)
        if len(bbox) > 5:
            label_text += '|{:.02f}'.format(bbox[-1])
        cv2.putText(img, label_text, (int(p1[0]), int(p1[1])),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)
def imdraw_det_bboxes(img,
                      bboxes,
                      class_names,
                      car_model_dict,
                      camera_matrix,
                      trans_pred_world,
                      euler_angle,
                      color_lists,
                      score_thr=0,
                      bbox_color='green',
                      text_color='green',
                      thickness=1,
                      font_scale=0.5):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.
    """
    assert bboxes.ndim == 2
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img_original = img.copy()
    img = img_original[1480:, :, :]

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        trans_pred_world = trans_pred_world[inds, :]
        euler_angle = euler_angle[inds, :]
    assert len(bboxes) == len(trans_pred_world) == len(euler_angle)
    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    for bbox_idx in range(len(bboxes)):
        bbox = bboxes[bbox_idx]
        label_text = class_names[bbox_idx]
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        cv2.rectangle(img,
                      left_top,
                      right_bottom,
                      bbox_color,
                      thickness=thickness)
        if len(bbox) > 4:
            label_text += '|{:.02f}'.format(bbox[-1])
        cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 2),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

        # now we draw mesh
        vertices = np.array(car_model_dict[class_names[bbox_idx]]['vertices'])
        vertices[:, 1] = -vertices[:, 1]
        triangles = np.array(
            car_model_dict[class_names[bbox_idx]]['faces']) - 1

        t = trans_pred_world[bbox_idx]
        ea = euler_angle[bbox_idx]
        yaw, pitch, roll = ea[0], ea[1], ea[2]
        yaw, pitch, roll = -pitch, -yaw, -roll
        Rt = np.eye(4)
        Rt[:3, 3] = t
        Rt[:3, :3] = euler_to_Rot(yaw, pitch, roll).T
        Rt = Rt[:3, :]
        P = np.ones((vertices.shape[0], vertices.shape[1] + 1))
        P[:, :-1] = vertices
        P = P.T

        img_cor_points = np.dot(camera_matrix, np.dot(Rt, P))
        img_cor_points = img_cor_points.T
        img_cor_points[:, 0] /= img_cor_points[:, 2]
        img_cor_points[:, 1] /= img_cor_points[:, 2]

        color_mesh = np.int32(color_lists[bbox_idx][0])
        color_tuple = tuple([int(x) for x in color_mesh])
        for t in triangles:
            coord = np.array([
                img_cor_points[t[0]][:2], img_cor_points[t[1]][:2],
                img_cor_points[t[2]][:2]
            ],
                             dtype=np.int32)
            # This will draw the mask for segmenation
            # cv2.drawContours(mask_seg, np.int32([coord]), 0, (255, 255, 255), -1)
            coord[:, 1] -= 1480
            cv2.polylines(img, np.int32([coord]), 1, color=color_tuple)

    im_combime = img_original.copy()
    im_combime[1480:, :, :] = img
    return im_combime
Exemplo n.º 11
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0.3,
                      bbox_color='green',
                      text_color='white',
                      thickness=2,
                      font_scale=0.5,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None,
                      image_id=0):
    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)
    # results[image_id] = []

    # do tracking
    bbox_xyxy = bboxes[:,:4]
    cls_conf = bboxes[..., 4]
    w = bbox_xyxy[..., 2] - bbox_xyxy[..., 0]
    h = bbox_xyxy[..., 3] - bbox_xyxy[..., 1]
    bbox_xyxy[:, 2] = w
    bbox_xyxy[:, 3] = h


    # 去除label不是人的BBox
    index = 0
    outbb = np.copy(bbox_xyxy)
    dd = []
    for bbox, label in zip(bboxes, labels):
        if label != 0:
            dd.append(index)
        index += 1
    outbb = np.delete(outbb, dd, 0)
    cls_conf = np.delete(cls_conf, dd, 0)
    labels = np.delete(labels, dd, 0)

    # 去除BBox过小
    BBox_area_sixze = w * h
    index_filter = 0
    outbb_filter = np.copy(outbb)
    dd_filter = []
    for bbox, size in zip(outbb_filter, BBox_area_sixze):
        if size <= 200:
            dd_filter.append(index_filter)
        index_filter += 1
    outbb_filter = np.delete(outbb_filter, dd_filter, 0)
    cls_conf = np.delete(cls_conf, dd_filter, 0)
    labels = np.delete(labels, dd_filter, 0)

    # 去除BBox过小
    BBox_area_h = h
    index_filter = 0
    ddd_filter = []
    for bbox, size in zip(outbb_filter, BBox_area_h):
        if size <= 35:
            ddd_filter.append(index_filter)
        index_filter += 1
    outbb_filter = np.delete(outbb_filter, ddd_filter, 0)
    cls_conf = np.delete(cls_conf, ddd_filter, 0)
    labels = np.delete(labels, ddd_filter, 0)



    # if image_id ==1:
    #     for i in range(4):
    #         outputs = tracker.update(img, outbb_filter, cls_conf)
    # else:
    #     outputs = tracker.update(img, outbb_filter, cls_conf)
    outputs = tracker.update(img, outbb_filter, cls_conf)

    online_tlwhs = []
    online_ids = []
    for t in outputs:
        online_tlwhs.append(t.tlwh)
        online_ids.append(t.track_id)


    if len(online_ids) > 0:
        bboxes = online_tlwhs
        ids = online_ids

        for bbox, label, id, conf in zip(bboxes, labels, ids, cls_conf):
            if label == 0:
                bbox_int = bbox.astype(np.int32)
                left_top = (bbox_int[0], bbox_int[1])
                right_bottom = (bbox_int[0]+bbox_int[2], bbox_int[1]+bbox_int[3])
                cv2.rectangle(
                    img, left_top, right_bottom, bbox_color, thickness=thickness)
                label_text = 'ID{} cnf{:.2}'.format(id, conf)
                if len(bbox) > 4:
                    label_text += '|{:.02f}'.format(bbox[-1])

                _height_half = int((bbox_int[3]-bbox_int[1])/2)
                cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] + _height_half),
                            cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)

                mybbox = bbox.astype(np.float).tolist()
                results.append((image_id, mybbox, id))
                # results.append((image_id, mybbox, id, conf))


        if show:
            imshow(img, win_name, wait_time)
        if out_file is not None:
            imwrite(img, out_file)
Exemplo n.º 12
0
def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      class_names=None,
                      score_thr=0,
                      bbox_color='green',
                      text_color='black',
                      thickness=4,
                      font_scale=1.1,
                      show=True,
                      win_name='',
                      wait_time=0,
                      out_file=None,
                      dataset=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.
    """

    # import random
    # color = "%06x" % random.randint(0, 0xFFFFFF)
    # from splits import COCO_ALL_CLASSES
    # color_map = {label: (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for label in COCO_ALL_CLASSES}

    assert bboxes.ndim == 2
    assert labels.ndim == 1
    assert bboxes.shape[0] == labels.shape[0]
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5
    img = imread(img)
    unseen_labels = get_unseen_class_labels(dataset, '177_23')

    if score_thr > 0:
        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        inds = scores > score_thr
        bboxes = bboxes[inds, :]
        labels = labels[inds]

    bbox_color = color_val(bbox_color)
    text_color = color_val(text_color)

    # import pdb; pdb.set_trace()

    for bbox, label in zip(bboxes, labels):
        bbox_int = bbox.astype(np.int32)
        left_top = (bbox_int[0], bbox_int[1])
        right_bottom = (bbox_int[2], bbox_int[3])
        label_text = class_names[
            label] if class_names is not None else 'cls {}'.format(label)

        if label_text in unseen_labels:
            bbox_color = color_val('red')
            # print(label_text)
        else:
            bbox_color = color_val('green')

        print(bbox_color, label_text)
        # bbox_color = color_map[label_text] if color_map is not None else bbox_color
        cv2.rectangle(img,
                      left_top,
                      right_bottom,
                      bbox_color,
                      thickness=thickness)

        label_text = label_text.title().replace('_', ' ')
        # if len(bbox) > 4:
        #     label_text += ': {}'.format(int(100*bbox[-1]))

        # import pdb; pdb.set_trace()
        txt_thickness = 4
        font = cv2.FONT_HERSHEY_SIMPLEX
        (text_width, text_height) = cv2.getTextSize(label_text,
                                                    font,
                                                    fontScale=font_scale,
                                                    thickness=txt_thickness)[0]
        text_offset_x = bbox_int[0]
        text_offset_y = bbox_int[1] - 2
        box_coords = ((text_offset_x, text_offset_y),
                      (text_offset_x + text_width + 2,
                       text_offset_y - text_height - 2))
        rectangle_bgr = (255, 255, 255)
        cv2.rectangle(img, box_coords[0], box_coords[1], rectangle_bgr,
                      cv2.FILLED)

        cv2.putText(img,
                    label_text, (bbox_int[0], bbox_int[1] - 2),
                    font,
                    font_scale,
                    text_color,
                    thickness=txt_thickness)

    if show:
        imshow(img, win_name, wait_time)
    if out_file is not None:
        imwrite(img, out_file)