Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,148 +1,58 @@
|
|
1 |
-
import PIL
|
2 |
import streamlit as st
|
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 |
-
st.
|
51 |
-
|
52 |
-
st.error(ex)
|
53 |
-
|
54 |
-
if st.sidebar.button('Detect Objects'):
|
55 |
-
res = model.predict(uploaded_image,
|
56 |
-
conf=confidence,
|
57 |
-
line_width=1,
|
58 |
-
show_labels=False,
|
59 |
-
show_conf=False
|
60 |
-
)
|
61 |
-
boxes = res[0].boxes
|
62 |
-
res_plotted = res[0].plot(labels=False, line_width=1)[:, :, ::-1]
|
63 |
-
with col2:
|
64 |
-
st.image(res_plotted,
|
65 |
-
caption='Detected Image',
|
66 |
-
width=image_width
|
67 |
-
)
|
68 |
-
try:
|
69 |
-
st.write(f'Number of empty slots detected: {len(boxes)}')
|
70 |
-
with st.expander("Detection Results (xywh)"):
|
71 |
-
for box in boxes:
|
72 |
-
st.write(box.xywh)
|
73 |
-
except Exception as ex:
|
74 |
-
st.write("No image is uploaded yet!")
|
75 |
-
import PIL
|
76 |
-
import streamlit as st
|
77 |
-
from ultralytics import YOLO
|
78 |
-
|
79 |
-
# Give the path of the best.pt (best weights)
|
80 |
-
model_path = 'best.pt'
|
81 |
-
|
82 |
-
# Setting page layout
|
83 |
-
st.set_page_config(
|
84 |
-
page_title="NISH(Neural Intelligence Synthesis Hub)", # Setting page title
|
85 |
-
page_icon="NK logo.jpeg", # Setting page icon
|
86 |
-
layout="wide", # Setting layout to wide
|
87 |
-
initial_sidebar_state="expanded", # Expanding sidebar by default
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
"Upload an image...", type=("jpg", "jpeg", "png", 'bmp', 'webp'))
|
97 |
-
|
98 |
-
# Model Options
|
99 |
-
confidence = float(st.slider(
|
100 |
-
"Select Model Confidence", 25, 100, 40)) / 100
|
101 |
-
|
102 |
-
# Creating main page heading
|
103 |
-
st.title("Object Detection")
|
104 |
-
st.caption('Updload a photo by selecting :blue[Browse files]')
|
105 |
-
st.caption('Then click the :blue[Detect Objects] button and check the result.')
|
106 |
-
# Creating two columns on the main page
|
107 |
-
col1, col2 = st.columns(2)
|
108 |
-
|
109 |
-
# Adding image to the first column if image is uploaded
|
110 |
-
with col1:
|
111 |
-
if source_img:
|
112 |
-
# Opening the uploaded image
|
113 |
-
uploaded_image = PIL.Image.open(source_img)
|
114 |
-
image_width, image_height = uploaded_image.size
|
115 |
-
# Adding the uploaded image to the page with a caption
|
116 |
-
st.image(source_img,
|
117 |
-
caption="Uploaded Image",
|
118 |
-
width=image_width
|
119 |
-
)
|
120 |
-
|
121 |
-
try:
|
122 |
-
model = YOLO(model_path)
|
123 |
-
except Exception as ex:
|
124 |
-
st.error(
|
125 |
-
f"Unable to load model. Check the specified path: {model_path}")
|
126 |
-
st.error(ex)
|
127 |
-
|
128 |
-
if st.sidebar.button('Detect Objects'):
|
129 |
-
res = model.predict(uploaded_image,
|
130 |
-
conf=confidence,
|
131 |
-
line_width=1,
|
132 |
-
show_labels=False,
|
133 |
-
show_conf=False
|
134 |
-
)
|
135 |
-
boxes = res[0].boxes
|
136 |
-
res_plotted = res[0].plot(labels=False, line_width=1)[:, :, ::-1]
|
137 |
-
with col2:
|
138 |
-
st.image(res_plotted,
|
139 |
-
caption='Detected Image',
|
140 |
-
width=image_width
|
141 |
-
)
|
142 |
-
try:
|
143 |
-
st.write(f'Number of empty slots detected: {len(boxes)}')
|
144 |
-
with st.expander("Detection Results (xywh)"):
|
145 |
-
for box in boxes:
|
146 |
-
st.write(box.xywh)
|
147 |
-
except Exception as ex:
|
148 |
-
st.write("No image is uploaded yet!")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
from torchvision import transforms
|
6 |
+
|
7 |
+
# Load YOLOv5 models
|
8 |
+
models = []
|
9 |
+
models.append(torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True))
|
10 |
+
models.append(torch.hub.load('ultralytics/yolov5', 'yolov5m', pretrained=True))
|
11 |
+
models.append(torch.hub.load('ultralytics/yolov5', 'yolov5l', pretrained=True))
|
12 |
+
models.append(torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True))
|
13 |
+
|
14 |
+
# Custom CSS
|
15 |
+
html_style = """
|
16 |
+
<style>
|
17 |
+
.container {
|
18 |
+
padding: 20px;
|
19 |
+
background-color: #f9f9f9;
|
20 |
+
border-radius: 10px;
|
21 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
22 |
+
}
|
23 |
+
.title {
|
24 |
+
color: #ff69b4;
|
25 |
+
font-size: 36px;
|
26 |
+
text-align: center;
|
27 |
+
margin-bottom: 30px;
|
28 |
+
}
|
29 |
+
.subheader {
|
30 |
+
color: #ff69b4;
|
31 |
+
font-size: 24px;
|
32 |
+
margin-top: 20px;
|
33 |
+
}
|
34 |
+
.image-container {
|
35 |
+
margin-top: 20px;
|
36 |
+
text-align: center;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
"""
|
40 |
+
|
41 |
+
st.markdown(html_style, unsafe_allow_html=True)
|
42 |
+
|
43 |
+
st.markdown("<h1 class='title'>AI Skin Analyzer</h1>", unsafe_allow_html=True)
|
44 |
+
|
45 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
46 |
+
|
47 |
+
if uploaded_file is not None:
|
48 |
+
image = cv2.imdecode(np.fromstring(uploaded_file.read(), np.uint8), 1)
|
49 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
50 |
+
st.markdown("<h2 class='subheader'>Model Predictions:</h2>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# Perform object detection for each model
|
53 |
+
for i, model in enumerate(models):
|
54 |
+
st.markdown(f"<h3 class='subheader'>Model {i+1}</h3>", unsafe_allow_html=True)
|
55 |
+
results = model(image)
|
56 |
+
results.render()
|
57 |
+
output_image = results.imgs[0]
|
58 |
+
st.image(output_image, use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|