krishnamishra8848 commited on
Commit
a1c2179
·
verified ·
1 Parent(s): e50eca6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -31
app.py CHANGED
@@ -1,51 +1,88 @@
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="last.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
- # Run detection directly after uploading
46
- st.write("Processing...")
47
  image = Image.open(uploaded_file)
48
- result_image = detect_license_plate(image)
49
 
50
- # Display the detection result
51
- st.image(result_image, caption="Detection Results", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from huggingface_hub import hf_hub_download
3
  from ultralytics import YOLO
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
+ import matplotlib.pyplot as plt
8
+ import tempfile
9
+ import os
10
 
11
+ # Title for the Streamlit App
12
+ st.title("Nepal Vehicle License Plate and Character Detection")
13
 
14
+ # Description
15
+ st.write("Upload an image to detect license plates and their characters using two advanced YOLO models.")
16
 
17
+ # Download YOLO model weights from Hugging Face
18
+ @st.cache_resource
19
+ def load_models():
20
+ # Full license plate detection model
21
+ full_plate_model_path = hf_hub_download(
22
+ repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="last.pt"
23
+ )
24
+ full_plate_model = YOLO(full_plate_model_path)
25
 
26
+ # Character detection model
27
+ character_model_path = hf_hub_download(
28
+ repo_id="krishnamishra8848/Nepal_Vehicle_License_Plates_Detection_Version3", filename="best.pt"
29
+ )
30
+ character_model = YOLO(character_model_path)
31
 
32
+ return full_plate_model, character_model
33
+
34
+ # Load models
35
+ full_plate_model, character_model = load_models()
36
+
37
+ # Function to detect and crop license plates
38
+ def detect_and_crop_license_plate(image):
39
+ img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
40
+ results = full_plate_model(img_bgr)
41
+
42
+ cropped_images = []
43
  for result in results:
44
  if hasattr(result, 'boxes') and result.boxes is not None:
45
+ for box in result.boxes.xyxy:
46
+ x1, y1, x2, y2 = map(int, box)
47
+ cropped_image = img_bgr[y1:y2, x1:x2]
48
+ cropped_images.append(cropped_image)
 
49
 
50
+ return cropped_images, img_bgr
51
+
52
+ # Function to detect characters
53
+ def detect_characters(image):
54
+ results = character_model(image)
55
+ for result in results:
56
+ if hasattr(result, 'boxes') and result.boxes is not None:
57
+ for box in result.boxes.xyxy:
58
+ x1, y1, x2, y2 = map(int, box)
59
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
60
 
61
+ return image
 
 
62
 
63
+ # Upload an image file
64
+ uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
65
 
66
  if uploaded_file is not None:
67
+ # Load image
 
68
  image = Image.open(uploaded_file)
 
69
 
70
+ # Display uploaded image
71
+ st.image(image, caption="Uploaded Image", use_column_width=True)
72
+
73
+ # Detect license plates and characters
74
+ with st.spinner("Processing image..."):
75
+ cropped_plates, original_image = detect_and_crop_license_plate(image)
76
+
77
+ if cropped_plates:
78
+ st.write(f"Detected {len(cropped_plates)} license plate(s). Showing results:")
79
+ for idx, cropped_image in enumerate(cropped_plates, 1):
80
+ st.write(f"License Plate {idx}:")
81
+ annotated_plate = detect_characters(cropped_image.copy())
82
+ st.image(cv2.cvtColor(annotated_plate, cv2.COLOR_BGR2RGB), caption=f"License Plate {idx} with Characters", use_column_width=True)
83
+ else:
84
+ st.write("No license plates detected. Running character detection on the full image.")
85
+ annotated_image = detect_characters(original_image.copy())
86
+ st.image(cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB), caption="Full Image with Characters", use_column_width=True)
87
+
88
+ st.success("Processing complete!")