I’m trying to make rectangular annotations on images but I’m unable to put annotation as I’m running into attribute erros as some modules are not recognized, the file directory has images in .jpeg and I want to make a interface on jupyter notebook to make selection on images or you can say rectangular annotations ,
the code is
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
from IPython.display import display, clear_output
import ipywidgets as widgets
import os
class ImageAnnotator:
def _init_(self):
self.image_dir = "C:\\annotated_images"
self.images = []
self.current_index = 0
self.rect_coords = []
self.rect_active = False
self.fig, self.ax = plt.subplots()
self.ax.axis('off')
self.canvas = self.fig.canvas
self.load_button = widgets.Button(description="Load Images")
self.load_button.on_click(self.load_images)
self.prev_button = widgets.Button(description="Previous (P)")
self.prev_button.on_click(self.previous_image)
self.next_button = widgets.Button(description="Next (N)")
self.next_button.on_click(self.next_image)
self.redo_button = widgets.Button(description="Redo (R)")
self.redo_button.on_click(self.redo_annotation)
self.enable_annotation_button = widgets.Button(description="Enable Annotation")
self.enable_annotation_button.on_click(self.enable_annotation)
self.file_path_entry = widgets.Text(value=self.image_dir, description="File Path:", disabled=True)
self.output = widgets.Output()
self.display_widgets()
def display_widgets(self):
display(self.file_path_entry)
display(self.load_button)
display(widgets.HBox([self.prev_button, self.next_button, self.redo_button]))
display(self.enable_annotation_button)
display(self.output)
def load_images(self, _):
self.images = [file for file in os.listdir(self.image_dir) if file.endswith('.jpg') or file.endswith('.jpeg')]
if self.images:
self.current_index = 0
self.show_image()
def show_image(self):
image_path = os.path.join(self.image_dir, self.images[self.current_index])
self.file_path_entry.value = image_path
self.image = Image.open(image_path)
self.ax.clear()
self.ax.imshow(self.image)
self.canvas.draw()
def previous_image(self, _):
if self.current_index > 0:
self.current_index -= 1
self.show_image()
def next_image(self, _):
if self.current_index < len(self.images) - 1:
self.current_index += 1
self.show_image()
def redo_annotation(self, _):
self.rect_coords = []
self.rect_active = False
self.ax.clear()
self.ax.imshow(self.image)
self.canvas.draw()
def enable_annotation(self, _):
self.rect_coords = []
self.rect_active = True
def on_canvas_click(self, event):
if not self.rect_active:
return
x, y = event.xdata, event.ydata
self.rect_coords.append((x, y))
if len(self.rect_coords) == 2:
self.draw_rectangle()
def draw_rectangle(self):
x1, y1 = self.rect_coords[0]
x2, y2 = self.rect_coords[1]
width = abs(x2 - x1)
height = abs(y2 - y1)
rect = patches.Rectangle((x1, y1), width, height, linewidth=2, edgecolor='r', facecolor='none')
self.ax.add_patch(rect)
self.rect_coords = []
self.rect_active = False
self.canvas.draw()
# Instantiate and display the ImageAnnotator
annotator = ImageAnnotator()
annotator.canvas.mpl_connect('button_press_event', annotator.on_canvas_click)
display(annotator.fig.canvas)
I wanted an interactive rectangular annotator but I’m running into loads of errors
1