gsvc commited on
Commit
e597aa5
β€’
1 Parent(s): ff16f65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -42
app.py CHANGED
@@ -11,6 +11,7 @@ from PIL import Image
11
  import base64
12
  import random
13
 
 
14
  st.set_page_config(layout="wide",initial_sidebar_state="expanded",
15
  page_icon='πŸ”Ž',page_title='Poth-Hole Detector')
16
 
@@ -25,8 +26,8 @@ def get_random_image():
25
  return None
26
  random_image_filename = random.choice(image_filenames)
27
  random_image_path = os.path.join(image_directory, random_image_filename)
28
- return random_image_path
29
-
30
  def get_video_base64(video_path):
31
  with open(video_path, "rb") as file:
32
  video_bytes = file.read()
@@ -194,54 +195,77 @@ def main():
194
  except Exception as e:
195
  st.error(f"An error occurred: {e}")
196
  elif sidebar_option == "Upload file":
197
-
198
 
199
- fileimage=st.file_uploader('Upload the file for detection πŸ“',type=['jpg'])
200
  st.info("If you haven't filed, our system will employ a default image for prediction πŸ“. Simply press the 'Predict' button and directly upload your file for analysis 🧐.")
 
 
201
  if st.button('Predict'):
202
- default_image_path = get_random_image()
203
- if fileimage is None:
204
- st.warning('πŸ›ŽοΈWe are using default image πŸ“·')
205
- counter=1
206
-
207
- pic = Image.open(default_image_path)
208
- image_np = np.array(pic)
209
- names={0:'POTH_HOLE'
 
 
 
 
210
  }
211
- mod1=YOLO('best.pt')
212
- mod1.predict(image_np,save=True,save_txt=True)
213
- txt_files_exist = any(filename.endswith('.txt') for filename in os.listdir('runs/detect/predict/labels'))
214
- if txt_files_exist:
215
- lis=open('runs/detect/predict/labels/image0.txt','r').readlines()
216
- with st.spinner('Wait for the results...!'):
217
- time.sleep(5)
218
 
219
 
220
- for line in lis:
221
- process_line(line, image_np,counter)
222
- counter+=1
223
- col1,col2=st.columns(2)
224
- with col1:
225
 
226
- st.info('Original Image!')
227
- st.image(default_image_path,use_column_width=True)
228
- with col2:
229
- st.info('Detected Image!')
230
- st.image(image_np,use_column_width=True)
231
- st.balloons()
232
 
233
- try:
234
- if os.path.exists('runs'):
235
- shutil.rmtree('runs')
236
- st.session_state.original_image = None # Clear the original_image variable
237
 
238
- except Exception as e:
239
- st.error(f"An error occurred: {e}")
240
-
241
- else:
242
-
243
- if fileimage is not None and not fileimage.name.endswith('.jpg'):
244
- st.warning('⚠️ Please upload only JPG images.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  else:
246
  st.info('Wait for the results...!')
247
  counter=1
@@ -311,4 +335,4 @@ if __name__ == '__main__':
311
 
312
 
313
  main()
314
-
 
11
  import base64
12
  import random
13
 
14
+
15
  st.set_page_config(layout="wide",initial_sidebar_state="expanded",
16
  page_icon='πŸ”Ž',page_title='Poth-Hole Detector')
17
 
 
26
  return None
27
  random_image_filename = random.choice(image_filenames)
28
  random_image_path = os.path.join(image_directory, random_image_filename)
29
+ return random_image_path
30
+
31
  def get_video_base64(video_path):
32
  with open(video_path, "rb") as file:
33
  video_bytes = file.read()
 
195
  except Exception as e:
196
  st.error(f"An error occurred: {e}")
197
  elif sidebar_option == "Upload file":
198
+
199
 
200
+ fileimage=st.file_uploader('Upload the file for detection πŸ“',type=['jpg','jpeg','png'])
201
  st.info("If you haven't filed, our system will employ a default image for prediction πŸ“. Simply press the 'Predict' button and directly upload your file for analysis 🧐.")
202
+
203
+
204
  if st.button('Predict'):
205
+
206
+
207
+ if True:
208
+
209
+ if fileimage is None:
210
+ default_image=get_random_image()
211
+ st.warning('⚠️ We are using random image from our backend!.')
212
+ st.info('Wait for the results...!')
213
+ counter=1
214
+ pic=Image.open(default_image)
215
+ image_np = np.array(pic)
216
+ names={0:'POTH_HOLE'
217
  }
218
+ mod1=YOLO('best.pt')
219
+ mod1.predict(image_np,save=True,save_txt=True)
220
+ txt_files_exist = any(filename.endswith('.txt') for filename in os.listdir('runs/detect/predict/labels'))
221
+ if txt_files_exist:
222
+ lis=open('runs/detect/predict/labels/image0.txt','r').readlines()
223
+ with st.spinner('Wait for the results...!'):
224
+ time.sleep(5)
225
 
226
 
227
+ for line in lis:
228
+ process_line(line, image_np,counter)
229
+ counter+=1
230
+ col1,col2=st.columns(2)
231
+ with col1:
232
 
233
+ st.info('Original Image!')
234
+ st.image(default_image,use_column_width=True)
235
+ with col2:
236
+ st.info('Detected Image!')
237
+ st.image(image_np,use_column_width=True)
238
+ st.balloons()
239
 
240
+ try:
241
+ if os.path.exists('runs'):
242
+ shutil.rmtree('runs')
243
+ st.session_state.original_image = None # Clear the original_image variable
244
 
245
+ except Exception as e:
246
+ st.error(f"An error occurred: {e}")
247
+ else:
248
+ st.warning('⚠️Please check your image')
249
+ st.info("πŸ“·βœ¨ **Encountering the 'Please check your image' error?**")
250
+ st.write(
251
+ """
252
+ Our algorithm may not have been able to predict the content of your image. To improve results, consider the following:
253
+
254
+ πŸ‘‰ **Verify image quality and resolution.**
255
+ πŸ‘‰ **Ensure the image is clear and well-lit.**
256
+ πŸ‘‰ **Check if the image meets our specified format requirements.**
257
+ πŸ‘‰ **Consider alternative images for better results.**
258
+
259
+ Our aim is to provide accurate predictions, and addressing these aspects can make a significant difference. If the issue persists, please reach out to our support team. We're here to help! πŸ€πŸ”§
260
+ """
261
+ )
262
+ try:
263
+ if os.path.exists('runs'):
264
+ shutil.rmtree('runs')
265
+ st.session_state.original_image = None # Clear the original_image variable
266
+
267
+ except Exception as e:
268
+ st.error(f"An error occurred: {e}")
269
  else:
270
  st.info('Wait for the results...!')
271
  counter=1
 
335
 
336
 
337
  main()
338
+