Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ from ultralytics import YOLO
|
|
| 5 |
from PIL import Image
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
-
# Model labels
|
| 9 |
model1Labels = {0: 'single_number_plate', 1: 'double_number_plate'}
|
| 10 |
|
| 11 |
model2Labels = {
|
|
@@ -18,6 +18,7 @@ model2Labels = {
|
|
| 18 |
model = YOLO("models/LP-detection.pt")
|
| 19 |
model2 = YOLO("models/Charcter-LP.pt")
|
| 20 |
|
|
|
|
| 21 |
def prediction(image):
|
| 22 |
result = model.predict(source=image, conf=0.5)
|
| 23 |
boxes = result[0].boxes
|
|
@@ -85,11 +86,13 @@ def prediction(image):
|
|
| 85 |
|
| 86 |
return lp_number, img_lp_final
|
| 87 |
|
|
|
|
| 88 |
def process_video(video_file):
|
| 89 |
# Open the video
|
| 90 |
cap = cv2.VideoCapture(video_file)
|
|
|
|
| 91 |
if not cap.isOpened(): # Check if video was opened successfully
|
| 92 |
-
return None, "Error: Unable to open video."
|
| 93 |
|
| 94 |
license_plate_texts = []
|
| 95 |
processed_frames = []
|
|
@@ -100,12 +103,16 @@ def process_video(video_file):
|
|
| 100 |
if not ret:
|
| 101 |
break # Exit if no frame is read
|
| 102 |
|
|
|
|
| 103 |
license_plate_text, cropped_plate_img = prediction(frame)
|
|
|
|
|
|
|
| 104 |
license_plate_texts.append(" ".join(license_plate_text)) # Join the list of texts into a single string
|
| 105 |
processed_frames.append(cropped_plate_img)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
# Save detected texts to Excel
|
| 111 |
df = pd.DataFrame(license_plate_texts, columns=["License Plate"])
|
|
@@ -114,14 +121,22 @@ def process_video(video_file):
|
|
| 114 |
# Save processed video with license plates highlighted
|
| 115 |
output_video_path = 'processed_video.mp4'
|
| 116 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
|
| 117 |
-
out = cv2.VideoWriter(output_video_path, fourcc, 20.0, (frame.shape[1], frame.shape[0]))
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Gradio interface
|
| 127 |
with gr.Blocks() as demo:
|
|
@@ -129,9 +144,9 @@ with gr.Blocks() as demo:
|
|
| 129 |
gr.Markdown("Upload a video to get the license number of vehicles detected in each frame.")
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
-
video_input = gr.
|
| 133 |
-
video_output = gr.Video(label="Processed Video")
|
| 134 |
-
excel_output = gr.File(label="Excel File with Detected License Plates")
|
| 135 |
|
| 136 |
video_input.upload(process_video, inputs=video_input, outputs=[video_output, excel_output])
|
| 137 |
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
+
# Model labels for characters and license plates
|
| 9 |
model1Labels = {0: 'single_number_plate', 1: 'double_number_plate'}
|
| 10 |
|
| 11 |
model2Labels = {
|
|
|
|
| 18 |
model = YOLO("models/LP-detection.pt")
|
| 19 |
model2 = YOLO("models/Charcter-LP.pt")
|
| 20 |
|
| 21 |
+
# Function to process license plate and detect text
|
| 22 |
def prediction(image):
|
| 23 |
result = model.predict(source=image, conf=0.5)
|
| 24 |
boxes = result[0].boxes
|
|
|
|
| 86 |
|
| 87 |
return lp_number, img_lp_final
|
| 88 |
|
| 89 |
+
# Function to process the video
|
| 90 |
def process_video(video_file):
|
| 91 |
# Open the video
|
| 92 |
cap = cv2.VideoCapture(video_file)
|
| 93 |
+
|
| 94 |
if not cap.isOpened(): # Check if video was opened successfully
|
| 95 |
+
return None, "Error: Unable to open video. Please check the file format or path."
|
| 96 |
|
| 97 |
license_plate_texts = []
|
| 98 |
processed_frames = []
|
|
|
|
| 103 |
if not ret:
|
| 104 |
break # Exit if no frame is read
|
| 105 |
|
| 106 |
+
# Perform license plate detection and character recognition
|
| 107 |
license_plate_text, cropped_plate_img = prediction(frame)
|
| 108 |
+
|
| 109 |
+
# Collect license plate text
|
| 110 |
license_plate_texts.append(" ".join(license_plate_text)) # Join the list of texts into a single string
|
| 111 |
processed_frames.append(cropped_plate_img)
|
| 112 |
|
| 113 |
+
# Check if frames were processed
|
| 114 |
+
if not processed_frames:
|
| 115 |
+
return None, "Error: No frames processed. Check the video file or format."
|
| 116 |
|
| 117 |
# Save detected texts to Excel
|
| 118 |
df = pd.DataFrame(license_plate_texts, columns=["License Plate"])
|
|
|
|
| 121 |
# Save processed video with license plates highlighted
|
| 122 |
output_video_path = 'processed_video.mp4'
|
| 123 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
|
|
|
|
| 124 |
|
| 125 |
+
# Ensure frame shape is valid
|
| 126 |
+
if len(processed_frames) > 0:
|
| 127 |
+
frame = processed_frames[0]
|
| 128 |
+
height, width, _ = frame.shape
|
| 129 |
+
out = cv2.VideoWriter(output_video_path, fourcc, 20.0, (width, height))
|
| 130 |
+
|
| 131 |
+
for processed_frame in processed_frames:
|
| 132 |
+
out.write(processed_frame)
|
| 133 |
+
|
| 134 |
+
cap.release()
|
| 135 |
+
out.release()
|
| 136 |
|
| 137 |
+
return output_video_path, "detected_license_plates.xlsx"
|
| 138 |
+
else:
|
| 139 |
+
return None, "Error: No valid frames found."
|
| 140 |
|
| 141 |
# Gradio interface
|
| 142 |
with gr.Blocks() as demo:
|
|
|
|
| 144 |
gr.Markdown("Upload a video to get the license number of vehicles detected in each frame.")
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
+
video_input = gr.Video(label="Upload Video", type="filepath") # Use Video for file input
|
| 148 |
+
video_output = gr.Video(label="Processed Video") # Output the processed video
|
| 149 |
+
excel_output = gr.File(label="Excel File with Detected License Plates") # Output the Excel file with detected text
|
| 150 |
|
| 151 |
video_input.upload(process_video, inputs=video_input, outputs=[video_output, excel_output])
|
| 152 |
|