OmidSakaki commited on
Commit
983e917
Β·
verified Β·
1 Parent(s): 158d098

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +112 -16
main.py CHANGED
@@ -1,5 +1,9 @@
1
  import streamlit as st
2
  import tensorflow as tf
 
 
 
 
3
  from PIL import ImageOps, Image
4
  import os # To work with operation system commands
5
  import cv2 # To process images
@@ -13,6 +17,24 @@ from ultralytics import YOLO # To Create Yolo model
13
  from termcolor import colored # To colorfull outputs
14
  import matplotlib.pyplot as plt # To visualizations
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  #load weights
17
  best_model_plate = YOLO('best.pt')
18
  best_model_digits = YOLO('best2.pt')
@@ -71,9 +93,8 @@ def FINAL(img) :
71
  result is digits and char on car plate.
72
  '''
73
  # Read car image ( STEP-1 )
74
- #img = cv2.imread(img)
75
- #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
76
- img = Image.open(img).convert('RGB')
77
 
78
  # First prediction -> Detect car-plate ( STEP-2 )
79
  result1, _ = Detect_Plate(img)
@@ -95,19 +116,94 @@ def FINAL(img) :
95
 
96
  Plot_Result(img, raw_plate, digits)
97
 
98
- # set title
99
- st.title('Persian Plates Digits Detection')
 
100
 
101
- # set header
102
- st.header('Please upload image')
 
 
 
 
103
 
104
- # upload file
105
- img = st.file_uploader('', type=['jpeg', 'jpg', 'png'])
106
 
107
- # display image
108
- if img is not None:
109
- image = Image.open(img).convert('RGB')
110
- st.image(image, use_column_width=True)
111
-
112
-
113
- st.write(FINAL(img))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import tensorflow as tf
3
+ import streamlit as st
4
+ import argparse
5
+ import io
6
+ from io import BytesIO
7
  from PIL import ImageOps, Image
8
  import os # To work with operation system commands
9
  import cv2 # To process images
 
17
  from termcolor import colored # To colorfull outputs
18
  import matplotlib.pyplot as plt # To visualizations
19
 
20
+ st.set_page_config(
21
+ page_title="Auto NPR",
22
+ page_icon="✨",
23
+ layout="centered",
24
+ initial_sidebar_state="expanded",
25
+ )
26
+
27
+ top_image = Image.open('banner_top.png')
28
+ bottom_image = Image.open('banner_bottom.png')
29
+ main_image = Image.open('main_banner.png')
30
+
31
+ st.image(main_image,use_column_width='auto')
32
+ st.title(' Automatic Number Plate Recognition πŸš˜πŸš™')
33
+ st.sidebar.image(top_image,use_column_width='auto')
34
+ st.sidebar.header('Input πŸ› ')
35
+ selected_type = st.sidebar.selectbox('Please select an activity type πŸš€', ["Upload Image", "Live Video Feed"])
36
+ st.sidebar.image(bottom_image,use_column_width='auto')
37
+
38
  #load weights
39
  best_model_plate = YOLO('best.pt')
40
  best_model_digits = YOLO('best2.pt')
 
93
  result is digits and char on car plate.
94
  '''
95
  # Read car image ( STEP-1 )
96
+ img = cv2.imread(img)
97
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
98
 
99
  # First prediction -> Detect car-plate ( STEP-2 )
100
  result1, _ = Detect_Plate(img)
 
116
 
117
  Plot_Result(img, raw_plate, digits)
118
 
119
+ if selected_type == "Upload Image":
120
+ st.info('✨ Supports all popular image formats πŸ“· - PNG, JPG, BMP πŸ˜‰')
121
+ uploaded_file = st.file_uploader("Upload Image of car's number plate πŸš“", type=["png","jpg","bmp","jpeg"])
122
 
