Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,31 @@ import numpy as np
|
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Download pretrained ANPR model (trained to detect license plates)
|
| 10 |
ANPR_WEIGHTS = "anpr_yolov8.pt"
|
| 11 |
if not os.path.exists(ANPR_WEIGHTS):
|
|
|
|
| 12 |
os.system(f"wget -O {ANPR_WEIGHTS} https://github.com/madalinabuzatu/yolov8-license-plate-detection/releases/download/v1.0/best.pt")
|
| 13 |
|
| 14 |
-
# Load YOLO ANPR model
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Load OCR reader
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def detect_and_read_plate(image):
|
| 21 |
results = model(image)
|
|
@@ -36,7 +51,6 @@ def detect_and_read_plate(image):
|
|
| 36 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 37 |
cv2.putText(image, text, (x1, y1 - 10),
|
| 38 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
| 39 |
-
|
| 40 |
return image
|
| 41 |
|
| 42 |
demo = gr.Interface(
|
|
@@ -46,4 +60,4 @@ demo = gr.Interface(
|
|
| 46 |
title="Automatic Number Plate Recognition (ANPR)",
|
| 47 |
description="Upload an image of a car to detect and read its license plate."
|
| 48 |
)
|
| 49 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Ensure directories exist
|
| 10 |
+
os.makedirs(os.getenv('EASYOCR_MODULE_PATH', '/app/.EasyOCR'), exist_ok=True)
|
| 11 |
+
os.makedirs(os.getenv('YOLO_CONFIG_DIR', '/app/.config/Ultralytics'), exist_ok=True)
|
| 12 |
+
|
| 13 |
# Download pretrained ANPR model (trained to detect license plates)
|
| 14 |
ANPR_WEIGHTS = "anpr_yolov8.pt"
|
| 15 |
if not os.path.exists(ANPR_WEIGHTS):
|
| 16 |
+
print(f"Downloading model weights to {ANPR_WEIGHTS}")
|
| 17 |
os.system(f"wget -O {ANPR_WEIGHTS} https://github.com/madalinabuzatu/yolov8-license-plate-detection/releases/download/v1.0/best.pt")
|
| 18 |
|
| 19 |
+
# Load YOLO ANPR model with error handling
|
| 20 |
+
try:
|
| 21 |
+
model = YOLO(ANPR_WEIGHTS)
|
| 22 |
+
print(f"Successfully loaded YOLO model from {ANPR_WEIGHTS}")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error loading YOLO model from {ANPR_WEIGHTS}: {str(e)}")
|
| 25 |
+
raise
|
| 26 |
|
| 27 |
+
# Load OCR reader with specified model storage directory
|
| 28 |
+
try:
|
| 29 |
+
reader = easyocr.Reader(['en'], model_storage_directory=os.getenv('EASYOCR_MODULE_PATH', '/app/.EasyOCR'))
|
| 30 |
+
print("Successfully initialized EasyOCR reader")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error initializing EasyOCR reader: {str(e)}")
|
| 33 |
+
raise
|
| 34 |
|
| 35 |
def detect_and_read_plate(image):
|
| 36 |
results = model(image)
|
|
|
|
| 51 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 52 |
cv2.putText(image, text, (x1, y1 - 10),
|
| 53 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
|
|
|
| 54 |
return image
|
| 55 |
|
| 56 |
demo = gr.Interface(
|
|
|
|
| 60 |
title="Automatic Number Plate Recognition (ANPR)",
|
| 61 |
description="Upload an image of a car to detect and read its license plate."
|
| 62 |
)
|
| 63 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|