dwb2023 commited on
Commit
fcb6651
·
verified ·
1 Parent(s): 625d75d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -5,12 +5,22 @@ import numpy as np
5
 
6
  # Load the dataset
7
  dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test")
8
- # print(f"Dataset loaded successfully. Number of images: {len(dataset)}")
9
 
10
- def draw_annotations(index):
 
 
 
 
 
 
 
 
 
 
 
11
  try:
12
  # Fetch the image and annotations from the dataset
13
- record = dataset[index]
14
 
15
  # Convert image to PIL Image if it's a numpy array
16
  if isinstance(record['image'], np.ndarray):
@@ -43,10 +53,10 @@ def draw_annotations(index):
43
  info += f"Segmentation: {segmentation}\n"
44
  info += f"Area: {area:.2f}"
45
 
46
- return img, info
47
  except Exception as e:
48
  print(f"Error processing image at index {index}: {e}")
49
- return Image.new('RGB', (300, 300), color='gray'), f"Error loading image information: {str(e)}"
50
 
51
  # Create Gradio interface
52
  with gr.Blocks() as demo:
@@ -57,13 +67,20 @@ with gr.Blocks() as demo:
57
  with gr.Column(scale=1):
58
  image_output = gr.Image(label="Annotated Image")
59
  with gr.Column(scale=1):
60
- image_index = gr.Slider(minimum=0, maximum=len(dataset)-1, step=1, value=0, label="Image ID Slider")
 
61
  info_output = gr.Textbox(label="Image Information", lines=10)
62
 
63
- # Update image and info when slider changes
64
- image_index.change(draw_annotations, inputs=image_index, outputs=[image_output, info_output])
 
 
 
 
 
 
65
 
66
  # Display initial image and info
67
- demo.load(draw_annotations, inputs=image_index, outputs=[image_output, info_output])
68
 
69
- demo.launch(debug=True)
 
5
 
6
  # Load the dataset
7
  dataset = load_dataset("dwb2023/brain-tumor-image-dataset-semantic-segmentation", split="test")
 
8
 
9
+ # Function to filter dataset based on category ID
10
+ def filter_dataset_by_category(category_id):
11
+ filtered_indices = [i for i, record in enumerate(dataset) if record["category_id"] == category_id]
12
+ return filtered_indices
13
+
14
+ # Function to draw annotations
15
+ def draw_annotations(index, category_id):
16
+ filtered_indices = filter_dataset_by_category(category_id)
17
+
18
+ if index >= len(filtered_indices):
19
+ index = 0
20
+
21
  try:
22
  # Fetch the image and annotations from the dataset
23
+ record = dataset[filtered_indices[index]]
24
 
25
  # Convert image to PIL Image if it's a numpy array
26
  if isinstance(record['image'], np.ndarray):
 
53
  info += f"Segmentation: {segmentation}\n"
54
  info += f"Area: {area:.2f}"
55
 
56
+ return img, info, len(filtered_indices) - 1
57
  except Exception as e:
58
  print(f"Error processing image at index {index}: {e}")
59
+ return Image.new('RGB', (300, 300), color='gray'), f"Error loading image information: {str(e)}", len(filtered_indices) - 1
60
 
61
  # Create Gradio interface
62
  with gr.Blocks() as demo:
 
67
  with gr.Column(scale=1):
68
  image_output = gr.Image(label="Annotated Image")
69
  with gr.Column(scale=1):
70
+ category_id_dropdown = gr.Dropdown(choices=[1, 2], value=1, label="Category ID")
71
+ image_index = gr.Slider(minimum=0, maximum=0, step=1, value=0, label="Image ID Slider")
72
  info_output = gr.Textbox(label="Image Information", lines=10)
73
 
74
+ def update_slider(category_id):
75
+ _, _, max_index = draw_annotations(0, category_id)
76
+ return gr.Slider.update(maximum=max_index)
77
+
78
+ # Update image and info when slider or category changes
79
+ category_id_dropdown.change(update_slider, inputs=category_id_dropdown, outputs=image_index)
80
+ category_id_dropdown.change(draw_annotations, inputs=[image_index, category_id_dropdown], outputs=[image_output, info_output, image_index])
81
+ image_index.change(draw_annotations, inputs=[image_index, category_id_dropdown], outputs=[image_output, info_output, image_index])
82
 
83
  # Display initial image and info
84
+ demo.load(draw_annotations, inputs=[image_index, category_id_dropdown], outputs=[image_output, info_output, image_index])
85
 
86
+ demo.launch(debug=True)