123
+ if uploaded_file is not None:
124
+ with open(os.path.join(upload_path,uploaded_file.name),"wb") as f:
125
+ f.write((uploaded_file).getbuffer())
126
+ with st.spinner(f"Working... πŸ’«"):
127
+ uploaded_image = os.path.abspath(os.path.join(upload_path,uploaded_file.name))
128
+ downloaded_image = os.path.abspath(os.path.join(download_path,str("output_"+uploaded_file.name)))
129
 
130
+ with open(uploaded_image,'rb') as imge:
131
+ img_bytes = imge.read()
132
 
133
+ img = Image.open(io.BytesIO(img_bytes))
134
+ results = FINAL(img)
135
+
136
+ for img in results.imgs:
137
+ img_base64 = Image.fromarray(img)
138
+ img_base64.save(downloaded_image, format="JPEG")
139
+
140
+ final_image = Image.open(downloaded_image)
141
+ print("Opening ",final_image)
142
+ st.markdown("---")
143
+ st.image(final_image, caption='This is how your final image looks like πŸ˜‰')
144
+ with open(downloaded_image, "rb") as file:
145
+ if uploaded_file.name.endswith('.jpg') or uploaded_file.name.endswith('.JPG'):
146
+ if st.download_button(
147
+ label="Download Output Image πŸ“·",
148
+ data=file,
149
+ file_name=str("output_"+uploaded_file.name),
150
+ mime='image/jpg'
151
+ ):
152
+ download_success()
153
+ if uploaded_file.name.endswith('.jpeg') or uploaded_file.name.endswith('.JPEG'):
154
+ if st.download_button(
155
+ label="Download Output Image πŸ“·",
156
+ data=file,
157
+ file_name=str("output_"+uploaded_file.name),
158
+ mime='image/jpeg'
159
+ ):
160
+ download_success()
161
+
162
+ if uploaded_file.name.endswith('.png') or uploaded_file.name.endswith('.PNG'):
163
+ if st.download_button(
164
+ label="Download Output Image πŸ“·",
165
+ data=file,
166
+ file_name=str("output_"+uploaded_file.name),
167
+ mime='image/png'
168
+ ):
169
+ download_success()
170
+
171
+ if uploaded_file.name.endswith('.bmp') or uploaded_file.name.endswith('.BMP'):
172
+ if st.download_button(
173
+ label="Download Output Image πŸ“·",
174
+ data=file,
175
+ file_name=str("output_"+uploaded_file.name),
176
+ mime='image/bmp'
177
+ ):
178
+ download_success()
179
+ else:
180
+ st.warning('⚠ Please upload your Image 😯')
181
+
182
+ else:
183
+ st.info('✨ The Live Feed from Web-Camera will take some time to load up 🎦')
184
+ live_feed = st.checkbox('Start Web-Camera βœ…')
185
+ FRAME_WINDOW = st.image([])
186
+ cap = cv2.VideoCapture(0)
187
+
188
+ if live_feed:
189
+ while(cap.isOpened()):
190
+ success, frame = cap.read()
191
+ if success == True:
192
+ ret,buffer=cv2.imencode('.jpg',frame)
193
+ frame=buffer.tobytes()
194
+ img = Image.open(io.BytesIO(frame))
195
+ model = instantiate_model()
196
+ results = model(img, size=640)
197
+ results.print()
198
+ img = np.squeeze(results.render())
199
+ img_BGR = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
200
+ else:
201
+ break
202
+ frame = cv2.imencode('.jpg', img_BGR)[1].tobytes()
203
+ FRAME_WINDOW.image(frame)
204
+ else:
205
+ cap.release()
206
+ cv2.destroyAllWindows()
207
+ st.warning('⚠ The Web-Camera is currently disabled. 😯')
208
+
209
+ st.markdown("<br><hr><center>Made with ❀️ by <a href='mailto:[email protected]?subject=Automatic Number Plate Recognition WebApp!&body=Please specify the issue you are facing with the app.'><strong>Prateek Ralhan</strong></a></center><hr>", unsafe_allow_html=True)