MKgoud commited on
Commit
3f5bffe
·
verified ·
1 Parent(s): 1386031

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements.txt +0 -0
  2. streamlit.py +113 -0
requirements.txt ADDED
Binary file (224 Bytes). View file
 
streamlit.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from ultralytics import YOLO
3
+ import numpy as np
4
+ import cv2
5
+ from PIL import Image
6
+
7
+ # Model labels
8
+ model1Labels = {0: 'single_number_plate', 1: 'double_number_plate'}
9
+
10
+ model2Labels = {
11
+ 0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C',
12
+ 13: 'D', 14: 'E', 15: 'F', 16: 'G', 17: 'H', 18: 'I', 19: 'J', 20: 'K', 21: 'L', 22: 'M', 23: 'N', 24: 'O',
13
+ 25: 'P', 26: 'Q', 27: 'R', 28: 'S', 29: 'T', 30: 'U', 31: 'V', 32: 'W', 33: 'X', 34: 'Y', 35: 'Z'
14
+ }
15
+
16
+ # Load models
17
+ model = YOLO("models/LP-detection.pt")
18
+ model2 = YOLO("models/Charcter-LP.pt")
19
+
20
+ def prediction(image):
21
+ result = model.predict(source=image, conf=0.5)
22
+ boxes = result[0].boxes
23
+ height = boxes.xywh
24
+ crd = boxes.data
25
+ print(crd)
26
+
27
+ n = len(crd)
28
+ lp_number = []
29
+ img_lp_final = None # To hold the last cropped license plate image
30
+
31
+ for i in range(n):
32
+ ht = int(height[i][3])
33
+ c = int(crd[i][5])
34
+
35
+ xmin = int(crd[i][0])
36
+ ymin = int(crd[i][1])
37
+ xmax = int(crd[i][2])
38
+ ymax = int(crd[i][3])
39
+
40
+ img_lp = image[ymin:ymax, xmin:xmax]
41
+ img_lp_final = img_lp.copy() # Store the cropped image for display
42
+ cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
43
+
44
+ h = np.median(ht)
45
+
46
+ # Second Model Prediction
47
+ result2 = model2.predict(source=img_lp, conf=0.3)
48
+ boxes_ocr = result2[0].boxes
49
+ data2 = boxes_ocr.data
50
+ print(data2)
51
+
52
+ n2 = len(data2)
53
+ xaxis0, xaxis11, xaxis12 = [], [], []
54
+ label0, label11, label12 = [], [], []
55
+ numberPlate = ""
56
+
57
+ if c == 0: # Single line license plate
58
+ for i in range(n2):
59
+ x = int(data2[i][2])
60
+ xaxis0.append(x)
61
+ l = int(data2[i][5])
62
+ label0.append(l)
63
+
64
+ # Sort characters by x-axis for single line
65
+ sorted_labels = [label0[i] for i in np.argsort(xaxis0)]
66
+ numberPlate = ''.join([model2Labels.get(l) for l in sorted_labels])
67
+ lp_number.append(numberPlate)
68
+
69
+ elif c == 1: # Double line license plate
70
+ for i in range(n2):
71
+ x = int(data2[i][0])
72
+ y = int(data2[i][3])
73
+ l = int(data2[i][5])
74
+ if y < (h / 2):
75
+ xaxis11.append(x)
76
+ label11.append(l)
77
+ else:
78
+ xaxis12.append(x)
79
+ label12.append(l)
80
+
81
+ # Sort characters by x-axis for double line (upper and lower separately)
82
+ sorted_labels11 = [label11[i] for i in np.argsort(xaxis11)]
83
+ sorted_labels12 = [label12[i] for i in np.argsort(xaxis12)]
84
+ numberPlate = ''.join([model2Labels.get(l) for l in sorted_labels11 + sorted_labels12])
85
+ lp_number.append(numberPlate)
86
+
87
+ return lp_number, img_lp_final # Return the last cropped license plate image
88
+
89
+ st.title('License Plate Recognition 🚗')
90
+ st.header('Upload an image of a license plate to get the License number.')
91
+
92
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
93
+ if uploaded_file is not None:
94
+ c1, c2, c3 = st.columns(3) # Create three columns
95
+
96
+ # Convert the uploaded file to an OpenCV image
97
+ image = np.array(Image.open(uploaded_file))
98
+
99
+ with c1:
100
+ st.image(image, caption='Uploaded Image', use_column_width=True)
101
+
102
+ # Run prediction
103
+ license_plate_text, img_lp = prediction(image)
104
+
105
+ with c2:
106
+ if img_lp is not None:
107
+ st.image(img_lp, caption='Cropped License Plate', use_column_width=True)
108
+ else:
109
+ st.write('No License Plate Detected')
110
+ with c3:
111
+ st.success(', '.join(license_plate_text))
112
+ st.write('License Plate Text')
113
+