Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import cv2
|
4 |
+
import pytesseract
|
5 |
+
|
6 |
+
# Load the trained YOLOv5 model (replace 'best.pt' with your actual model path)
|
7 |
+
model = torch.hub.load('ultralytics/yolov5:v6.0', 'custom', path='runs/train/exp/weights/yolov10n.pt')
|
8 |
+
|
9 |
+
def process_video(input_video):
|
10 |
+
# Read video frames
|
11 |
+
cap = cv2.VideoCapture(input_video.name)
|
12 |
+
output_video = "output.mp4"
|
13 |
+
|
14 |
+
# Get video details
|
15 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
16 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
17 |
+
frame_width = int(cap.get(3))
|
18 |
+
frame_height = int(cap.get(4))
|
19 |
+
|
20 |
+
out = cv2.VideoWriter(output_video, fourcc, fps, (frame_width, frame_height))
|
21 |
+
|
22 |
+
while cap.isOpened():
|
23 |
+
ret, frame = cap.read()
|
24 |
+
if not ret:
|
25 |
+
break
|
26 |
+
|
27 |
+
# Use YOLO model to detect license plates
|
28 |
+
results = model(frame)
|
29 |
+
detected_boxes = results.xyxy[0] # Bounding boxes, confidence scores, and class IDs
|
30 |
+
|
31 |
+
# Loop through all the detected bounding boxes
|
32 |
+
for box in detected_boxes:
|
33 |
+
x1, y1, x2, y2, conf, cls = map(int, box[:6]) # Extract bounding box coordinates and confidence
|
34 |
+
if conf > 0.5: # You can adjust the confidence threshold as needed
|
35 |
+
# Draw the bounding box on the frame
|
36 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
37 |
+
|
38 |
+
# Optionally, draw the confidence score and label (use class names for 3 classes)
|
39 |
+
if cls == 0:
|
40 |
+
label = "Analog License Plate"
|
41 |
+
elif cls == 1:
|
42 |
+
label = "Digital License Plate"
|
43 |
+
elif cls == 2:
|
44 |
+
label = "Non-License Plate"
|
45 |
+
else:
|
46 |
+
label = "Unknown"
|
47 |
+
|
48 |
+
# Draw label and confidence on frame
|
49 |
+
cv2.putText(frame, f"{label}: {conf:.2f}", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
50 |
+
|
51 |
+
# Optionally, collect the bounding box coordinates for further processing (e.g., OCR)
|
52 |
+
license_plate = frame[y1:y2, x1:x2]
|
53 |
+
# Convert to grayscale for better OCR results
|
54 |
+
gray_license_plate = cv2.cvtColor(license_plate, cv2.COLOR_BGR2GRAY)
|
55 |
+
|
56 |
+
# Use Tesseract OCR to extract text from Bangla license plates (adjust config as needed)
|
57 |
+
text = pytesseract.image_to_string(gray_license_plate, config="--psm 6 -l ben") # 'ben' is for Bangla
|
58 |
+
print(f"Detected License Plate Text: {text.strip()}")
|
59 |
+
|
60 |
+
# Write the annotated frame to output video
|
61 |
+
out.write(frame)
|
62 |
+
|
63 |
+
cap.release()
|
64 |
+
out.release()
|
65 |
+
return output_video
|
66 |
+
|
67 |
+
# Create Gradio Interface
|
68 |
+
interface = gr.Interface(fn=process_video, inputs=gr.inputs.Video(), outputs=gr.outputs.Video())
|
69 |
+
interface.launch()
|