Upload 4 files
Browse files- README.md +6 -6
- app.py +147 -0
- model.onnx +3 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
-
short_description:
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: License Plate Detection YOLO11
|
3 |
+
emoji: 🏆
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: yellow
|
6 |
sdk: streamlit
|
7 |
+
sdk_version: 1.39.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
+
short_description: It's our computer vision project
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import onnxruntime as ort
|
5 |
+
from PIL import Image
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
# Load the ONNX model
|
9 |
+
@st.cache_resource
|
10 |
+
def load_model():
|
11 |
+
return ort.InferenceSession("model.onnx")
|
12 |
+
|
13 |
+
ort_session = load_model()
|
14 |
+
|
15 |
+
def preprocess_image(image, target_size=(640, 640)):
|
16 |
+
# Convert PIL Image to numpy array if necessary
|
17 |
+
if isinstance(image, Image.Image):
|
18 |
+
image = np.array(image)
|
19 |
+
|
20 |
+
# Convert RGB to BGR
|
21 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
22 |
+
|
23 |
+
# Resize image
|
24 |
+
image = cv2.resize(image, target_size)
|
25 |
+
# Normalize
|
26 |
+
image = image.astype(np.float32) / 255.0
|
27 |
+
# Transpose for ONNX input
|
28 |
+
image = np.transpose(image, (2, 0, 1))
|
29 |
+
# Add batch dimension
|
30 |
+
image = np.expand_dims(image, axis=0)
|
31 |
+
return image
|
32 |
+
|
33 |
+
def postprocess_results(output, image_shape, confidence_threshold=0.25, iou_threshold=0.45):
|
34 |
+
# Handle different possible output formats
|
35 |
+
if isinstance(output, (list, tuple)):
|
36 |
+
predictions = output[0]
|
37 |
+
elif isinstance(output, np.ndarray):
|
38 |
+
predictions = output
|
39 |
+
else:
|
40 |
+
raise ValueError(f"Unexpected output type: {type(output)}")
|
41 |
+
|
42 |
+
# Reshape if necessary
|
43 |
+
if len(predictions.shape) == 4:
|
44 |
+
predictions = predictions.squeeze((0, 1))
|
45 |
+
elif len(predictions.shape) == 3:
|
46 |
+
predictions = predictions.squeeze(0)
|
47 |
+
|
48 |
+
# Extract boxes, scores, and class_ids
|
49 |
+
boxes = predictions[:, :4]
|
50 |
+
scores = predictions[:, 4]
|
51 |
+
class_ids = predictions[:, 5]
|
52 |
+
|
53 |
+
# Filter by confidence
|
54 |
+
mask = scores > confidence_threshold
|
55 |
+
boxes = boxes[mask]
|
56 |
+
scores = scores[mask]
|
57 |
+
class_ids = class_ids[mask]
|
58 |
+
|
59 |
+
# Convert boxes from [x, y, w, h] to [x1, y1, x2, y2]
|
60 |
+
boxes[:, 2:] += boxes[:, :2]
|
61 |
+
|
62 |
+
# Scale boxes to image size
|
63 |
+
boxes[:, [0, 2]] *= image_shape[1]
|
64 |
+
boxes[:, [1, 3]] *= image_shape[0]
|
65 |
+
|
66 |
+
# Apply NMS
|
67 |
+
indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), confidence_threshold, iou_threshold)
|
68 |
+
|
69 |
+
results = []
|
70 |
+
for i in indices:
|
71 |
+
box = boxes[i]
|
72 |
+
score = scores[i]
|
73 |
+
class_id = class_ids[i]
|
74 |
+
x1, y1, x2, y2 = map(int, box)
|
75 |
+
results.append((x1, y1, x2, y2, float(score), int(class_id)))
|
76 |
+
|
77 |
+
return results
|
78 |
+
|
79 |
+
def process_image(image):
|
80 |
+
orig_image = image.copy()
|
81 |
+
processed_image = preprocess_image(image)
|
82 |
+
|
83 |
+
# Run inference
|
84 |
+
inputs = {ort_session.get_inputs()[0].name: processed_image}
|
85 |
+
outputs = ort_session.run(None, inputs)
|
86 |
+
|
87 |
+
results = postprocess_results(outputs, image.shape)
|
88 |
+
|
89 |
+
# Draw bounding boxes on the image
|
90 |
+
for x1, y1, x2, y2, score, class_id in results:
|
91 |
+
cv2.rectangle(orig_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
92 |
+
label = f"License Plate: {score:.2f}"
|
93 |
+
cv2.putText(orig_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
94 |
+
|
95 |
+
return cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
|
96 |
+
|
97 |
+
def process_video(video_path):
|
98 |
+
cap = cv2.VideoCapture(video_path)
|
99 |
+
|
100 |
+
# Get video properties
|
101 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
102 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
103 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
104 |
+
|
105 |
+
# Create a temporary file to store the processed video
|
106 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
107 |
+
out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
108 |
+
|
109 |
+
while cap.isOpened():
|
110 |
+
ret, frame = cap.read()
|
111 |
+
if not ret:
|
112 |
+
break
|
113 |
+
|
114 |
+
processed_frame = process_image(frame)
|
115 |
+
out.write(cv2.cvtColor(processed_frame, cv2.COLOR_RGB2BGR))
|
116 |
+
|
117 |
+
cap.release()
|
118 |
+
out.release()
|
119 |
+
|
120 |
+
return temp_file.name
|
121 |
+
|
122 |
+
st.title("License Plate Detection")
|
123 |
+
|
124 |
+
uploaded_file = st.file_uploader("Choose an image or video file", type=["jpg", "jpeg", "png", "mp4"])
|
125 |
+
|
126 |
+
if uploaded_file is not None:
|
127 |
+
file_type = uploaded_file.type.split('/')[0]
|
128 |
+
|
129 |
+
if file_type == "image":
|
130 |
+
image = Image.open(uploaded_file)
|
131 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
132 |
+
|
133 |
+
if st.button("Detect License Plates"):
|
134 |
+
processed_image = process_image(np.array(image))
|
135 |
+
st.image(processed_image, caption="Processed Image", use_column_width=True)
|
136 |
+
|
137 |
+
elif file_type == "video":
|
138 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
139 |
+
tfile.write(uploaded_file.read())
|
140 |
+
|
141 |
+
st.video(tfile.name)
|
142 |
+
|
143 |
+
if st.button("Detect License Plates"):
|
144 |
+
processed_video = process_video(tfile.name)
|
145 |
+
st.video(processed_video)
|
146 |
+
|
147 |
+
st.write("Upload an image or video to detect license plates.")
|
model.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f5a5c61b72e901fc8acbec24a57acb957d9aac4dbbd9fdaa80cba8635259fbab
|
3 |
+
size 10585821
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
opencv-python-headless
|
3 |
+
onnxruntime
|
4 |
+
numpy
|
5 |
+
Pillow
|