Abs6187 commited on
Commit
3e29c86
·
verified ·
1 Parent(s): a516b42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -284
app.py CHANGED
@@ -1,294 +1,31 @@
1
  import gradio as gr
2
- import pandas as pd
3
- from detector import (
4
- yolov8_detect,
5
- download_sample_images,
6
- get_ocr_status,
7
- ADVANCED_OCR_AVAILABLE,
8
- OCR_AVAILABLE
9
- )
10
 
11
- try:
12
- from advanced_ocr import get_available_models
13
- except ImportError:
14
- def get_available_models():
15
- return {}
16
-
17
- download_sample_images()
18
-
19
- ocr_status = get_ocr_status()
20
- custom_css = """
21
- .gradio-container {
22
- max-width: 1200px !important;
23
- margin: 0 auto;
24
- }
25
-
26
- .main-header {
27
- text-align: center;
28
- margin-bottom: 2rem;
29
- padding: 1rem;
30
- }
31
-
32
- .main-title {
33
- font-size: 2rem;
34
- font-weight: 600;
35
- color: #333;
36
- margin: 0;
37
- }
38
-
39
- .subtitle {
40
- color: #666;
41
- font-size: 1rem;
42
- margin: 0.5rem 0;
43
- }
44
-
45
- .status-info {
46
- font-size: 0.9rem;
47
- color: #888;
48
- margin: 0.5rem 0;
49
- }
50
-
51
- .section-gap {
52
- margin: 1.5rem 0;
53
- }
54
- """
55
-
56
- def toggle_sections(extract_text_checked, crop_checked):
57
- show_gallery = bool(extract_text_checked and crop_checked)
58
- show_ocr = bool(extract_text_checked)
59
- return (
60
- gr.update(visible=show_gallery),
61
- gr.update(visible=show_ocr),
62
- )
63
-
64
- def get_ocr_status_text():
65
- if ADVANCED_OCR_AVAILABLE:
66
- return "Advanced OCR Available"
67
- elif OCR_AVAILABLE:
68
- return "Basic OCR Available"
69
- else:
70
- return "OCR Not Available"
71
-
72
- with gr.Blocks(css=custom_css, title="AI Helmet Detection System") as demo:
73
 
74
- gr.HTML(f"""
75
- <div class="main-header">
76
- <h1 class="main-title">AI Helmet Detection System</h1>
77
- <p class="subtitle">Motorcyclist safety monitoring with license plate recognition</p>
78
- <p class="status-info">YOLOv11 • {get_ocr_status_text()} • Real-time Processing</p>
79
- </div>
80
- """)
81
-
82
- with gr.Tabs():
83
- with gr.TabItem("Detection"):
84
- with gr.Row():
85
- with gr.Column(scale=1):
86
- gr.Markdown("### Settings")
87
-
88
- input_image = gr.Image(
89
- type="filepath",
90
- label="Upload Image",
91
- sources=["upload", "webcam"]
92
- )
93
-
94
- with gr.Row():
95
- image_size = gr.Slider(
96
- minimum=320, maximum=1280, value=640, step=32,
97
- label="Image Size"
98
- )
99
-
100
- with gr.Row():
101
- conf_threshold = gr.Slider(
102
- minimum=0.0, maximum=1.0, value=0.4, step=0.05,
103
- label="Confidence"
104
- )
105
- iou_threshold = gr.Slider(
106
- minimum=0.0, maximum=1.0, value=0.5, step=0.05,
107
- label="IoU Threshold"
108
- )
109
-
110
- show_stats = gr.Checkbox(
111
- value=True,
112
- label="Show Statistics"
113
- )
114
- crop_plates = gr.Checkbox(
115
- value=True,
116
- label="Extract License Plates"
117
- )
118
-
119
- if ocr_status["any_available"]:
120
- extract_text = gr.Checkbox(
121
- value=False,
122
- label="Enable OCR"
123
- )
124
- ocr_on_no_helmet = gr.Checkbox(
125
- value=True,
126
- label="Auto-OCR for No Helmet"
127
- )
128
-
129
- if ADVANCED_OCR_AVAILABLE:
130
- models = get_available_models()
131
- model_choices = [("Auto (Recommended)", "auto"), ("Basic EasyOCR", "basic")]
132
- for key, info in models.items():
133
- model_choices.append((info['name'], key))
134
- selected_ocr_model = gr.Dropdown(
135
- choices=model_choices,
136
- value="auto",
137
- label="OCR Model"
138
- )
139
- else:
140
- selected_ocr_model = gr.State("basic")
141
-
142
- gr.Markdown("*Note: OCR processing may increase detection time.*")
143
- else:
144
- extract_text = gr.Checkbox(
145
- value=False,
146
- label="OCR Not Available",
147
- interactive=False
148
- )
149
- ocr_on_no_helmet = gr.Checkbox(
150
- value=False,
151
- label="Auto-OCR (Not Available)",
152
- interactive=False
153
- )
154
- selected_ocr_model = gr.State("basic")
155
-
156
- with gr.Row():
157
- submit_btn = gr.Button("Start Detection", variant="primary")
158
- clear_btn = gr.Button("Clear")
159
-
160
- with gr.Column(scale=2):
161
- gr.Markdown("### Results")
162
-
163
- output_image = gr.Image(
164
- type="pil",
165
- label="Detection Results"
166
- )
167
-
168
- with gr.Row():
169
- output_table = gr.Dataframe(
170
- headers=["Object", "Confidence", "Position", "Dimensions"],
171
- label="Detection Details",
172
- interactive=False
173
- )
174
- output_stats = gr.Textbox(
175
- label="Statistics",
176
- interactive=False,
177
- lines=6
178
- )
179
-
180
- license_gallery = gr.Gallery(
181
- label="License Plates",
182
- columns=3,
183
- visible=False
184
- )
185
-
186
- ocr_group = gr.Group(visible=False)
187
- with ocr_group:
188
- plate_text_output = gr.Textbox(
189
- label="OCR Results",
190
- lines=4,
191
- interactive=False
192
- )
193
-
194
- download_file = gr.File(
195
- label="Download Results (ZIP)",
196
- interactive=False
197
- )
198
-
199
- with gr.TabItem("Examples"):
200
- gr.Markdown("### Sample Images")
201
- gr.Markdown("Click any example to test the detection system:")
202
-
203
- gr.Examples(
204
- examples=[
205
- ["sample_1.jpg"],
206
- ["sample_2.jpg"],
207
- ["sample_3.jpg"],
208
- ["sample_4.jpg"],
209
- ["sample_6.jpg"],
210
- ["sample_7.jpg"],
211
- ["sample_8.jpg"],
212
- ],
213
- inputs=input_image,
214
- outputs=[
215
- output_image,
216
- output_table,
217
- output_stats,
218
- license_gallery,
219
- download_file,
220
- plate_text_output,
221
- ],
222
- fn=lambda img: yolov8_detect(
223
- img, 640, 0.4, 0.5, True, True, True, False
224
- ),
225
- cache_examples=True
226
  )
 
