Пример #1
0
def experiment2(annotation_file, detector, outputs, exp_name):
    def getdata(transforms):
        dataset = Dataset(annotation_file, transforms)
        return dataset

    transforms = [[], [FlipImage()], [BlurImage(1)], [RescaleImage(2.0)],
                  [RescaleImage(0.5)], [RotateImage(-90)], [RotateImage(45)]]

    rows = 2
    columns = 4
    fig = plt.figure()
    axes = []
    for i, transform in enumerate(transforms):
        dataset = getdata(transform)
        img = dataset[3]["image"]
        preds = detector(img)
        img = img * 255
        img = np.array(img.transpose(1, 2, 0), dtype=np.uint8)
        print(f"Annotating Image {exp_name} {i}")
        img = plot_boxes(img, preds)
        img.save(f"{outputs}/{exp_name}_{i}.jpg")
        axes.append(fig.add_subplot(rows, columns, i + 1))
        axes[-1].set_title(f"Subplot {i+1}")
        plt.imshow(img)
    fig.tight_layout()
    plt.savefig(f"{outputs}/{exp_name}.jpg")
def predictor(image, image_dict, detector):
    image_dict['bboxes'] = []
    pred_boxes, pred_class, pred_score = detector(
        np.transpose(np.array(image), (2, 0, 1)) / 255)
    if len(pred_score) > 5:  # top 5 confident predictions
        pred_score = pred_score[:5]
        pred_boxes = pred_boxes[:5]
        pred_class = pred_class[:5]
    for k in range(len(pred_score)):
        my_dict = {}
        my_dict['bbox'] = list(chain.from_iterable(pred_boxes[k]))
        for j in range(len(my_dict['bbox'])):
            my_dict['bbox'][j] = int(my_dict['bbox'][j])
        my_dict['category'] = pred_class[k]
        image_dict['bboxes'].append(my_dict)
    return plot_boxes(image_dict=image_dict, image=image)
Пример #3
0
def experiment(annotation_file, detector, transforms, outputs):
    '''
        Function to perform the desired experiments

        Arguments:
        annotation_file: Path to annotation file
        detector: The object detector
        transforms: List of transformation classes
        outputs: path of the output folder to store the images
    '''

    # Create the instance of the dataset.
    dataset = Dataset(annotation_file, transforms)

    for i, data in enumerate(dataset):
        img = data["image"]
        preds = detector(img)
        img = img * 255
        img = np.array(img.transpose(1, 2, 0), dtype=np.uint8)
        print(f"Annotating Image {i}")
        img = plot_boxes(img, preds)
        img.save(f"{outputs}/{i}.jpg")
