Spaces:
Build error
Build error
Upload 2 files
Browse files- streamlit_app.py +104 -0
- yolov8m-cls.pt +3 -0
streamlit_app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
# ------------------------------
|
| 7 |
+
# Streamlit Config (must be first)
|
| 8 |
+
# ------------------------------
|
| 9 |
+
st.set_page_config(page_title="Image Categorization Demo", layout="wide")
|
| 10 |
+
|
| 11 |
+
# ------------------------------
|
| 12 |
+
# Load YOLO classification model
|
| 13 |
+
# ------------------------------
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_model():
|
| 16 |
+
from ultralytics import YOLO
|
| 17 |
+
model = YOLO("yolov8m-cls.pt") # replace with your trained model
|
| 18 |
+
return model
|
| 19 |
+
|
| 20 |
+
model = load_model()
|
| 21 |
+
|
| 22 |
+
# ------------------------------
|
| 23 |
+
# Helper: manage temp folder
|
| 24 |
+
# ------------------------------
|
| 25 |
+
TEMP_FOLDER = "temfolder"
|
| 26 |
+
|
| 27 |
+
def prepare_temp_folder():
|
| 28 |
+
if os.path.exists(TEMP_FOLDER):
|
| 29 |
+
shutil.rmtree(TEMP_FOLDER)
|
| 30 |
+
os.makedirs(TEMP_FOLDER)
|
| 31 |
+
|
| 32 |
+
def cleanup_temp_folder():
|
| 33 |
+
if os.path.exists(TEMP_FOLDER):
|
| 34 |
+
shutil.rmtree(TEMP_FOLDER)
|
| 35 |
+
|
| 36 |
+
# ------------------------------
|
| 37 |
+
# Streamlit UI
|
| 38 |
+
# ------------------------------
|
| 39 |
+
st.title("Image Categorization Demo")
|
| 40 |
+
|
| 41 |
+
with st.form("upload_form", clear_on_submit=True):
|
| 42 |
+
uploaded_files = st.file_uploader(
|
| 43 |
+
"Upload one or more images",
|
| 44 |
+
type=["jpg", "jpeg", "png"],
|
| 45 |
+
accept_multiple_files=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
col1, col2 = st.columns([1, 1])
|
| 49 |
+
submit = col1.form_submit_button("π Submit for Classification")
|
| 50 |
+
refresh = col2.form_submit_button("π Refresh")
|
| 51 |
+
|
| 52 |
+
if refresh:
|
| 53 |
+
cleanup_temp_folder()
|
| 54 |
+
st.success("π§Ή Uploads cleared!")
|
| 55 |
+
|
| 56 |
+
if submit:
|
| 57 |
+
if not uploaded_files:
|
| 58 |
+
st.warning("β οΈ Please upload at least one image before submitting.")
|
| 59 |
+
else:
|
| 60 |
+
total_files = len(uploaded_files)
|
| 61 |
+
st.write(f"π Classifying **{total_files}** images...")
|
| 62 |
+
|
| 63 |
+
# Prepare clean folder
|
| 64 |
+
prepare_temp_folder()
|
| 65 |
+
|
| 66 |
+
results_by_class = {}
|
| 67 |
+
|
| 68 |
+
progress = st.progress(0) # progress bar
|
| 69 |
+
status_text = st.empty() # placeholder for progress text
|
| 70 |
+
|
| 71 |
+
for idx, file in enumerate(uploaded_files, start=1):
|
| 72 |
+
# Save uploaded file into temfolder
|
| 73 |
+
img_path = os.path.join(TEMP_FOLDER, file.name)
|
| 74 |
+
with open(img_path, "wb") as f:
|
| 75 |
+
f.write(file.read())
|
| 76 |
+
|
| 77 |
+
# Run YOLO classification
|
| 78 |
+
results = model(img_path)
|
| 79 |
+
pred_class = results[0].names[results[0].probs.top1]
|
| 80 |
+
|
| 81 |
+
# Group images by predicted class
|
| 82 |
+
if pred_class not in results_by_class:
|
| 83 |
+
results_by_class[pred_class] = []
|
| 84 |
+
results_by_class[pred_class].append(img_path)
|
| 85 |
+
|
| 86 |
+
# Update progress bar + text
|
| 87 |
+
percent = int((idx / total_files) * 100)
|
| 88 |
+
progress.progress(idx / total_files)
|
| 89 |
+
status_text.text(f"Processing {idx}/{total_files} images ({percent}%)")
|
| 90 |
+
|
| 91 |
+
st.success("β
Classification complete!")
|
| 92 |
+
|
| 93 |
+
# ------------------------------
|
| 94 |
+
# Show gallery grouped by class
|
| 95 |
+
# ------------------------------
|
| 96 |
+
for cls, img_list in results_by_class.items():
|
| 97 |
+
st.subheader(f"π Category: **{cls}** ({len(img_list)})")
|
| 98 |
+
cols = st.columns(4) # show 4 images per row
|
| 99 |
+
for i, img_path in enumerate(img_list):
|
| 100 |
+
with cols[i % 4]:
|
| 101 |
+
st.image(Image.open(img_path), use_column_width=True)
|
| 102 |
+
|
| 103 |
+
# Cleanup after displaying
|
| 104 |
+
cleanup_temp_folder()
|
yolov8m-cls.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:864010e833b46be542ed849b2eb0fdacf51e4679dc677c18dcc4505244d08ec1
|
| 3 |
+
size 34285204
|