227
 
228
- with gr.TabItem("Info"):
229
- gr.Markdown("### System Information")
230
-
231
- gr.Markdown(f"""
232
- **AI Model:** YOLOv11
233
- **Classes:** Helmet, No Helmet, License Plate
234
- **OCR Status:** {get_ocr_status_text()}
235
- **Features:** Detection, extraction, text recognition
236
-
237
- **Privacy:** All processing is local. No data stored.
238
- **Usage:** For demonstration and research purposes only.
239
- """)
240
-
241
- submit_btn.click(
242
- fn=yolov8_detect,
243
- inputs=[
244
- input_image,
245
- image_size,
246
- conf_threshold,
247
- iou_threshold,
248
- show_stats,
249
- gr.State(True),
250
- crop_plates,
251
- extract_text,
252
- ocr_on_no_helmet,
253
- selected_ocr_model,
254
- ],
255
- outputs=[
256
- output_image,
257
- output_table,
258
- output_stats,
259
- license_gallery,
260
- download_file,
261
- plate_text_output,
262
- ],
263
- )
264
-
265
- clear_btn.click(
266
- fn=lambda: [None, None, None, None, None, None],
267
- inputs=[],
268
- outputs=[
269
- input_image,
270
- output_image,
271
- output_table,
272
- output_stats,
273
- license_gallery,
274
- download_file,
275
- plate_text_output,
276
- ],
277
- )
278
-
279
- extract_text.change(
280
- fn=toggle_sections,
281
- inputs=[extract_text, crop_plates],
282
- outputs=[license_gallery, ocr_group],
283
- )
284
- crop_plates.change(
285
- fn=toggle_sections,
286
- inputs=[extract_text, crop_plates],
287
- outputs=[license_gallery, ocr_group],
288
- )
289
 
290
  if __name__ == "__main__":
291
- demo.launch(
 
292
  debug=True,
293
  share=True,
294
  server_name="0.0.0.0",
 
1
  import gradio as gr
2
+ from ui import UIComponents
 
 
 
 
 
 
 
3
 
4
+ def create_app():
5
+ ui = UIComponents()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ with gr.Blocks(css=ui.custom_css, title="AI Helmet Detection System") as demo:
8
+ ui.create_header()
9
+
10
+ with gr.Tabs():
11
+ with gr.TabItem("Detection"):
12
+ with gr.Row():
13
+ settings_components = ui.create_settings_panel()
14
+ results_components = ui.create_results_panel()
15
+
16
+ ui.create_examples_tab(
17
+ settings_components['input_image'],
18
+ results_components
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
+ ui.create_info_tab()
21
 
22
+ ui.setup_event_handlers(settings_components, results_components)
23
+
24
+ return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  if __name__ == "__main__":
27
+ app = create_app()
28
+ app.launch(
29
  debug=True,
30
  share=True,
31
  server_name="0.0.0.0",