import streamlit as st
from ultralytics import YOLO
from PIL import Image

model = YOLO('yolo12n.pt')
	
# Streamlit 網頁介面
st.title('上傳圖檔執行YOLO物體偵測')
source_img = st.file_uploader(
	label = '選擇圖檔...',
	type = ('jpg', 'jpeg', 'png', 'bmp', 'webp')
)

col1, col2 = st.columns(2)

with col1:
    if source_img:
        uploaded_image = Image.open(source_img)
        st.image(image = source_img,
                 caption = '上傳圖檔',
                 width = 'stretch')

if source_img:
    if st.button('執行'):
        with st.spinner('執行中...'):
            res = model.predict(uploaded_image, conf = 0.6)                    
            res_plotted = res[0].plot()[:, :, ::-1]