Spaces:
Sleeping
Sleeping
File size: 14,147 Bytes
1266fc8 ff16f65 1266fc8 e597aa5 1266fc8 ff16f65 1266fc8 ff16f65 e597aa5 1266fc8 653be31 1266fc8 2b1a056 1266fc8 e597aa5 1266fc8 e597aa5 ce4e9d2 e597aa5 1266fc8 e597aa5 1266fc8 e597aa5 1266fc8 e597aa5 1266fc8 e597aa5 1266fc8 e597aa5 1266fc8 e597aa5 1266fc8 2b1a056 1266fc8 e597aa5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
import numpy as np
import pandas as pd
from ultralytics import YOLO
import streamlit as st
import cv2
import base64
import time
import shutil
import os
from PIL import Image
import base64
import random
st.set_page_config(layout="wide",initial_sidebar_state="expanded",
page_icon='π',page_title='Poth-Hole Detector')
image_directory = "val" # Assuming "val" is the directory name
# Get a list of image filenames in the directory
image_filenames = [filename for filename in os.listdir(image_directory) if filename.endswith(".jpg")]
# Function to generate a random image from the list of filenames
def get_random_image():
if not image_filenames:
return None
random_image_filename = random.choice(image_filenames)
random_image_path = os.path.join(image_directory, random_image_filename)
return random_image_path
def get_video_base64(video_path):
with open(video_path, "rb") as file:
video_bytes = file.read()
base64_encoded = base64.b64encode(video_bytes).decode("utf-8")
return base64_encoded
video_path = "deep1.mp4"
video_base64 = get_video_base64(video_path)
video_html = f"""
<style>
#myVideo {{
position: fixed;
right: 0;
bottom: 0;
min-width: 100%;
min-height: 100%;
}}
.content {{
position: fixed;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
color: #f1f1f1;
width: 100%;
padding: 20px;
}}
</style>
<video autoplay loop muted id="myVideo">
<source type="video/mp4" src="data:video/mp4;base64,{video_base64}">
</video>
"""
st.markdown(video_html, unsafe_allow_html=True)
# Define custom style for the glowing text
glowing_text_style = '''
<style>
.glowing-text {
font-family: 'Arial Black', sans-serif;
font-size: 48px;
text-align: center;
animation: glowing 2s infinite;
}
@keyframes glowing {
0% { color: #FF9933; } /* Saffron color */
25% { color: #FFFFFF; } /* White color */
50% { color: #128807; } /* Green color */
75% { color: #0000FF; } /* Blue color */
100% { color: #FF9933; } /* Saffron color */
}
</style>
'''
# Display the glowing text using st.markdown
st.markdown(glowing_text_style, unsafe_allow_html=True)
st.markdown(f'<p class="glowing-text">π³οΈ PothHole Detector π³οΈ</p>', unsafe_allow_html=True)
def upload():
image=None
image_filename=None
initial_image = st.camera_input('Take a picture')
original_image = initial_image
temp_path = None
if initial_image is not None:
image_filename = f"{int(time.time())}.jpg"
bytes_data = initial_image.getvalue()
image = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
return image, original_image,image_filename
def process_line(line, image_np,counter):
# Process a single line from the labels.txt file
bresults = line.split()
if len(bresults) >=5:
names={0:'POTH_HOLE'}
xc, yc, nw, nh = map(float, bresults[1:5])
h, w = image_np.shape[0], image_np.shape[1]
xc *= w
yc *= h
nw *= w
nh *= h
top_left = (int(xc - nw / 2), int(yc - nh / 2))
bottom_right = (int(xc + nw / 2), int(yc + nh / 2))
# Draw bounding box
cv2.rectangle(image_np, top_left, bottom_right, (4, 29, 255), 3, cv2.LINE_4)
# Draw label text
#label = names[int(bresults[0])]
label = f'{names[int(bresults[0])]}-{counter}'
text_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 2, 3)[0]
text_width, text_height = text_size
text_x = (top_left[0] + bottom_right[0] - text_width) // 2 + 100
text_y = top_left[1] - 10
cv2.putText(image_np, label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
sidebar_option = st.sidebar.radio("Select an option", ("Take picture for prediction", "Upload file"))
def main():
if sidebar_option == "Take picture for prediction":
if st.checkbox('Take a picture for prediction'):
image, original_image,image_filename= upload()
if original_image is not None and original_image is not None and len(image_filename)!=0 and st.button('Prediction'): # Check if original_image is not None
st.info('Wait for the results...!')
#image1=cv2.imread(image)
counter=1
names={0:'POTH_HOLE'
}
model=YOLO('best.pt')
result = model.predict(image,save=True,save_txt=True)
txt_files_exist = any(filename.endswith('.txt') for filename in os.listdir('runs/detect/predict/labels'))
if txt_files_exist:
lis=open('runs/detect/predict/labels/image0.txt','r').readlines()
for line in lis:
process_line(line, image,counter)
counter+=1
with st.spinner('Wait for the results...!'):
time.sleep(5)
st.image(image,use_column_width=True)
st.balloons()
try:
if os.path.exists('runs'):
shutil.rmtree('runs')
st.session_state.original_image = None # Clear the original_image variable
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning('β οΈPlease check your image')
st.info("π·β¨ **Encountering the 'Please check your image' error?**")
st.write(
"""
Our algorithm may not have been able to predict the content of your image. To improve results, consider the following:
π **Verify image quality and resolution.**
π **Ensure the image is clear and well-lit.**
π **Check if the image meets our specified format requirements.**
π **Consider alternative images for better results.**
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! π€π§
"""
)
try:
if os.path.exists('runs'):
shutil.rmtree('runs')
st.session_state.original_image = None # Clear the original_image variable
except Exception as e:
st.error(f"An error occurred: {e}")
elif sidebar_option == "Upload file":
fileimage=st.file_uploader('Upload the file for detection π',type=['jpg','jpeg','png'])
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 π§.")
if st.button('Predict'):
if True:
if fileimage is None:
default_image=get_random_image()
st.warning('β οΈ We are using random image from our backend!.')
st.info('Wait for the results...!')
counter=1
pic=Image.open(default_image)
image_np = np.array(pic)
names={0:'POTH_HOLE'
}
mod1=YOLO('best.pt')
mod1.predict(image_np,save=True,save_txt=True)
txt_files_exist = any(filename.endswith('.txt') for filename in os.listdir('runs/detect/predict/labels'))
if txt_files_exist:
lis=open('runs/detect/predict/labels/image0.txt','r').readlines()
with st.spinner('Wait for the results...!'):
time.sleep(5)
for line in lis:
process_line(line, image_np,counter)
counter+=1
col1,col2=st.columns(2)
with col1:
st.info('Original Image!')
st.image(default_image,use_column_width=True)
with col2:
st.info('Detected Image!')
st.image(image_np,use_column_width=True)
st.balloons()
try:
if os.path.exists('runs'):
shutil.rmtree('runs')
st.session_state.original_image = None # Clear the original_image variable
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning('β οΈPlease check your image')
st.info("π·β¨ **Encountering the 'Please check your image' error?**")
st.write(
"""
Our algorithm may not have been able to predict the content of your image. To improve results, consider the following:
π **Verify image quality and resolution.**
π **Ensure the image is clear and well-lit.**
π **Check if the image meets our specified format requirements.**
π **Consider alternative images for better results.**
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! π€π§
"""
)
try:
if os.path.exists('runs'):
shutil.rmtree('runs')
st.session_state.original_image = None # Clear the original_image variable
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.info('Wait for the results...!')
counter=1
pic=Image.open(fileimage)
image_np = np.array(pic)
names={0:'POTH_HOLE'
}
mod1=YOLO('best.pt')
mod1.predict(image_np,save=True,save_txt=True)
txt_files_exist = any(filename.endswith('.txt') for filename in os.listdir('runs/detect/predict/labels'))
if txt_files_exist:
lis=open('runs/detect/predict/labels/image0.txt','r').readlines()
with st.spinner('Wait for the results...!'):
time.sleep(5)
for line in lis:
process_line(line, image_np,counter)
counter+=1
col1,col2=st.columns(2)
with col1:
st.info('Original Image!')
st.image(fileimage,use_column_width=True)
with col2:
st.info('Detected Image!')
st.image(image_np,use_column_width=True)
st.balloons()
try:
if os.path.exists('runs'):
shutil.rmtree('runs')
st.session_state.original_image = None # Clear the original_image variable
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning('β οΈPlease check your image')
st.info("π·β¨ **Encountering the 'Please check your image' error?**")
st.write(
"""
Our algorithm may not have been able to predict the content of your image. To improve results, consider the following:
π **Verify image quality and resolution.**
π **Ensure the image is clear and well-lit.**
π **Check if the image meets our specified format requirements.**
π **Consider alternative images for better results.**
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! π€π§
"""
)
try:
if os.path.exists('runs'):
shutil.rmtree('runs')
st.session_state.original_image = None # Clear the original_image variable
except Exception as e:
st.error(f"An error occurred: {e}")
if __name__ == '__main__':
main()
|