Spaces:
Runtime error
Runtime error
Create inference_utils.py
Browse files- inference_utils.py +58 -0
inference_utils.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# inference_utils.py
|
| 2 |
+
import os, cv2, re
|
| 3 |
+
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from paddleocr import PaddleOCR
|
| 8 |
+
from difflib import get_close_matches
|
| 9 |
+
|
| 10 |
+
# Load models from Hugging Face
|
| 11 |
+
def load_models():
|
| 12 |
+
vehicle_detector = YOLO("https://huggingface.co/Prabhat51/veh-detect/resolve/main/veh_detect.pt")
|
| 13 |
+
vehicle_classifier = YOLO("https://huggingface.co/Prabhat51/veh-class/resolve/main/veh_class.pt")
|
| 14 |
+
plate_detector = YOLO("https://huggingface.co/Prabhat51/plate-detect/resolve/main/plate_detect.pt")
|
| 15 |
+
ocr_reader = PaddleOCR(use_angle_cls=True, lang='en')
|
| 16 |
+
return vehicle_detector, vehicle_classifier, plate_detector, ocr_reader
|
| 17 |
+
|
| 18 |
+
# Validate Indian number plate
|
| 19 |
+
valid_rto_codes = { ... } # use your RTO set here
|
| 20 |
+
|
| 21 |
+
def correct_plate_text(text):
|
| 22 |
+
text = re.sub(r'[^A-Z0-9]', '', text.upper())
|
| 23 |
+
text = text.replace('O', '0').replace('I', '1')
|
| 24 |
+
match = re.match(r'^([A-Z]{2})([0-9]{2})([A-Z]{1,2})([0-9]{3,4})$', text)
|
| 25 |
+
if match and match.group(1) in valid_rto_codes:
|
| 26 |
+
return text
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# Inference on single frame
|
| 30 |
+
def process_frame(frame, vehicle_detector, vehicle_classifier, plate_detector, ocr_reader):
|
| 31 |
+
results = []
|
| 32 |
+
detections = vehicle_detector(frame)[0].boxes
|
| 33 |
+
for box in detections:
|
| 34 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 35 |
+
vehicle_crop = frame[y1:y2, x1:x2]
|
| 36 |
+
|
| 37 |
+
cls_result = vehicle_classifier(vehicle_crop)
|
| 38 |
+
if not cls_result[0].probs:
|
| 39 |
+
continue
|
| 40 |
+
vehicle_type = cls_result[0].names[cls_result[0].probs.top1]
|
| 41 |
+
|
| 42 |
+
plate_boxes = plate_detector(vehicle_crop)[0].boxes
|
| 43 |
+
for pb in plate_boxes:
|
| 44 |
+
px1, py1, px2, py2 = map(int, pb.xyxy[0])
|
| 45 |
+
plate_crop = vehicle_crop[py1:py2, px1:px2]
|
| 46 |
+
|
| 47 |
+
ocr_result = ocr_reader.ocr(plate_crop, cls=True)
|
| 48 |
+
if not ocr_result or not ocr_result[0]:
|
| 49 |
+
continue
|
| 50 |
+
|
| 51 |
+
raw_text = ocr_result[0][0][1][0]
|
| 52 |
+
plate_text = correct_plate_text(raw_text)
|
| 53 |
+
if not plate_text:
|
| 54 |
+
continue
|
| 55 |
+
|
| 56 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 57 |
+
results.append((timestamp, vehicle_type, plate_text))
|
| 58 |
+
return results
|