Himanshu2003 commited on
Commit
4feaf1e
·
verified ·
1 Parent(s): 860653b

Upload 8 files

Browse files
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ from ultralytics import YOLO
4
+ import os
5
+ import easyocr
6
+ from moviepy import ImageSequenceClip
7
+
8
+
9
+ st.title("🚗 PlateVision 🔍")
10
+ st.caption("AI-powered license plate detection & recognition from images and videos")
11
+
12
+
13
+ os.makedirs("input", exist_ok=True)
14
+ os.makedirs("output", exist_ok=True)
15
+
16
+ @st.cache_resource
17
+ def load_yolo_model():
18
+ return YOLO("best_model.pt")
19
+
20
+ @st.cache_resource
21
+ def load_ocr_reader():
22
+ return easyocr.Reader(['en'], model_storage_directory='model')
23
+
24
+ def process_and_find_plate(input_path, output_path):
25
+ extension = os.path.splitext(input_path)[1].lower()
26
+ if extension in ['.mp4', '.mkv']:
27
+ path = find_plate_on_video(input_path, output_path)
28
+
29
+ elif extension in ['.jpg', '.jpeg', '.png']:
30
+ path = find_plate_on_image(input_path, output_path)
31
+
32
+ else:
33
+ st.error("Unsupported file type")
34
+ return None
35
+
36
+ return path
37
+
38
+ def find_plate_on_image(input_path, output_path):
39
+
40
+ model = load_yolo_model()
41
+ reader = load_ocr_reader()
42
+
43
+ image = cv2.imread(input_path)
44
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
45
+ outputs = model.predict(image, verbose=False)
46
+
47
+ for output in outputs:
48
+ for box in output.boxes:
49
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
50
+ confidence = box.conf[0]
51
+ roi = image[y1:y2, x1:x2]
52
+ results = reader.readtext(roi)
53
+ try:
54
+ plate_num = results[0][1].strip()
55
+ except Exception as e:
56
+ plate_num = ""
57
+
58
+ if plate_num == "":
59
+ plate_num = "Not Visible!"
60
+
61
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
62
+ cv2.putText(image, f'{confidence*100:.2f}%', (x1, y1-20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA)
63
+ cv2.putText(image, f'Number: {plate_num}', (x1, y2+20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2, cv2.LINE_AA)
64
+
65
+ cv2.imwrite(output_path, image)
66
+ return output_path
67
+
68
+
69
+ def find_plate_on_video(input_path, output_path):
70
+ model = load_yolo_model()
71
+ reader = load_ocr_reader()
72
+
73
+ cap = cv2.VideoCapture(input_path)
74
+ if not cap.isOpened():
75
+ st.error("Error opening the video")
76
+ return None
77
+
78
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
79
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
80
+ frames = []
81
+ frame_idx = 0
82
+ skip_frame = 5
83
+
84
+ progress_bar = st.progress(0, text="🔍 Analyzing video frames...")
85
+
86
+ while cap.isOpened():
87
+ ret, frame = cap.read()
88
+ if not ret:
89
+ break
90
+
91
+ if frame_idx % skip_frame != 0:
92
+ continue
93
+
94
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
95
+ outputs = model.predict(rgb_frame, verbose=False)
96
+
97
+ for output in outputs:
98
+ for box in output.boxes:
99
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
100
+ confidence = box.conf[0]
101
+ roi = frame[y1:y2, x1:x2]
102
+ results = reader.readtext(roi)
103
+ try:
104
+ plate_num = results[0][1].strip()
105
+ except Exception:
106
+ plate_num = ""
107
+
108
+ if plate_num == "":
109
+ plate_num = "Not Visible!"
110
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
111
+ cv2.putText(frame, f'{confidence*100:.2f}%', (x1, y1 - 20),
112
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
113
+ cv2.putText(frame, f'Number: {plate_num}', (x1, y2 + 20),
114
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
115
+
116
+ frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
117
+ frame_idx += 1
118
+
119
+ # Update progress
120
+ progress = min(frame_idx / total_frames, 1.0)
121
+ progress_bar.progress(progress, text=f"📸 Processed {frame_idx}/{total_frames} frames...")
122
+
123
+ cap.release()
124
+ progress_bar.empty()
125
+
126
+ # Write final video using MoviePy
127
+ clip = ImageSequenceClip(frames, fps=fps // frame_skip if fps >= frame_skip else 1)
128
+ clip.write_videofile(output_path, codec='libx264', audio=False, logger=None)
129
+
130
+ return output_path
131
+
132
+
133
+
134
+ uploaded_file = st.file_uploader("📤 Upload an image or video", type=['jpg', 'jpeg', 'png', 'mp4', 'mkv'])
135
+
136
+ if uploaded_file is not None:
137
+ input_path = f"input/{uploaded_file.name}"
138
+ output_path = f"output/{uploaded_file.name}"
139
+ with open(input_path, 'wb') as f:
140
+ f.write(uploaded_file.getbuffer())
141
+ with st.spinner("🚦 Detecting plates... please fasten your seatbelt!"):
142
+ path = process_and_find_plate(input_path, output_path)
143
+
144
+ if path.endswith(('.mp4', '.mkv')):
145
+ video_file = open(path, 'rb')
146
+ video_bytes = video_file.read()
147
+ st.video(video_bytes)
148
+ elif path.endswith(('.jpg', '.jpeg', '.png')):
149
+ st.image(path)
150
+ else:
151
+ st.error("Error occured while proccessing the file.")
152
+
153
+
154
+
155
+
156
+
best_model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4ac0243025d1c33e3a71714baf889304f64100cb53bcd0ee7b368779d102d7c6
3
+ size 6234533
input/car.jpg ADDED
license-plate-detection-using-yolo.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
model/craft_mlt_25k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a5efbfb48b4081100544e75e1e2b57f8de3d84f213004b14b85fd4b3748db17
3
+ size 83152330
model/english_g2.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e2272681d9d67a04e2dff396b6e95077bc19001f8f6d3593c307b9852e1c29e8
3
+ size 15143997
output/car.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit == 1.50.0
2
+ opencv-python == 4.12.0.88
3
+ ultralytics == 8.3.221
4
+ easyocr == 1.7.2
5
+ moviepy == 2.2.1
6
+ imageio-ffmpeg == 0.6.0