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( """
1. Upload or select the input image
2. Press "Perform Prediction" to start image processing"