|
import gradio as gr |
|
import torch |
|
import cv2 |
|
import pytesseract |
|
|
|
|
|
model = torch.hub.load('ultralytics/yolov5:v6.0', 'custom', path='runs/train/exp/weights/yolov10n.pt') |
|
|
|
def process_video(input_video): |
|
|
|
cap = cv2.VideoCapture(input_video.name) |
|
output_video = "output.mp4" |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v') |
|
fps = int(cap.get(cv2.CAP_PROP_FPS)) |
|
frame_width = int(cap.get(3)) |
|
frame_height = int(cap.get(4)) |
|
|
|
out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height)) |
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
results = model(frame) |
|
detected_boxes = results.xyxy[0] |
|
|
|
|
|
for box in detected_boxes: |
|
x1, y1, x2, y2, conf, cls = map(int, box[:6]) |
|
if conf > 0.5: |
|
|
|
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) |
|
|
|
|
|
if cls == 0: |
|
label = "Analog License Plate" |
|
elif cls == 1: |
|
label = "Digital License Plate" |
|
elif cls == 2: |
|
label = "Non-License Plate" |
|
else: |
|
label = "Unknown" |
|
|
|
|
|
cv2.putText(frame, f"{label}: {conf:.2f}", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) |
|
|
|
|
|
license_plate = frame[y1:y2, x1:x2] |
|
|
|
gray_license_plate = cv2.cvtColor(license_plate, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
text = pytesseract.image_to_string(gray_license_plate, config="--psm 6 -l ben") |
|
print(f"Detected License Plate Text: {text.strip()}") |
|
|
|
|
|
out.write(frame) |
|
|
|
cap.release() |
|
out.release() |
|
return output_video |
|
|
|
|
|
interface = gr.Interface(fn=process_video, inputs=gr.inputs.Video(), outputs=gr.outputs.Video()) |
|
interface.launch() |
|
|