joshangngoching commited on
Commit
0903305
·
verified ·
1 Parent(s): 11372bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -42
app.py CHANGED
@@ -4,7 +4,6 @@ from transformers import pipeline
4
  import numpy as np
5
  import cv2
6
  import matplotlib.cm as cm
7
- import time
8
  import base64
9
  from io import BytesIO
10
 
@@ -13,7 +12,6 @@ st.set_page_config(layout="wide")
13
  with open("styles.css") as f:
14
  st.markdown('<style>{}</style>'.format(f.read()), unsafe_allow_html=True)
15
 
16
-
17
  st.markdown("<h1 class='title'>Segformer Semantic Segmentation</h1>", unsafe_allow_html=True)
18
  st.markdown("""
19
  <div class='text-center'>
@@ -25,10 +23,6 @@ road scene and the app will return the image with semantic segmentation applied.
25
 
26
  group_members = ["Ang Ngo Ching, Josh Darren W.", "Bautista, Ryan Matthew M.", "Lacuesta, Angelo Giuseppe M.", "Reyes, Kenwin Hans", "Ting, Sidney Mitchell O."]
27
 
28
-
29
- # model_versions = ["b1", "b2", "b3", "b4", "b5"]
30
- # selected_model_version = st.selectbox("Select a model version:", model_versions)
31
-
32
  st.markdown("""
33
  <h3 class='text-center' style='margin-top: 0.5rem;'>
34
  ℹ️ You can get sample images of road scenes in this <a href='https://drive.google.com/drive/folders/1202EMeXAHnN18NuhJKWWme34vg0V-svY?fbclid=IwAR3kyjGS895nOBKi9aGT_P4gLX9jvSNrV5b5y3GH49t2Pvg2sZSRA58LLxs' target='_blank'>link</a>.
@@ -39,14 +33,6 @@ st.markdown("""
39
  📜 Read more about the paper <a href='https://arxiv.org/pdf/2105.15203.pdf' target='_blank'>here</a>.
40
  </h3>""", unsafe_allow_html=True)
41
 
42
- st.markdown("Group Members:")
43
- for member in group_members:
44
- st.markdown("- " + member)
45
-
46
- semantic_segmentation = pipeline("image-segmentation", f"nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
47
-
48
- new_file_uploaded = False
49
- uploaded_file = st.file_uploader("", type=["jpg", "png"])
50
  label_colors = {}
51
 
52
  def draw_masks_fromDict(image, results):
@@ -68,45 +54,47 @@ def draw_masks_fromDict(image, results):
68
  masked_image = masked_image.astype(np.uint8)
69
  return cv2.addWeighted(image, 0.3, masked_image, 0.7, 0)
70
 
71
- col1, col2 = st.columns(2)
72
-
73
- if "uploaded_file" not in st.session_state:
74
- st.session_state.uploaded_file = None
75
 
76
  if uploaded_file is not None:
77
- st.session_state.uploaded_file = uploaded_file
78
-
79
- if st.session_state.uploaded_file is not None:
80
- image = Image.open(st.session_state.uploaded_file)
81
  col1, col2 = st.columns(2)
82
 
83
  with col1:
84
  st.image(image, caption='Uploaded Image.', use_column_width=True)
85
 
 
 
 
 
86
 
87
- while True:
88
- with st.spinner('Processing...'):
89
- segmentation_results = semantic_segmentation(image)
90
- image_with_masks = draw_masks_fromDict(np.array(image)[:, :, :3], segmentation_results)
91
- image_with_masks_pil = Image.fromarray(image_with_masks, 'RGB')
92
 
93
- with col2:
94
- st.image(image_with_masks_pil, caption='Segmented Image.', use_column_width=True)
95
-
96
- st.markdown("**Labels:**")
97
- for color, label in label_colors.items():
98
- st.markdown(f"<div style='display: flex; align-items: center; margin-bottom: 0.5rem;'><span style='display: inline-block; width: 20px; height: 20px; background-color: rgb{color}; margin-right: 1rem; border-radius: 10px;'></span><p style='margin: 0;'>{label}</p></div>", unsafe_allow_html=True)
 
 
 
 
99
 
100
- buffered = BytesIO()
101
- image_with_masks_pil.save(buffered, format="PNG")
102
- img_str = base64.b64encode(buffered.getvalue()).decode()
103
- href = f'<a href="data:file/png;base64,{img_str}" download="segmented_{st.session_state.uploaded_file.name}">Download Segmented Image</a>'
104
- st.markdown(href, unsafe_allow_html=True)
105
 
106
- new_file_uploaded = False
107
 
108
- label_colors = {}
 
 
 
 
 
109
 
110
- while not new_file_uploaded:
111
- time.sleep(1)
 
112
 
 
 
4
  import numpy as np
5
  import cv2
6
  import matplotlib.cm as cm
 
7
  import base64
8
  from io import BytesIO
9
 
 
12
  with open("styles.css") as f:
13
  st.markdown('<style>{}</style>'.format(f.read()), unsafe_allow_html=True)
14
 
 
15
  st.markdown("<h1 class='title'>Segformer Semantic Segmentation</h1>", unsafe_allow_html=True)
16
  st.markdown("""
17
  <div class='text-center'>
 
23
 
24
  group_members = ["Ang Ngo Ching, Josh Darren W.", "Bautista, Ryan Matthew M.", "Lacuesta, Angelo Giuseppe M.", "Reyes, Kenwin Hans", "Ting, Sidney Mitchell O."]
25
 
 
 
 
 
26
  st.markdown("""
27
  <h3 class='text-center' style='margin-top: 0.5rem;'>
28
  ℹ️ You can get sample images of road scenes in this <a href='https://drive.google.com/drive/folders/1202EMeXAHnN18NuhJKWWme34vg0V-svY?fbclid=IwAR3kyjGS895nOBKi9aGT_P4gLX9jvSNrV5b5y3GH49t2Pvg2sZSRA58LLxs' target='_blank'>link</a>.
 
33
  📜 Read more about the paper <a href='https://arxiv.org/pdf/2105.15203.pdf' target='_blank'>here</a>.
34
  </h3>""", unsafe_allow_html=True)
35
 
 
 
 
 
 
 
 
 
36
  label_colors = {}
37
 
38
  def draw_masks_fromDict(image, results):
 
54
  masked_image = masked_image.astype(np.uint8)
55
  return cv2.addWeighted(image, 0.3, masked_image, 0.7, 0)
56
 
57
+ uploaded_file = st.file_uploader("", type=["jpg", "png"])
 
 
 
58
 
59
  if uploaded_file is not None:
60
+ image = Image.open(uploaded_file)
61
+
 
 
62
  col1, col2 = st.columns(2)
63
 
64
  with col1:
65
  st.image(image, caption='Uploaded Image.', use_column_width=True)
66
 
67
+ semantic_segmentation = pipeline("image-segmentation", f"nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
68
+ segmentation_results = semantic_segmentation(image)
69
+ image_with_masks = draw_masks_fromDict(np.array(image)[:, :, :3], segmentation_results)
70
+ image_with_masks_pil = Image.fromarray(image_with_masks, 'RGB')
71
 
72
+ with col2:
73
+ st.image(image_with_masks_pil, caption='Segmented Image.', use_column_width=True)
 
 
 
74
 
75
+ html_segment = "<div class='container'><h3>Labels:</h3>"
76
+
77
+ for color, label in label_colors.items():
78
+ html_segment += f"<div style='display: flex; align-items: center; margin-bottom: 0.5rem;'><span style='display: inline-block; width: 20px; height: 20px; background-color: rgb{color}; margin-right: 1rem; border-radius: 10px;'></span><p style='margin: 0;'>{label}</p></div>"
79
+
80
+ buffered = BytesIO()
81
+ image_with_masks_pil.save(buffered, format="PNG")
82
+ img_str = base64.b64encode(buffered.getvalue()).decode()
83
+ html_segment += f'<a href="data:file/png;base64,{img_str}" download="segmented_{uploaded_file.name}">Download Segmented Image</a>'
84
+ st.markdown(html_segment + "</div>", unsafe_allow_html=True)
85
 
 
 
 
 
 
86
 
87
+ pdf_url = "https://arxiv.org/pdf/2105.15203.pdf"
88
 
89
+ st.markdown("""
90
+ <hr><h3 style='text-align: center; margin-top: 2rem;'>
91
+ Read more about the paper below👇
92
+ </h5>
93
+ """, unsafe_allow_html=True)
94
+ st.markdown(f'<iframe class="pdf" src={pdf_url}></iframe>', unsafe_allow_html=True)
95
 
96
+ html_members = "<hr><div style='display: flex; justify-content: center;'><h3>Group 6 - Members:</h3><ul>"
97
+ for member in group_members:
98
+ html_members += "<li>" + member + "</li>"
99
 
100
+ st.markdown(html_members + "</ul></div>", unsafe_allow_html=True)