def experiment(annotation_file, detector, transforms, outputs):
    '''
        Function to perform the desired experiments

        Arguments:
        annotation_file: Path to annotation file
        detector: The object detector
        transforms: List of transformation classes
        outputs: path of the output folder to store the images
    '''

    #Create the instance of the dataset.
    d = Dataset(annotation_file, transforms)
    #Iterate over all data items.
    for i in range(len(d)):
        image, image_dict = d[i]

        #Get the predictions from the detector.
        pred_boxes, pred_class, pred_score = detector(
            np.transpose(np.array(image), (2, 0, 1)) / 255)

        if len(pred_score) > 5:  # top 5 confident predictions
            pred_score = pred_score[:5]
            pred_boxes = pred_boxes[:5]
            pred_class = pred_class[:5]

        image_dict['bboxes'] = []
        for k in range(len(pred_score)):
            my_dict = {}
            my_dict['bbox'] = list(chain.from_iterable(pred_boxes[k]))
            for j in range(len(my_dict['bbox'])):
                my_dict['bbox'][j] = int(my_dict['bbox'][j])
            my_dict['category'] = pred_class[k]
            image_dict['bboxes'].append(my_dict)

    #Draw the boxes on the image and save them.
        image = plot_boxes(location='./output/',
                           image_dict=image_dict,
                           image=image,
                           saver=1)

    #Do the required analysis experiments.
    my_image = Image.open('./data/imgs/9.jpg')
    x, y = my_image.size

    # no transformation
    d = Dataset(annotation_file, transforms=[])
    my_image, my_image_dict = d[9]
    plt.subplot(2, 4, 1)
    plt.title('Original Image')
    plt.imshow(predictor(my_image, my_image_dict, detector))

    # horizontal flipping
    d = Dataset(annotation_file, transforms=[FlipImage()])
    image, image_dict = d[9]
    plt.subplot(2, 4, 2)
    plt.title('Horizontally Flipped Image')
    plt.imshow(predictor(image, image_dict, detector))

    # blurring
    d = Dataset(annotation_file, transforms=[BlurImage(radius=3)])
    image, image_dict = d[9]
    plt.subplot(2, 4, 3)
    plt.title('Blurred Image (Radius = 3)')
    plt.imshow(predictor(image, image_dict, detector))

    # 2x rescaling
    d = Dataset(annotation_file, transforms=[RescaleImage((2 * y, 2 * x))])
    image, image_dict = d[9]
    plt.subplot(2, 4, 4)
    plt.title('2x Rescaled Image')
    plt.imshow(predictor(image, image_dict, detector))

    # 0.5x rescaling
    d = Dataset(annotation_file, transforms=[RescaleImage((y // 2, x // 2))])
    image, image_dict = d[9]
    plt.subplot(2, 4, 5)
    plt.title('0.5x Rescaled Image')
    plt.imshow(predictor(image, image_dict, detector))

    # 90 degree rotation to right
    d = Dataset(annotation_file, transforms=[RotateImage(360 - 90)])
    image, image_dict = d[9]
    plt.subplot(2, 4, 6)
    plt.title('90 degree Rotated Image')
    plt.imshow(predictor(image, image_dict, detector))

    # 45 degree rotation to left
    d = Dataset(annotation_file, transforms=[RotateImage(45)])
    image, image_dict = d[9]
    plt.subplot(2, 4, 7)
    plt.title('-45 degree Rotated Image')
    plt.imshow(predictor(image, image_dict, detector))

    plt.show()
Пример #5
0
def experiment(annotation_file, detector, transforms = None, outputs = None):
    '''
        Function to perform the desired experiments

        Arguments:
        annotation_file: Path to annotation file
        detector: The object detector
        transforms: List of transformation classes
        outputs: path of the output folder to store the images
    '''

    #Create the instance of the dataset.
    d = Dataset(annotation_file, transforms)

    #Iterate over all data items.
    for i in range(len(d)):

    #Get the predictions from the detector.
        pred = detector(d[i]['image'])

    #Draw the boxes on the image and save them.
        plot_boxes(outputs+str(i)+".png",d[i]['image'],pred)

    #Do the required analysis experiments.
    fp = "./outputs/"
    img1 = d[1]['image']
    pred = detector(img1)
    n_img = plot_boxes(fp + "original.png", img1, pred)
    plt.subplot(2,4,1)
    plt.imshow(n_img)
    plt.title("Original")

    d = Dataset(annotation_file,[FlipImage('horizontal')])
    img2 = d[1]['image']    
    pred = detector(img2)
    n_img = plot_boxes(fp + "flip.png", img2, pred)
    plt.subplot(2,4,2)
    plt.imshow(n_img)
    plt.title("Horizontally flip")

    d = Dataset(annotation_file,[BlurImage(3)])
    img3 = d[1]['image']    
    pred = detector(img3)
    n_img = plot_boxes(fp+"blur.png", img3, pred)
    plt.subplot(2,4,3)
    plt.imshow(n_img)
    plt.title("Blurred")

    d = Dataset(annotation_file,[RescaleImage((2*img3.shape[2],2*img3.shape[1]))])
    img4 = d[1]['image']
    pred = detector(img4)
    n_img = plot_boxes(fp + "2x.png", img4, pred)
    plt.subplot(2,4,4)
    plt.imshow(n_img)
    plt.title("2X")

    d = Dataset(annotation_file,[RescaleImage((round(0.5*img3.shape[2]),round(0.5*img3.shape[1])))])   
    img5 = d[1]['image']
    pred = detector(img5)
    n_img = plot_boxes(fp + "halfx.png", img5, pred)
    plt.subplot(2,4,5)
    plt.imshow(n_img)
    plt.title("0.5X")

    d = Dataset(annotation_file,[RotateImage(90)])
    img6 = d[1]['image']    
    pred = detector(img6)
    n_img = plot_boxes(fp + "90right.png", img6, pred)
    plt.subplot(2,4,6)
    plt.imshow(n_img)
    plt.title("90degree right")

    d = Dataset(annotation_file,[RotateImage(-45)])
    img7 = d[1]['image']    
    pred = detector(img7)
    n_img = plot_boxes(fp + "45left.png", img7, pred)
    plt.subplot(2,4,7)
    plt.imshow(n_img)
    plt.title("45degree left")
    plt.show()
def experiment(annotation_file, detector, transforms, outputs):
    '''
        Function to perform the desired experiments

        Arguments:
        annotation_file: Path to annotation file
        detector: The object detector
        transforms: List of transformation classes
        outputs: path of the output folder to store the images
    '''

    # Create the instance of the dataset.
    data_obj = Dataset(annotation_file)

    img_list = list()
    fig = plt.figure()
    titles = [
        "Original Image", "Horizontally Flipped Image", "Blurred Image",
        "2x Rescaled Image", "0.5x Rescaled Image",
        "90 Degree Right Rotated Image", "45 Degree Left Rotated Image"
    ]

    # Iterate over all data items.
    for i, data in enumerate(data_obj):
        print("Plotting boundary boxes for image " + str(i))

        # Get the predictions from the detector.
        image_array = data["image"]
        pred_boxes, pred_class, pred_score = detector(image_array)
        predictions = {
            "pred_boxes": pred_boxes,
            "pred_class": pred_class,
            "pred_score": pred_score
        }
        image_array *= 255.0
        image_array = np.array(image_array.transpose(1, 2, 0), dtype=np.uint8)

        # Draw the boxes on the image and save them.
        image = plot_boxes(image_array, predictions, outputs,
                           str(i) + "_pred.jpg")
        if (i == 8):
            img_list.append(fig.add_subplot(2, 4, 1))
            img_list[-1].set_title(titles[0])
            plt.imshow(image)
    print()

    # Do the required analysis experiments.
    print("Performing analysis task (a)"
          )  # this is already done in the upper loop
    for i, obj in enumerate(transforms):
        d_obj = Dataset(annotation_file, [obj])
        print("Performing analysis task (" + chr(98 + i) + ")")
        image_array = d_obj[8]["image"]
        pred_boxes, pred_class, pred_score = detector(image_array)
        predictions = {
            "pred_boxes": pred_boxes,
            "pred_class": pred_class,
            "pred_score": pred_score
        }
        image_array *= 255.0
        image_array = np.array(image_array.transpose(1, 2, 0), dtype=np.uint8)
        image = plot_boxes(image_array, predictions, outputs,
                           "8_" + chr(98 + i) + ".jpg")
        img_list.append(fig.add_subplot(2, 4, i + 2))
        img_list[-1].set_title(titles[i + 1])
        plt.imshow(image)
    print()

    plt.show()