Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,54 @@
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from ultralytics import YOLO
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
st.title("Nepal Vehicle License Plate Detection")
|
25 |
-
st.write("Upload an image to detect
|
26 |
-
|
27 |
-
# Upload image
|
28 |
-
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") # Ensure proper file extension
|
33 |
-
temp_file.write(uploaded_image.read())
|
34 |
-
temp_file.close()
|
35 |
|
|
|
36 |
# Display the uploaded image
|
37 |
-
image = Image.open(
|
38 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
39 |
|
40 |
-
# Run
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
# Draw bounding boxes and confidence scores on the image
|
45 |
-
draw = ImageDraw.Draw(image)
|
46 |
-
results_table = []
|
47 |
-
for box in results[0].boxes:
|
48 |
-
# Get bounding box coordinates and confidence score
|
49 |
-
x_min, y_min, x_max, y_max = map(int, box.xyxy[0].tolist())
|
50 |
-
confidence = box.conf.item()
|
51 |
-
label = f"Plate: {confidence:.2f}"
|
52 |
-
|
53 |
-
# Draw rectangle and label
|
54 |
-
draw.rectangle([(x_min, y_min), (x_max, y_max)], outline="red", width=3)
|
55 |
-
draw.text((x_min, y_min - 10), label, fill="red")
|
56 |
-
|
57 |
-
# Append detection to the table
|
58 |
-
results_table.append({"x_min": x_min, "y_min": y_min, "x_max": x_max, "y_max": y_max, "confidence": confidence})
|
59 |
-
|
60 |
-
# Display the resulting image with bounding boxes
|
61 |
-
st.image(image, caption="Detected Image", use_column_width=True)
|
62 |
-
|
63 |
-
# Show individual detections in a table
|
64 |
-
st.write("### Detection Results")
|
65 |
-
st.write(results_table)
|
66 |
|
67 |
-
#
|
68 |
-
|
|
|
1 |
+
# Install necessary libraries
|
2 |
import streamlit as st
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
from ultralytics import YOLO
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Step 1: Download the YOLO model weights from your Hugging Face repository
|
10 |
+
weights_path = hf_hub_download(repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="best.pt")
|
11 |
+
|
12 |
+
# Step 2: Load the YOLO model
|
13 |
+
model = YOLO(weights_path)
|
14 |
+
|
15 |
+
# Step 3: Function to process and display results
|
16 |
+
def detect_license_plate(image):
|
17 |
+
# Convert the PIL image to a numpy array
|
18 |
+
img = np.array(image)
|
19 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
20 |
+
|
21 |
+
# Perform inference
|
22 |
+
results = model(img)
|
23 |
+
|
24 |
+
# Draw bounding boxes and confidence scores
|
25 |
+
for result in results:
|
26 |
+
if hasattr(result, 'boxes') and result.boxes is not None:
|
27 |
+
for box, conf in zip(result.boxes.xyxy, result.boxes.conf):
|
28 |
+
x1, y1, x2, y2 = map(int, box) # Convert to integers
|
29 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) # Green rectangle
|
30 |
+
label = f"{conf:.2f}"
|
31 |
+
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
32 |
+
|
33 |
+
# Convert back to RGB for Streamlit display
|
34 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
35 |
+
return Image.fromarray(img)
|
36 |
+
|
37 |
+
# Streamlit Interface
|
38 |
st.title("Nepal Vehicle License Plate Detection")
|
39 |
+
st.write("Upload an image to detect license plates.")
|
|
|
|
|
|
|
40 |
|
41 |
+
# File uploader
|
42 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
43 |
|
44 |
+
if uploaded_file is not None:
|
45 |
# Display the uploaded image
|
46 |
+
image = Image.open(uploaded_file)
|
47 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
48 |
|
49 |
+
# Run detection
|
50 |
+
st.write("Processing...")
|
51 |
+
result_image = detect_license_plate(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Display the results
|
54 |
+
st.image(result_image, caption="Detection Results", use_column_width=True)
|