VietCat commited on
Commit
f98ab8d
·
1 Parent(s): 6468f09

Remove zoom feature - keep interface clean and simple

Browse files

- Removed all zoom sliders and zoomed image components
- Removed apply_zoom functions and global image storage
- Back to core functionality: upload image, adjust threshold, detect
- Auto-detection on image upload and threshold change
- Manual detect button and clear button still available

Files changed (1) hide show
  1. app.py +7 -61
app.py CHANGED
@@ -6,16 +6,10 @@ from model import TrafficSignDetector
6
  # Load the detector
7
  detector = TrafficSignDetector('config.yaml')
8
 
9
- # Store original images in memory
10
- original_output_image = None
11
- original_preprocessed_image = None
12
-
13
  def detect_traffic_signs(image, confidence_threshold):
14
  """
15
  Process the uploaded image and return the image with detected signs.
16
  """
17
- global original_output_image, original_preprocessed_image
18
-
19
  print(f"Received image type: {type(image)}")
20
  if hasattr(image, 'convert'):
21
  image = np.array(image)
@@ -32,31 +26,12 @@ def detect_traffic_signs(image, confidence_threshold):
32
  result_image = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
33
  preprocessed_image = cv2.cvtColor(preprocessed_image, cv2.COLOR_BGR2RGB)
34
 
35
- # Store originals
36
- original_output_image = result_image.copy()
37
- original_preprocessed_image = preprocessed_image.copy()
38
-
39
  return result_image, preprocessed_image
40
 
41
- def apply_zoom(image, zoom_level):
42
- """Resize image based on zoom level"""
43
- if image is None:
44
- return None
45
-
46
- zoom_factor = zoom_level / 100.0
47
- h, w = image.shape[:2]
48
- new_w = int(w * zoom_factor)
49
- new_h = int(h * zoom_factor)
50
-
51
- if new_w > 0 and new_h > 0:
52
- zoomed = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
53
- return zoomed
54
- return image
55
-
56
  # Create Gradio interface
57
  with gr.Blocks(title="Traffic Sign Detector") as demo:
58
  gr.Markdown("# Traffic Sign Detector")
59
- gr.Markdown("Upload an image to detect traffic signs using YOLOv8.")
60
 
61
  with gr.Row():
62
  input_image = gr.Image(label="Upload Image", type="pil")
@@ -71,28 +46,15 @@ with gr.Blocks(title="Traffic Sign Detector") as demo:
71
  maximum=0.9,
72
  value=0.30,
73
  step=0.01,
74
- label="Confidence Threshold"
 
75
  )
76
 
77
  with gr.Row():
78
  detect_btn = gr.Button("Detect Traffic Signs", variant="primary")
79
  reset_btn = gr.Button("Clear")
80
 
81
- gr.Markdown("### Zoom Inspector")
82
- gr.Markdown("**Note:** Use these sliders to zoom in and inspect details. Scroll within the image view.")
83
-
84
- with gr.Row():
85
- with gr.Column(scale=1):
86
- gr.Markdown("**Detected Image**")
87
- zoom_output = gr.Slider(50, 300, value=100, step=10, label="Zoom %")
88
- output_zoomed = gr.Image(label="", interactive=False, show_download_button=False)
89
-
90
- with gr.Column(scale=1):
91
- gr.Markdown("**Preprocessed Image**")
92
- zoom_preprocessed = gr.Slider(50, 300, value=100, step=10, label="Zoom %")
93
- preprocessed_zoomed = gr.Image(label="", interactive=False, show_download_button=False)
94
-
95
- # Auto-detect on upload
96
  input_image.change(
97
  fn=detect_traffic_signs,
98
  inputs=[input_image, confidence_threshold],
@@ -116,26 +78,10 @@ with gr.Blocks(title="Traffic Sign Detector") as demo:
116
  queue=True
117
  )
118
 
119
- # Zoom output
120
- zoom_output.change(
121
- fn=lambda z: apply_zoom(original_output_image, z),
122
- inputs=[zoom_output],
123
- outputs=[output_zoomed],
124
- queue=False
125
- )
126
-
127
- # Zoom preprocessed
128
- zoom_preprocessed.change(
129
- fn=lambda z: apply_zoom(original_preprocessed_image, z),
130
- inputs=[zoom_preprocessed],
131
- outputs=[preprocessed_zoomed],
132
- queue=False
133
- )
134
-
135
- # Clear
136
  reset_btn.click(
137
- fn=lambda: (None, None, None, 100, 100, None, None),
138
- outputs=[input_image, output_image, preprocessed_image, zoom_output, zoom_preprocessed, output_zoomed, preprocessed_zoomed]
139
  )
140
 
141
  if __name__ == "__main__":
 
6
  # Load the detector
7
  detector = TrafficSignDetector('config.yaml')
8
 
 
 
 
 
9
  def detect_traffic_signs(image, confidence_threshold):
10
  """
11
  Process the uploaded image and return the image with detected signs.
12
  """
 
 
13
  print(f"Received image type: {type(image)}")
14
  if hasattr(image, 'convert'):
15
  image = np.array(image)
 
26
  result_image = cv2.cvtColor(result_image, cv2.COLOR_BGR2RGB)
27
  preprocessed_image = cv2.cvtColor(preprocessed_image, cv2.COLOR_BGR2RGB)
28
 
 
 
 
 
29
  return result_image, preprocessed_image
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # Create Gradio interface
32
  with gr.Blocks(title="Traffic Sign Detector") as demo:
33
  gr.Markdown("# Traffic Sign Detector")
34
+ gr.Markdown("Upload an image to detect traffic signs using YOLOv8. Detection runs automatically when you upload or adjust the threshold.")
35
 
36
  with gr.Row():
37
  input_image = gr.Image(label="Upload Image", type="pil")
 
46
  maximum=0.9,
47
  value=0.30,
48
  step=0.01,
49
+ label="Confidence Threshold",
50
+ info="Lower values show more detections (less confident). Adjust to find optimal balance."
51
  )
52
 
53
  with gr.Row():
54
  detect_btn = gr.Button("Detect Traffic Signs", variant="primary")
55
  reset_btn = gr.Button("Clear")
56
 
57
+ # Auto-detect on image upload
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  input_image.change(
59
  fn=detect_traffic_signs,
60
  inputs=[input_image, confidence_threshold],
 
78
  queue=True
79
  )
80
 
81
+ # Clear button
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  reset_btn.click(
83
+ fn=lambda: (None, None, None, 0.30),
84
+ outputs=[input_image, output_image, preprocessed_image, confidence_threshold]
85
  )
86
 
87
  if __name__ == "__main__":