xinwei89 commited on
Commit
311bc12
1 Parent(s): d812008

modify visualizer

Browse files
Files changed (2) hide show
  1. app.py +2 -9
  2. backend.py +12 -1
app.py CHANGED
@@ -3,18 +3,11 @@ from backend import visualize_image
3
 
4
  # gradio inputs
5
  image_input = gr.inputs.Image(type="pil", label="Input Image")
6
- color_mode_select = gr.inputs.Radio(["Black/white", "Random", "Segmentation"])
7
- mode_dropdown = gr.inputs.Dropdown(["Trees", "Buildings", "Both"])
8
 
9
  tree_threshold_slider = gr.inputs.Slider(0, 1, 0.1, 0.7, label='Set confidence threshold "%" for trees')
10
  building_threshold_slider = gr.inputs.Slider(0, 1, 0.1, 0.7, label='Set confidence threshold "%" for buildings')
11
- if mode_dropdown == "Trees":
12
- tree_threshold_slider.enable()
13
- if mode_dropdown == "Buildings":
14
- building_threshold_slider.enable()
15
- if mode_dropdown == "Both":
16
- tree_threshold_slider.enable()
17
- building_threshold_slider.enable()
18
 
19
  # gradio outputs
20
  output_image = gr.outputs.Image(type="pil", label="Output Image")
 
3
 
4
  # gradio inputs
5
  image_input = gr.inputs.Image(type="pil", label="Input Image")
6
+ color_mode_select = gr.inputs.Radio(["Black/white", "Random", "Segmentation"], label="Color Mode")
7
+ mode_dropdown = gr.inputs.Dropdown(["Trees", "Buildings", "Both"], label="Detection Mode")
8
 
9
  tree_threshold_slider = gr.inputs.Slider(0, 1, 0.1, 0.7, label='Set confidence threshold "%" for trees')
10
  building_threshold_slider = gr.inputs.Slider(0, 1, 0.1, 0.7, label='Set confidence threshold "%" for buildings')
 
 
 
 
 
 
 
11
 
12
  # gradio outputs
13
  output_image = gr.outputs.Image(type="pil", label="Output Image")
backend.py CHANGED
@@ -81,10 +81,21 @@ def visualize_image(im, mode="BOTH", tree_threshold=0.7, building_threshold=0.7,
81
  building_instances = segment_building(im, building_threshold)
82
  instances = Instances.cat([tree_instances, building_instances])
83
 
 
 
 
 
 
 
84
  visualizer = Visualizer(im[:, :, ::-1],
 
85
  scale=0.5,
86
  instance_mode=color_mode)
87
-
 
 
 
 
88
  output_image = visualizer.draw_instance_predictions(instances)
89
 
90
  return Image.fromarray(output_image.get_image()[:, :, ::-1])
 
81
  building_instances = segment_building(im, building_threshold)
82
  instances = Instances.cat([tree_instances, building_instances])
83
 
84
+ # visualizer = Visualizer(im[:, :, ::-1],
85
+ # scale=0.5,
86
+ # instance_mode=color_mode)
87
+
88
+ metadata = MetadataCatalog.get("your_model_metadata")
89
+ category_names = metadata.get("thing_classes")
90
  visualizer = Visualizer(im[:, :, ::-1],
91
+ metadata=metadata,
92
  scale=0.5,
93
  instance_mode=color_mode)
94
+ # in the visualizer, add category label names to detected instances
95
+ for instance in instances:
96
+ label = category_names[instance["category_id"]]
97
+ visualizer.draw_text(label, instance["bbox"][:2])
98
+
99
  output_image = visualizer.draw_instance_predictions(instances)
100
 
101
  return Image.fromarray(output_image.get_image()[:, :, ::-1])