Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
import requests
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Download YOLOv8 model from Hugging Face
|
| 9 |
+
model_url = "https://huggingface.co/krishnamishra8848/Nepal_Vehicle_License_Plates_Detection_Version2/resolve/main/best.pt"
|
| 10 |
+
model_path = "best.pt"
|
| 11 |
+
|
| 12 |
+
# Download the model if not already downloaded
|
| 13 |
+
if not os.path.exists(model_path):
|
| 14 |
+
st.info("Downloading model from Hugging Face...")
|
| 15 |
+
with open(model_path, "wb") as f:
|
| 16 |
+
response = requests.get(model_url)
|
| 17 |
+
f.write(response.content)
|
| 18 |
+
st.success("Model downloaded successfully!")
|
| 19 |
+
|
| 20 |
+
# Load the YOLOv8 model
|
| 21 |
+
model = YOLO(model_path)
|
| 22 |
+
|
| 23 |
+
# App title
|
| 24 |
+
st.title("Nepal Vehicle License Plate Detection")
|
| 25 |
+
st.write("Upload an image to detect vehicle license plates along with their confidence scores.")
|
| 26 |
+
|
| 27 |
+
# Upload image
|
| 28 |
+
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 29 |
+
|
| 30 |
+
if uploaded_image is not None:
|
| 31 |
+
# Display the uploaded image
|
| 32 |
+
image = Image.open(uploaded_image)
|
| 33 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 34 |
+
|
| 35 |
+
# Run YOLOv8 inference
|
| 36 |
+
with st.spinner("Running detection..."):
|
| 37 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 38 |
+
temp_file.write(uploaded_image.read())
|
| 39 |
+
results = model(temp_file.name)
|
| 40 |
+
|
| 41 |
+
# Draw bounding boxes and confidence scores on the image
|
| 42 |
+
draw = ImageDraw.Draw(image)
|
| 43 |
+
results_table = []
|
| 44 |
+
for box in results[0].boxes:
|
| 45 |
+
# Get bounding box coordinates and confidence score
|
| 46 |
+
x_min, y_min, x_max, y_max = map(int, box.xyxy[0].tolist())
|
| 47 |
+
confidence = box.conf.item()
|
| 48 |
+
label = f"Plate: {confidence:.2f}"
|
| 49 |
+
|
| 50 |
+
# Draw rectangle and label
|
| 51 |
+
draw.rectangle([(x_min, y_min), (x_max, y_max)], outline="red", width=3)
|
| 52 |
+
draw.text((x_min, y_min - 10), label, fill="red")
|
| 53 |
+
|
| 54 |
+
# Append detection to the table
|
| 55 |
+
results_table.append({"x_min": x_min, "y_min": y_min, "x_max": x_max, "y_max": y_max, "confidence": confidence})
|
| 56 |
+
|
| 57 |
+
# Display the resulting image with bounding boxes
|
| 58 |
+
st.image(image, caption="Detected Image", use_column_width=True)
|
| 59 |
+
|
| 60 |
+
# Show individual detections in a table
|
| 61 |
+
st.write("### Detection Results")
|
| 62 |
+
st.write(results_table)
|