krishnamishra8848 commited on
Commit
41356b7
·
verified ·
1 Parent(s): 4fcae56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -59
app.py CHANGED
@@ -1,68 +1,54 @@
 
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
- # Save the uploaded image to a temporary file
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(temp_file.name)
38
  st.image(image, caption="Uploaded Image", use_column_width=True)
39
 
40
- # Run YOLOv8 inference
41
- with st.spinner("Running detection..."):
42
- results = model(temp_file.name)
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
- # Remove temporary files (optional cleanup)
68
- os.unlink(temp_file.name)
 
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)