|
import streamlit as st |
|
import torch |
|
import open_clip |
|
from PIL import Image |
|
from classifier import few_shot_fault_classification |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model, _, preprocess = open_clip.create_model_and_transforms('RN50', pretrained='openai') |
|
model = model.to(device) |
|
model.eval() |
|
|
|
st.title("🛠️ Few-Shot Fault Detection (Industrial Quality Control)") |
|
|
|
st.markdown("Upload **10 Nominal Images**, **10 Defective Images**, and one or more **Test Images** to classify.") |
|
|
|
col1, col2 = st.columns(2) |
|
with col1: |
|
nominal_files = st.file_uploader("Upload Nominal Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True) |
|
with col2: |
|
defective_files = st.file_uploader("Upload Defective Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True) |
|
|
|
test_files = st.file_uploader("Upload Test Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True) |
|
|
|
if st.button("Classify Test Images"): |
|
if len(nominal_files) < 1 or len(defective_files) < 1 or len(test_files) < 1: |
|
st.warning("Please upload at least 1 image in each category.") |
|
else: |
|
st.info("Running classification...") |
|
|
|
nominal_imgs = [preprocess(Image.open(f).convert("RGB")).unsqueeze(0) for f in nominal_files] |
|
defective_imgs = [preprocess(Image.open(f).convert("RGB")).unsqueeze(0) for f in defective_files] |
|
test_imgs = [preprocess(Image.open(f).convert("RGB")).unsqueeze(0) for f in test_files] |
|
|
|
results = few_shot_fault_classification( |
|
model=model, |
|
test_images=[img.squeeze(0) for img in test_imgs], |
|
test_image_filenames=[f.name for f in test_files], |
|
nominal_images=[img.squeeze(0) for img in nominal_imgs], |
|
nominal_descriptions=[f.name for f in nominal_files], |
|
defective_images=[img.squeeze(0) for img in defective_imgs], |
|
defective_descriptions=[f.name for f in defective_files], |
|
num_few_shot_nominal_imgs=len(nominal_files), |
|
device=device |
|
) |
|
|
|
for res in results: |
|
st.write(f"**{res['image_path']}** ➜ {res['classification_result']} " |
|
f"(Nominal: {res['non_defect_prob']}, Defective: {res['defect_prob']})") |
|
|