Spaces:
Sleeping
Sleeping
File size: 6,566 Bytes
8cff839 d88cafd 8cff839 9e20103 8cff839 e4984cd 9c9f59c 8cff839 0b843f0 8cff839 34698bf 8cff839 e4984cd 8cff839 0f5786a 8cff839 0f5786a 8cff839 0f5786a 8cff839 4d0f74a 8cff839 4d0f74a 8cff839 2911a7c 8cff839 82b8cd6 4826646 6139cc0 4826646 a9c493f 10cc61c 5a83e07 31f6ddd 8805629 8cff839 4826646 d266fc1 5e92a6f |
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 |
import streamlit as st
import sahi.utils.file
import sahi.utils.mmdet
from sahi import AutoDetectionModel
from PIL import Image
import random
from utils import sahi_mmdet_inference
from streamlit_image_comparison import image_comparison
MMDET_YOLOX_TINY_MODEL_URL = "https://huggingface.co/fcakyon/mmdet-yolox-tiny/resolve/main/yolox_tiny_8x8_300e_coco_20211124_171234-b4047906.pth"
MMDET_YOLOX_TINY_MODEL_PATH = "yolox.pt"
MMDET_YOLOX_TINY_CONFIG_URL = "https://huggingface.co/fcakyon/mmdet-yolox-tiny/raw/main/yolox_tiny_8x8_300e_coco.py"
MMDET_YOLOX_TINY_CONFIG_PATH = "config.py"
IMAGE_TO_URL = {
"apple_tree.jpg": "https://user-images.githubusercontent.com/34196005/142730935-2ace3999-a47b-49bb-83e0-2bdd509f1c90.jpg",
"highway.jpg": "https://user-images.githubusercontent.com/34196005/142730936-1b397756-52e5-43be-a949-42ec0134d5d8.jpg",
"highway2.jpg": "https://user-images.githubusercontent.com/34196005/142742871-bf485f84-0355-43a3-be86-96b44e63c3a2.jpg",
"highway3.jpg": "https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg",
"highway2-yolox.jpg": "https://user-images.githubusercontent.com/34196005/143309873-c0c1f31c-c42e-4a36-834e-da0a2336bb19.jpg",
"highway2-sahi.jpg": "https://user-images.githubusercontent.com/34196005/143309867-42841f5a-9181-4d22-b570-65f90f2da231.jpg",
}
slice_size=512
overlap_ratio=0.2
postprocess_match_metric = 'IOU'
postprocess_type = 'NMS'
postprocess_match_threshold = 0.5
postprocess_class_agnostic = True
@st.cache(allow_output_mutation=True, show_spinner=False)
def download_comparison_images():
sahi.utils.file.download_from_url(
"https://user-images.githubusercontent.com/34196005/143309873-c0c1f31c-c42e-4a36-834e-da0a2336bb19.jpg",
"highway2-yolox.jpg",
)
sahi.utils.file.download_from_url(
"https://user-images.githubusercontent.com/34196005/143309867-42841f5a-9181-4d22-b570-65f90f2da231.jpg",
"highway2-sahi.jpg",
)
@st.cache(allow_output_mutation=True, show_spinner=False)
def get_model():
sahi.utils.file.download_from_url(
MMDET_YOLOX_TINY_MODEL_URL,
MMDET_YOLOX_TINY_MODEL_PATH,
)
sahi.utils.file.download_from_url(
MMDET_YOLOX_TINY_CONFIG_URL,
MMDET_YOLOX_TINY_CONFIG_PATH,
)
detection_model = AutoDetectionModel.from_pretrained(
model_type='mmdet',
model_path=MMDET_YOLOX_TINY_MODEL_PATH,
config_path=MMDET_YOLOX_TINY_CONFIG_PATH,
confidence_threshold=0.5,
device="cpu",
)
return detection_model
class SpinnerTexts:
def __init__(self):
self.ind_history_list = []
self.text_list = [
"Loading...",
]
def _store(self, ind):
if len(self.ind_history_list) == 6:
self.ind_history_list.pop(0)
self.ind_history_list.append(ind)
def get(self):
ind = 0
while ind in self.ind_history_list:
ind = random.randint(0, len(self.text_list) - 1)
self._store(ind)
return self.text_list[ind]
st.set_page_config(
page_title="A Demonstration of SARAI's Utility",
page_icon="🐦",
layout="wide",
initial_sidebar_state="auto",
)
download_comparison_images()
if "last_spinner_texts" not in st.session_state:
st.session_state["last_spinner_texts"] = SpinnerTexts()
if "output_1" not in st.session_state:
st.session_state["output_1"] = Image.open("highway2-yolox.jpg")
if "output_2" not in st.session_state:
st.session_state["output_2"] = Image.open("highway2-sahi.jpg")
st.markdown(
"""
<h2 style='text-align: center'>
A Demonstration of SARAI's Utility
</h2>
""",
unsafe_allow_html=True,
)
st.write("##")
with st.expander("Instructions for Use"):
st.markdown(
"""
<p>
1. Upload or select the input image
<br />
2. Press "Perform Prediction" to start image processing"
</p>
""",
unsafe_allow_html=True,
)
st.write("##")
col1, col2, col3 = st.columns([4, 1, 6])
with col1:
st.markdown(f"##### Set input image:")
# set input image by upload
image_file = st.file_uploader(
"Upload an image:", type=["jpg", "jpeg", "png"]
)
# set input image from exapmles
def slider_func(option):
option_to_id = {
"apple_tree.jpg": str(1),
"highway.jpg": str(2),
"highway2.jpg": str(3),
"highway3.jpg": str(4),
}
return option_to_id[option]
slider = st.select_slider(
"Or select from our sample images:",
options=["apple_tree.jpg", "highway.jpg", "highway2.jpg", "highway3.jpg"],
format_func=slider_func,
value="highway2.jpg",
)
# visualize input image
if image_file is not None:
image = Image.open(image_file)
else:
image = sahi.utils.cv.read_image_as_pil(IMAGE_TO_URL[slider])
st.image(image, width=325)
with col3:
st.markdown(f"##### YOLOX Standard vs SARAI Prediction:")
static_component = image_comparison(
img1=st.session_state["output_1"],
img2=st.session_state["output_2"],
label1="YOLOX",
label2="SARAI",
width=700,
starting_position=50,
show_labels=True,
make_responsive=True,
in_memory=True,
)
col1, col2, col3, col4, col5= st.columns([1, 2, 4, 2, 2])
with col2:
# submit button
submit = st.button("Perform Prediction")
if submit:
# perform prediction
with st.spinner(
text="Downloading model weight.. "
+ st.session_state["last_spinner_texts"].get()
):
detection_model = get_model()
image_size = 416
with st.spinner(
text="Performing prediction.. " + st.session_state["last_spinner_texts"].get()
):
output_1, output_2 = sahi_mmdet_inference(
image,
detection_model,
image_size=image_size,
slice_height=slice_size,
slice_width=slice_size,
overlap_height_ratio=overlap_ratio,
overlap_width_ratio=overlap_ratio,
postprocess_type=postprocess_type,
postprocess_match_metric=postprocess_match_metric,
postprocess_match_threshold=postprocess_match_threshold,
postprocess_class_agnostic=postprocess_class_agnostic,
)
st.session_state["output_1"] = output_1
st.session_state["output_2"] = output_2
with col4:
st.markdown(f"##### Slide to Compare")
|