Vikrant26 commited on
Commit
f93376b
·
verified ·
1 Parent(s): 2664050

Upload 2 files

Browse files
Automatic License Plate detection.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import numpy as np
4
+ from ultralytics import YOLO
5
+ from PIL import Image
6
+ import os
7
+
8
+ # Set the title of the Streamlit app
9
+ st.title("License Plate Detection using YOLO")
10
+
11
+ # Ensure the temp directory exists
12
+ if not os.path.exists("temp"):
13
+ os.makedirs("temp")
14
+
15
+ # Allow users to upload images or videos
16
+ uploaded_file = st.file_uploader("Upload an image or video",
17
+ type=["jpg", "jpeg", "png", "bmp", "mp4", "avi", "mov", "mkv"])
18
+
19
+ # Load YOLO model
20
+ try:
21
+ model = YOLO('best.pt') # Use the relative path to your trained YOLO model
22
+ except Exception as e:
23
+ st.error(f"Error loading YOLO model: {e}")
24
+
25
+
26
+ def predict_and_save_image(path_test_car, output_image_path):
27
+ """
28
+ Predicts and saves the bounding boxes on the given test image using the trained YOLO model.
29
+
30
+ Parameters:
31
+ path_test_car (str): Path to the test image file.
32
+ output_image_path (str): Path to save the output image file.
33
+
34
+ Returns:
35
+ str: The path to the saved output image file.
36
+ """
37
+ try:
38
+ results = model.predict(path_test_car, device='cpu')
39
+ image = cv2.imread(path_test_car)
40
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
41
+ for result in results:
42
+ for box in result.boxes:
43
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
44
+ confidence = box.conf[0]
45
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
46
+ cv2.putText(image, f'{confidence * 100:.2f}%', (x1, y1 - 10),
47
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
48
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
49
+ cv2.imwrite(output_image_path, image)
50
+ return output_image_path
51
+ except Exception as e:
52
+ st.error(f"Error processing image: {e}")
53
+ return None
54
+
55
+
56
+ def predict_and_plot_video(video_path, output_path):
57
+ """
58
+ Predicts and saves the bounding boxes on the given test video using the trained YOLO model.
59
+
60
+ Parameters:
61
+ video_path (str): Path to the test video file.
62
+ output_path (str): Path to save the output video file.
63
+
64
+ Returns:
65
+ str: The path to the saved output video file.
66
+ """
67
+ try:
68
+ cap = cv2.VideoCapture(video_path)
69
+ if not cap.isOpened():
70
+ st.error(f"Error opening video file: {video_path}")
71
+ return None
72
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
73
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
74
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
75
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
76
+ out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
77
+ while cap.isOpened():
78
+ ret, frame = cap.read()
79
+ if not ret:
80
+ break
81
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
82
+ results = model.predict(rgb_frame, device='cpu')
83
+ for result in results:
84
+ for box in result.boxes:
85
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
86
+ confidence = box.conf[0]
87
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
88
+ cv2.putText(frame, f'{confidence * 100:.2f}%', (x1, y1 - 10),
89
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
90
+ out.write(frame)
91
+ cap.release()
92
+ out.release()
93
+ return output_path
94
+ except Exception as e:
95
+ st.error(f"Error processing video: {e}")
96
+ return None
97
+
98
+
99
+ def process_media(input_path, output_path):
100
+ """
101
+ Processes the uploaded media file (image or video) and returns the path to the saved output file.
102
+
103
+ Parameters:
104
+ input_path (str): Path to the input media file.
105
+ output_path (str): Path to save the output media file.
106
+
107
+ Returns:
108
+ str: The path to the saved output media file.
109
+ """
110
+ file_extension = os.path.splitext(input_path)[1].lower()
111
+ if file_extension in ['.mp4', '.avi', '.mov', '.mkv']:
112
+ return predict_and_plot_video(input_path, output_path)
113
+ elif file_extension in ['.jpg', '.jpeg', '.png', '.bmp']:
114
+ return predict_and_save_image(input_path, output_path)
115
+ else:
116
+ st.error(f"Unsupported file type: {file_extension}")
117
+ return None
118
+
119
+
120
+ if uploaded_file is not None:
121
+ input_path = os.path.join("temp", uploaded_file.name)
122
+ output_path = os.path.join("temp", f"output_{uploaded_file.name}")
123
+ try:
124
+ with open(input_path, "wb") as f:
125
+ f.write(uploaded_file.getbuffer())
126
+ st.write("Processing...")
127
+ result_path = process_media(input_path, output_path)
128
+ if result_path:
129
+ if input_path.endswith(('.mp4', '.avi', '.mov', '.mkv')):
130
+ video_file = open(result_path, 'rb')
131
+ video_bytes = video_file.read()
132
+ st.video(video_bytes)
133
+ else:
134
+ image = Image.open(result_path)
135
+ st.image(image)
136
+ except Exception as e:
137
+ st.error(f"Error uploading or processing file: {e}")
requirements (1).txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ numpy
2
+ pandas
3
+ matplotlib
4
+ streamlit
5
+ opencv-python
6
+ ultralytics