Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,9 @@ 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 |
|
|
@@ -24,47 +22,36 @@ def load_ocr_reader():
|
|
| 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 |
-
|
| 28 |
-
|
| 29 |
elif extension in ['.jpg', '.jpeg', '.png']:
|
| 30 |
-
|
| 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 |
-
|
| 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),
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
return output_path
|
| 67 |
-
|
| 68 |
|
| 69 |
def find_plate_on_video(input_path, output_path):
|
| 70 |
model = load_yolo_model()
|
|
@@ -75,11 +62,11 @@ def find_plate_on_video(input_path, output_path):
|
|
| 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 |
|
|
@@ -88,49 +75,39 @@ def find_plate_on_video(input_path, output_path):
|
|
| 88 |
if not ret:
|
| 89 |
break
|
| 90 |
|
| 91 |
-
if frame_idx % skip_frame
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 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 |
-
|
| 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 |
-
#
|
| 127 |
-
|
|
|
|
| 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:
|
|
@@ -138,19 +115,14 @@ if uploaded_file is not None:
|
|
| 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 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
elif path.endswith(('.jpg', '.jpeg', '.png')):
|
| 149 |
st.image(path)
|
| 150 |
else:
|
| 151 |
-
st.error("Error
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 5 |
import easyocr
|
| 6 |
from moviepy import ImageSequenceClip
|
| 7 |
|
|
|
|
| 8 |
st.title("π PlateVision π")
|
| 9 |
st.caption("AI-powered license plate detection & recognition from images and videos")
|
| 10 |
|
|
|
|
| 11 |
os.makedirs("input", exist_ok=True)
|
| 12 |
os.makedirs("output", exist_ok=True)
|
| 13 |
|
|
|
|
| 22 |
def process_and_find_plate(input_path, output_path):
|
| 23 |
extension = os.path.splitext(input_path)[1].lower()
|
| 24 |
if extension in ['.mp4', '.mkv']:
|
| 25 |
+
return find_plate_on_video(input_path, output_path)
|
|
|
|
| 26 |
elif extension in ['.jpg', '.jpeg', '.png']:
|
| 27 |
+
return find_plate_on_image(input_path, output_path)
|
|
|
|
| 28 |
else:
|
| 29 |
st.error("Unsupported file type")
|
| 30 |
return None
|
| 31 |
|
|
|
|
|
|
|
| 32 |
def find_plate_on_image(input_path, output_path):
|
|
|
|
| 33 |
model = load_yolo_model()
|
| 34 |
reader = load_ocr_reader()
|
| 35 |
+
|
| 36 |
image = cv2.imread(input_path)
|
| 37 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 38 |
outputs = model.predict(image, verbose=False)
|
| 39 |
+
|
| 40 |
for output in outputs:
|
| 41 |
for box in output.boxes:
|
| 42 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 43 |
confidence = box.conf[0]
|
| 44 |
roi = image[y1:y2, x1:x2]
|
| 45 |
results = reader.readtext(roi)
|
| 46 |
+
plate_num = results[0][1].strip() if results else "Not Visible!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 48 |
+
cv2.putText(image, f'{confidence*100:.2f}%', (x1, y1 - 20),
|
| 49 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 50 |
+
cv2.putText(image, f'Number: {plate_num}', (x1, y2 + 20),
|
| 51 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 52 |
+
|
| 53 |
+
cv2.imwrite(output_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
|
| 54 |
return output_path
|
|
|
|
| 55 |
|
| 56 |
def find_plate_on_video(input_path, output_path):
|
| 57 |
model = load_yolo_model()
|
|
|
|
| 62 |
st.error("Error opening the video")
|
| 63 |
return None
|
| 64 |
|
| 65 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS)) or 25
|
| 66 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 1
|
| 67 |
+
frames = []
|
| 68 |
frame_idx = 0
|
| 69 |
+
skip_frame = 5 # β
define properly
|
| 70 |
|
| 71 |
progress_bar = st.progress(0, text="π Analyzing video frames...")
|
| 72 |
|
|
|
|
| 75 |
if not ret:
|
| 76 |
break
|
| 77 |
|
| 78 |
+
if frame_idx % skip_frame == 0:
|
| 79 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 80 |
+
outputs = model.predict(rgb_frame, verbose=False)
|
| 81 |
+
|
| 82 |
+
for output in outputs:
|
| 83 |
+
for box in output.boxes:
|
| 84 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 85 |
+
confidence = box.conf[0]
|
| 86 |
+
roi = frame[y1:y2, x1:x2]
|
| 87 |
+
results = reader.readtext(roi)
|
| 88 |
+
plate_num = results[0][1].strip() if results else "Not Visible!"
|
| 89 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 90 |
+
cv2.putText(frame, f'{confidence*100:.2f}%', (x1, y1 - 20),
|
| 91 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 92 |
+
cv2.putText(frame, f'Number: {plate_num}', (x1, y2 + 20),
|
| 93 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 94 |
+
|
| 95 |
+
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
frame_idx += 1
|
| 98 |
progress = min(frame_idx / total_frames, 1.0)
|
| 99 |
progress_bar.progress(progress, text=f"πΈ Processed {frame_idx}/{total_frames} frames...")
|
| 100 |
|
| 101 |
cap.release()
|
| 102 |
progress_bar.empty()
|
| 103 |
|
| 104 |
+
# β
fix undefined variable (use skip_frame)
|
| 105 |
+
output_fps = max(fps // skip_frame, 1)
|
| 106 |
+
clip = ImageSequenceClip(frames, fps=output_fps)
|
| 107 |
clip.write_videofile(output_path, codec='libx264', audio=False, logger=None)
|
| 108 |
|
| 109 |
return output_path
|
| 110 |
|
|
|
|
|
|
|
| 111 |
uploaded_file = st.file_uploader("π€ Upload an image or video", type=['jpg', 'jpeg', 'png', 'mp4', 'mkv'])
|
| 112 |
|
| 113 |
if uploaded_file is not None:
|
|
|
|
| 115 |
output_path = f"output/{uploaded_file.name}"
|
| 116 |
with open(input_path, 'wb') as f:
|
| 117 |
f.write(uploaded_file.getbuffer())
|
| 118 |
+
|
| 119 |
with st.spinner("π¦ Detecting plates... please fasten your seatbelt!"):
|
| 120 |
path = process_and_find_plate(input_path, output_path)
|
| 121 |
|
| 122 |
+
if path and path.endswith(('.mp4', '.mkv')):
|
| 123 |
+
with open(path, 'rb') as video_file:
|
| 124 |
+
st.video(video_file.read())
|
| 125 |
+
elif path and path.endswith(('.jpg', '.jpeg', '.png')):
|
|
|
|
| 126 |
st.image(path)
|
| 127 |
else:
|
| 128 |
+
st.error("Error occurred while processing the file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|