Update app.py
Browse files
app.py
CHANGED
|
@@ -203,7 +203,8 @@ def yoloV8_func(
|
|
| 203 |
show_stats=True,
|
| 204 |
show_confidence=True,
|
| 205 |
crop_plates=True,
|
| 206 |
-
extract_text=False
|
|
|
|
| 207 |
):
|
| 208 |
if image_size is None:
|
| 209 |
image_size = 640
|
|
@@ -242,25 +243,39 @@ def yoloV8_func(
|
|
| 242 |
plate_texts = []
|
| 243 |
download_files = None
|
| 244 |
|
|
|
|
|
|
|
|
|
|
| 245 |
if crop_plates and detections:
|
| 246 |
try:
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
print(f"Successfully cropped {len(cropped_plates)} license plates")
|
| 250 |
|
| 251 |
license_plate_gallery = [plate_data['image'] for plate_data in cropped_plates]
|
| 252 |
|
| 253 |
-
if
|
| 254 |
print("Extracting text from license plates...")
|
| 255 |
plate_texts = []
|
| 256 |
for i, plate_data in enumerate(cropped_plates):
|
| 257 |
text = plate_data.get('text', 'No text detected')
|
| 258 |
print(f"Plate {i+1} text: {text}")
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
plate_texts = ["OCR not available - install requirements: pip install transformers easyocr"]
|
| 262 |
-
elif not
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
if cropped_plates or detections:
|
| 266 |
download_files, _, _ = create_download_files(annotated_image, cropped_plates, detections)
|
|
@@ -284,11 +299,17 @@ def yoloV8_func(
|
|
| 284 |
if cropped_plates:
|
| 285 |
stats_text += f"\nLicense Plates Cropped: {len(cropped_plates)}\n"
|
| 286 |
|
| 287 |
-
if
|
|
|
|
|
|
|
|
|
|
| 288 |
stats_text += "Extracted Text:\n"
|
| 289 |
for i, plate_data in enumerate(cropped_plates):
|
| 290 |
text = plate_data.get('text', 'No text')
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
if show_stats and stats_text:
|
| 294 |
draw = ImageDraw.Draw(annotated_image)
|
|
@@ -360,9 +381,11 @@ with gr.Blocks(css=custom_css, title="YOLOv11 Motorcyclist Helmet Detection") as
|
|
| 360 |
|
| 361 |
if OCR_AVAILABLE:
|
| 362 |
extract_text = gr.Checkbox(value=False, label="Extract Text from License Plates (OCR)")
|
|
|
|
| 363 |
gr.Markdown("*Note: OCR processing may take additional time*")
|
| 364 |
else:
|
| 365 |
extract_text = gr.Checkbox(value=False, label="Extract Text (OCR Not Available)", interactive=False)
|
|
|
|
| 366 |
gr.Markdown("*Install requirements: `pip install torch transformers easyocr opencv-python`*")
|
| 367 |
|
| 368 |
submit_btn = gr.Button("Detect Objects", variant="primary")
|
|
@@ -427,7 +450,7 @@ with gr.Blocks(css=custom_css, title="YOLOv11 Motorcyclist Helmet Detection") as
|
|
| 427 |
|
| 428 |
submit_btn.click(
|
| 429 |
fn=yoloV8_func,
|
| 430 |
-
inputs=[input_image, image_size, conf_threshold, iou_threshold, show_stats, gr.State(True), crop_plates, extract_text],
|
| 431 |
outputs=[output_image, output_table, output_stats, license_gallery, download_file, plate_text_output]
|
| 432 |
)
|
| 433 |
|
|
|
|
| 203 |
show_stats=True,
|
| 204 |
show_confidence=True,
|
| 205 |
crop_plates=True,
|
| 206 |
+
extract_text=False,
|
| 207 |
+
ocr_on_no_helmet=False
|
| 208 |
):
|
| 209 |
if image_size is None:
|
| 210 |
image_size = 640
|
|
|
|
| 243 |
plate_texts = []
|
| 244 |
download_files = None
|
| 245 |
|
| 246 |
+
has_no_helmet = any(detection['Object'] == 'Without Helmet' for detection in detections)
|
| 247 |
+
should_extract_text = extract_text or (ocr_on_no_helmet and has_no_helmet)
|
| 248 |
+
|
| 249 |
if crop_plates and detections:
|
| 250 |
try:
|
| 251 |
+
license_plate_count = len([d for d in detections if d['Object'] == 'License Plate'])
|
| 252 |
+
print(f"Processing {license_plate_count} license plates...")
|
| 253 |
+
|
| 254 |
+
if ocr_on_no_helmet and has_no_helmet:
|
| 255 |
+
print("⚠️ No helmet detected - OCR will be performed on license plates")
|
| 256 |
+
|
| 257 |
+
cropped_plates = crop_license_plates(image, detections, should_extract_text)
|
| 258 |
print(f"Successfully cropped {len(cropped_plates)} license plates")
|
| 259 |
|
| 260 |
license_plate_gallery = [plate_data['image'] for plate_data in cropped_plates]
|
| 261 |
|
| 262 |
+
if should_extract_text and OCR_AVAILABLE:
|
| 263 |
print("Extracting text from license plates...")
|
| 264 |
plate_texts = []
|
| 265 |
for i, plate_data in enumerate(cropped_plates):
|
| 266 |
text = plate_data.get('text', 'No text detected')
|
| 267 |
print(f"Plate {i+1} text: {text}")
|
| 268 |
+
if ocr_on_no_helmet and has_no_helmet:
|
| 269 |
+
plate_texts.append(f"🚨 No Helmet Violation - Plate {i+1}: {text}")
|
| 270 |
+
else:
|
| 271 |
+
plate_texts.append(f"Plate {i+1}: {text}")
|
| 272 |
+
elif should_extract_text and not OCR_AVAILABLE:
|
| 273 |
plate_texts = ["OCR not available - install requirements: pip install transformers easyocr"]
|
| 274 |
+
elif not should_extract_text:
|
| 275 |
+
if ocr_on_no_helmet and not has_no_helmet:
|
| 276 |
+
plate_texts = [f"Plate {i+1}: OCR only on no-helmet violations" for i in range(len(cropped_plates))]
|
| 277 |
+
else:
|
| 278 |
+
plate_texts = [f"Plate {i+1}: Text extraction disabled" for i in range(len(cropped_plates))]
|
| 279 |
|
| 280 |
if cropped_plates or detections:
|
| 281 |
download_files, _, _ = create_download_files(annotated_image, cropped_plates, detections)
|
|
|
|
| 299 |
if cropped_plates:
|
| 300 |
stats_text += f"\nLicense Plates Cropped: {len(cropped_plates)}\n"
|
| 301 |
|
| 302 |
+
if has_no_helmet:
|
| 303 |
+
stats_text += "⚠️ HELMET VIOLATION DETECTED!\n"
|
| 304 |
+
|
| 305 |
+
if should_extract_text and OCR_AVAILABLE:
|
| 306 |
stats_text += "Extracted Text:\n"
|
| 307 |
for i, plate_data in enumerate(cropped_plates):
|
| 308 |
text = plate_data.get('text', 'No text')
|
| 309 |
+
if has_no_helmet and ocr_on_no_helmet:
|
| 310 |
+
stats_text += f"🚨 Violation - Plate {i+1}: {text}\n"
|
| 311 |
+
else:
|
| 312 |
+
stats_text += f"- Plate {i+1}: {text}\n"
|
| 313 |
|
| 314 |
if show_stats and stats_text:
|
| 315 |
draw = ImageDraw.Draw(annotated_image)
|
|
|
|
| 381 |
|
| 382 |
if OCR_AVAILABLE:
|
| 383 |
extract_text = gr.Checkbox(value=False, label="Extract Text from License Plates (OCR)")
|
| 384 |
+
ocr_on_no_helmet = gr.Checkbox(value=True, label="🚨 Auto-OCR when No Helmet Detected")
|
| 385 |
gr.Markdown("*Note: OCR processing may take additional time*")
|
| 386 |
else:
|
| 387 |
extract_text = gr.Checkbox(value=False, label="Extract Text (OCR Not Available)", interactive=False)
|
| 388 |
+
ocr_on_no_helmet = gr.Checkbox(value=False, label="🚨 Auto-OCR when No Helmet (Not Available)", interactive=False)
|
| 389 |
gr.Markdown("*Install requirements: `pip install torch transformers easyocr opencv-python`*")
|
| 390 |
|
| 391 |
submit_btn = gr.Button("Detect Objects", variant="primary")
|
|
|
|
| 450 |
|
| 451 |
submit_btn.click(
|
| 452 |
fn=yoloV8_func,
|
| 453 |
+
inputs=[input_image, image_size, conf_threshold, iou_threshold, show_stats, gr.State(True), crop_plates, extract_text, ocr_on_no_helmet],
|
| 454 |
outputs=[output_image, output_table, output_stats, license_gallery, download_file, plate_text_output]
|
| 455 |
)
|
| 456 |
|