0

Introduction

I am trying to make a small tool for classifying images using the ipywidgets in a Jupyter Notebook, but I am having some trouble aligning the classes and the images. Do you have any suggestion how to fix this.

What I did

import ipywidgets as widgets
from IPython.display import display
import glob

# My images
image_paths = glob.glob("./images/*.png")

# Display image
def display_image(path):    
    file = open(path, "rb")
    image = file.read()
    return widgets.Image(
                value=image,
                format='png',
                width=700,
                height=700,
            )

# Dropdown
def create_dropdown():
    return widgets.Dropdown(
        options=["1","2","3","4","5","6","7","8","9","10"],
        value='5',
        description='Category:',
        disabled=False
    )

# Creating widgets
input_dropdown = create_dropdown()
button = widgets.Button(description="Submit")
output_image = widgets.Image()
output_image.value = display_image(image_paths[-1]).value

# Define function to bind value of the input to the output variable 
def bind_input_to_output(sender):
    image_path = image_paths[-1]
    image_score = input_dropdown.value
    next_image_path = image_paths.pop()
    print(image_score, image_path)
    output_image.value = display_image(next_image_path).value
    
# Tell the text input widget to call bind_input_to_output() on submit
button.on_click(bind_input_to_output)

# Displaying widgets
display(output_image, input_dropdown, button)

Results

With the above code I end up categorising the upcoming picture, but I really don't understand why. It seems the widgets does not update the image the first time I press the button.

2
  • Hard to test this without the input file of pngs, can you share? Commented Feb 11, 2021 at 8:40
  • It was just a bunch of random images, but I will update post to include downloading some files. Commented Feb 11, 2021 at 14:30

1 Answer 1

1
def bind_input_to_output(sender):
    image_path = image_paths.pop()
    image_score = input_dropdown.value
    next_image_path = image_paths[-1]
    print(image_score, image_path)
    output_image.value = display_image(next_image_path).value

pop first and give next filename at last item

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.