Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,9 @@ import numpy as np
|
|
| 11 |
from urllib.parse import urlparse
|
| 12 |
from PIL import Image
|
| 13 |
from transformers import AutoImageProcessor, YolosForObjectDetection, DetrForObjectDetection
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
| 16 |
|
|
@@ -39,20 +42,74 @@ def get_original_image(url_input):
|
|
| 39 |
return image
|
| 40 |
|
| 41 |
|
| 42 |
-
# ---------------- Model Loading ----------------
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
if
|
| 48 |
-
|
| 49 |
-
elif
|
| 50 |
-
|
| 51 |
else:
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
return processor, model
|
| 56 |
|
| 57 |
|
| 58 |
# ---------------- Core Inference ----------------
|
|
|
|
| 11 |
from urllib.parse import urlparse
|
| 12 |
from PIL import Image
|
| 13 |
from transformers import AutoImageProcessor, YolosForObjectDetection, DetrForObjectDetection
|
| 14 |
+
import easyocr
|
| 15 |
+
from datetime import datetime
|
| 16 |
+
|
| 17 |
|
| 18 |
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
| 19 |
|
|
|
|
| 42 |
return image
|
| 43 |
|
| 44 |
|
|
|
|
| 45 |
|
| 46 |
+
# -------------------- Database --------------------
|
| 47 |
+
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
|
| 48 |
+
cursor = conn.cursor()
|
| 49 |
+
cursor.execute("""
|
| 50 |
+
CREATE TABLE IF NOT EXISTS vehicles (
|
| 51 |
+
plate TEXT,
|
| 52 |
+
type TEXT,
|
| 53 |
+
time TEXT
|
| 54 |
+
)
|
| 55 |
+
""")
|
| 56 |
+
conn.commit()
|
| 57 |
+
|
| 58 |
+
# -------------------- Models --------------------
|
| 59 |
+
processor = YolosImageProcessor.from_pretrained(
|
| 60 |
+
"nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 61 |
+
)
|
| 62 |
+
model = YolosForObjectDetection.from_pretrained(
|
| 63 |
+
"nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 64 |
+
)
|
| 65 |
+
model.eval()
|
| 66 |
+
|
| 67 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 68 |
+
|
| 69 |
+
# -------------------- Plate Color Classifier --------------------
|
| 70 |
+
def classify_plate_color(plate_img):
|
| 71 |
+
img = np.array(plate_img)
|
| 72 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
| 73 |
+
|
| 74 |
+
green = np.sum(cv2.inRange(hsv, (35, 40, 40), (85, 255, 255)))
|
| 75 |
+
yellow = np.sum(cv2.inRange(hsv, (15, 50, 50), (35, 255, 255)))
|
| 76 |
+
white = np.sum(cv2.inRange(hsv, (0, 0, 200), (180, 30, 255)))
|
| 77 |
|
| 78 |
+
if green > yellow and green > white:
|
| 79 |
+
return "EV"
|
| 80 |
+
elif yellow > green and yellow > white:
|
| 81 |
+
return "Commercial"
|
| 82 |
else:
|
| 83 |
+
return "Personal"
|
| 84 |
+
|
| 85 |
+
# -------------------- OCR --------------------
|
| 86 |
+
def read_plate(plate_img):
|
| 87 |
+
results = reader.readtext(np.array(plate_img))
|
| 88 |
+
if results:
|
| 89 |
+
return results[0][1]
|
| 90 |
+
return "UNKNOWN"
|
| 91 |
+
|
| 92 |
+
# -------------------- Dashboard --------------------
|
| 93 |
+
def get_dashboard():
|
| 94 |
+
df = pd.read_sql("SELECT * FROM vehicles", conn)
|
| 95 |
+
|
| 96 |
+
fig, ax = plt.subplots(figsize=(8, 5))
|
| 97 |
+
|
| 98 |
+
if len(df) == 0:
|
| 99 |
+
ax.text(0.5, 0.5, "No vehicles scanned yet",
|
| 100 |
+
ha="center", va="center", fontsize=10)
|
| 101 |
+
ax.axis("off")
|
| 102 |
+
return fig
|
| 103 |
+
|
| 104 |
+
counts = df["type"].value_counts()
|
| 105 |
+
counts.plot(kind="bar", ax=ax)
|
| 106 |
+
|
| 107 |
+
ax.set_title("Vehicle Classification Dashboard")
|
| 108 |
+
ax.set_xlabel("Vehicle Type")
|
| 109 |
+
ax.set_ylabel("Count")
|
| 110 |
+
ax.grid(axis="y")
|
| 111 |
|
| 112 |
+
return fig
|
|
|
|
| 113 |
|
| 114 |
|
| 115 |
# ---------------- Core Inference ----------------
|