arsath-sm commited on
Commit
6d35603
·
verified ·
1 Parent(s): 115f5f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Resize image
17
+ image = cv2.resize(image, target_size)
18
+ # Normalize
19
+ image = image.astype(np.float32) / 255.0
20
+ # Transpose for ONNX input
21
+ image = np.transpose(image, (2, 0, 1))
22
+ # Add batch dimension
23
+ image = np.expand_dims(image, axis=0)
24
+ return image
25
+
26
+ def postprocess_results(output, image_shape, confidence_threshold=0.25, iou_threshold=0.45):
27
+ # Assuming YOLO v5 output format
28
+ boxes = output[0]
29
+ scores = output[1]
30
+ class_ids = output[2]
31
+
32
+ # Filter by confidence
33
+ mask = scores > confidence_threshold
34
+ boxes = boxes[mask]
35
+ scores = scores[mask]
36
+ class_ids = class_ids[mask]
37
+
38
+ # Apply NMS
39
+ indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), confidence_threshold, iou_threshold)
40
+
41
+ results = []
42
+ for i in indices:
43
+ box = boxes[i]
44
+ score = scores[i]
45
+ class_id = class_ids[i]
46
+ x, y, w, h = box
47
+ x1 = int(x * image_shape[1])
48
+ y1 = int(y * image_shape[0])
49
+ x2 = int((x + w) * image_shape[1])
50
+ y2 = int((y + h) * image_shape[0])
51
+ results.append((x1, y1, x2, y2, score, class_id))
52
+
53
+ return results
54
+
55
+ def process_image(image):
56
+ orig_image = image.copy()
57
+ processed_image = preprocess_image(image)
58
+
59
+ # Run inference
60
+ inputs = {ort_session.get_inputs()[0].name: processed_image}
61
+ outputs = ort_session.run(None, inputs)
62
+
63
+ results = postprocess_results(outputs, image.shape)
64
+
65
+ # Draw bounding boxes on the image
66
+ for x1, y1, x2, y2, score, class_id in results:
67
+ cv2.rectangle(orig_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
68
+ label = f"License Plate: {score:.2f}"
69
+ cv2.putText(orig_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
70
+
71
+ return orig_image
72
+
73
+ def process_video(video_path):
74
+ cap = cv2.VideoCapture(video_path)
75
+
76
+ # Get video properties
77
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
78
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
79
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
80
+
81
+ # Create a temporary file to store the processed video
82
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
83
+ out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
84
+
85
+ while cap.isOpened():
86
+ ret, frame = cap.read()
87
+ if not ret:
88
+ break
89
+
90
+ processed_frame = process_image(frame)
91
+ out.write(processed_frame)
92
+
93
+ cap.release()
94
+ out.release()
95
+
96
+ return temp_file.name
97
+
98
+ st.title("License Plate Detection")
99
+
100
+ uploaded_file = st.file_uploader("Choose an image or video file", type=["jpg", "jpeg", "png", "mp4"])
101
+
102
+ if uploaded_file is not None:
103
+ file_type = uploaded_file.type.split('/')[0]
104
+
105
+ if file_type == "image":
106
+ image = Image.open(uploaded_file)
107
+ image = np.array(image)
108
+
109
+ st.image(image, caption="Uploaded Image", use_column_width=True)
110
+
111
+ if st.button("Detect License Plates"):
112
+ processed_image = process_image(image)
113
+ st.image(processed_image, caption="Processed Image", use_column_width=True)
114
+
115
+ elif file_type == "video":
116
+ tfile = tempfile.NamedTemporaryFile(delete=False)
117
+ tfile.write(uploaded_file.read())
118
+
119
+ st.video(tfile.name)
120
+
121
+ if st.button("Detect License Plates"):
122
+ processed_video = process_video(tfile.name)
123
+ st.video(processed_video)
124
+
125
+ st.write("Upload an image or video to detect license plates.")