[Python] offset when visualizing masks from open-images-v7 loaded using fiftyone in python

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    This is how I'm getting the dataset:

    train_dataset = fiftyone.zoo.load_zoo_dataset(
    "open-images-v7",
    split="train",
    label_types=["segmentations"],
    classes=["target class"],
    )


    This is how I'm processing and visualizing the mask for a random sample:

    sample = train_dataset.take(1).first()

    for detection in sample.ground_truth.detections:
    if detection.label == 'target class':
    bbox = detection.bounding_box
    mask = detection.mask.astype(np.float32)
    break

    img = tf.io.read_file(sample["filepath"])
    img = tf.image.decode_jpeg(img, channels=3)

    x_center, y_center, w, h = bbox
    image_height = img.shape[0]
    image_width = img.shape[1]
    # Convert to absolute coordinates
    x_center_px = x_center * image_width
    y_center_px = y_center * image_height
    w_px = w * image_width
    h_px = h * image_height
    # Convert [x_center, y_center, w, h] → [x_min, y_min, x_max, y_max]
    x_min = x_center_px - (w_px / 2)
    y_min = y_center_px - (h_px / 2)
    x_max = x_center_px + (w_px / 2)
    y_max = y_center_px + (h_px / 2)

    full_mask = np.zeros((image_height, image_width), dtype=np.uint8)
    resized_mask = cv2.resize(mask, (int(w_px), int(h_px)), interpolation=cv2.INTER_NEAREST)
    x_min, y_min = int(x_min), int(y_min)
    full_mask[y_min:y_min+resized_mask.shape[0], x_min:x_min+resized_mask.shape[1]] = resized_mask

    plt.imshow(img)
    plt.imshow(full_mask, alpha=0.5, cmap='jet')
    plt.show()


    Problem:

    When running above code, either it runs successfully, but in the output image, the mask always has offset and is located in upper left of the actual place, while preserving the correct size and shape.

    Or, an error rises:

    ---> 29 full_mask[y_min:y_min+resized_mask.shape[0], x_min:x_min+resized_mask.shape[1]] = resized_mask
    30
    31 plt.imshow(img)

    ValueError: could not broadcast input array from shape (107,512) into shape (107,0)

    Continue reading...

Compartilhe esta Página