AmirKaseb commited on
Commit
17b9fca
·
verified ·
1 Parent(s): be51e99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -146
app.py CHANGED
@@ -1,148 +1,58 @@
1
- import PIL
2
  import streamlit as st
3
- from ultralytics import YOLO
4
-
5
- # Give the path of the best.pt (best weights)
6
- model_path = 'best.pt'
7
-
8
- # Setting page layout
9
- st.set_page_config(
10
- page_title="NISH(Neural Intelligence Synthesis Hub)", # Setting page title
11
- page_icon="NK logo.jpeg", # Setting page icon
12
- layout="wide", # Setting layout to wide
13
- initial_sidebar_state="expanded", # Expanding sidebar by default
14
-
15
- )
16
-
17
- # Creating sidebar
18
- with st.sidebar:
19
- st.header("Image Config") # Adding header to sidebar
20
- # Adding file uploader to sidebar for selecting images
21
- source_img = st.file_uploader(
22
- "Upload an image...", type=("jpg", "jpeg", "png", 'bmp', 'webp'))
23
-
24
- # Model Options
25
- confidence = float(st.slider(
26
- "Select Model Confidence", 25, 100, 40)) / 100
27
-
28
- # Creating main page heading
29
- st.title("Object Detection")
30
- st.caption('Updload a photo by selecting :blue[Browse files]')
31
- st.caption('Then click the :blue[Detect Objects] button and check the result.')
32
- # Creating two columns on the main page
33
- col1, col2 = st.columns(2)
34
-
35
- # Adding image to the first column if image is uploaded
36
- with col1:
37
- if source_img:
38
- # Opening the uploaded image
39
- uploaded_image = PIL.Image.open(source_img)
40
- image_width, image_height = uploaded_image.size
41
- # Adding the uploaded image to the page with a caption
42
- st.image(source_img,
43
- caption="Uploaded Image",
44
- width=image_width
45
- )
46
-
47
- try:
48
- model = YOLO(model_path)
49
- except Exception as ex:
50
- st.error(
51
- f"Unable to load model. Check the specified path: {model_path}")
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
- # Creating sidebar
92
- with st.sidebar:
93
- st.header("Image Config") # Adding header to sidebar
94
- # Adding file uploader to sidebar for selecting images
95
- source_img = st.file_uploader(
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)