Skip to content

Inference on single Image  #3

Description

@sanariaz154

Hi, I am trying to run the following code to get the detection boxes from the image but I am always getting an empty list for contours and boxes.
please can anyone help me with what i am doing wrong or how can I do it

def image_loader(image_name):
    """load image, returns  tensor"""
    imsize = 512
    image = Image.open(image_name).convert('RGB')
    loader= transforms.Compose([
    #transforms.Scale(imsize),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225]),
    ]) 
    image = loader(image).float()
    image = image.unsqueeze(1) 
    return image 

def get_contours_and_boxes(binarized_map, min_area=10):
    """

    :param binarized_map: np.array of np.uint8
    :param min_area:
    :return:
    """
    assert binarized_map.dtype == np.uint8
    contours, _ = cv2.findContours(
        binarized_map,
        mode=cv2.RETR_EXTERNAL,
        method=cv2.CHAIN_APPROX_SIMPLE
    )

    contours = list(filter(lambda cnt: cv2.contourArea(cnt) > min_area, contours))
    rects = [cv2.minAreaRect(cnt) for cnt in contours]
    boxes = [cv2.boxPoints(rect).reshape((-1, 2)) for rect in rects]
    assert len(boxes) == len(contours)

    return contours, boxes

ckpt_path="/barcode_detection_benchmark/resnet18_unet/ZVZ-real/best_full_init_synth.pth"
model = ZharkovDilatedNet()
state_dict = torch.load(ckpt_path)
model.load_state_dict(state_dict['model_state_dict'],strict=False)
model.eval()
# model loaded successfully

image_path="image_1.png"    #example image from zvz_real dataset
image = image_loader(image_path)

heatmap = model(image)[:,0]
heatmap = torch.sigmoid(heatmap).detach().cpu().numpy()
d_binarized_map = (heatmap[:, 0] > 0.5).astype(np.uint8)
cont, box= get_contours_and_boxes(d_binarized_map)
print(cont, box)  # getting empty list 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions