Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import open_clip
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from classifier import few_shot_fault_classification
|
| 6 |
+
|
| 7 |
+
# Load lightweight CLIP model
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
model, _, preprocess = open_clip.create_model_and_transforms('RN50', pretrained='openai')
|
| 10 |
+
model = model.to(device)
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
st.title("🛠️ Few-Shot Fault Detection (Industrial Quality Control)")
|
| 14 |
+
|
| 15 |
+
st.markdown("Upload **10 Nominal Images**, **10 Defective Images**, and one or more **Test Images** to classify.")
|
| 16 |
+
|
| 17 |
+
col1, col2 = st.columns(2)
|
| 18 |
+
with col1:
|
| 19 |
+
nominal_files = st.file_uploader("Upload Nominal Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 20 |
+
with col2:
|
| 21 |
+
defective_files = st.file_uploader("Upload Defective Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 22 |
+
|
| 23 |
+
test_files = st.file_uploader("Upload Test Images", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 24 |
+
|
| 25 |
+
if st.button("Classify Test Images"):
|
| 26 |
+
if len(nominal_files) < 1 or len(defective_files) < 1 or len(test_files) < 1:
|
| 27 |
+
st.warning("Please upload at least 1 image in each category.")
|
| 28 |
+
else:
|
| 29 |
+
st.info("Running classification...")
|
| 30 |
+
|
| 31 |
+
nominal_imgs = [preprocess(Image.open(f).convert("RGB")).unsqueeze(0) for f in nominal_files]
|
| 32 |
+
defective_imgs = [preprocess(Image.open(f).convert("RGB")).unsqueeze(0) for f in defective_files]
|
| 33 |
+
test_imgs = [preprocess(Image.open(f).convert("RGB")).unsqueeze(0) for f in test_files]
|
| 34 |
+
|
| 35 |
+
results = few_shot_fault_classification(
|
| 36 |
+
model=model,
|
| 37 |
+
test_images=[img.squeeze(0) for img in test_imgs],
|
| 38 |
+
test_image_filenames=[f.name for f in test_files],
|
| 39 |
+
nominal_images=[img.squeeze(0) for img in nominal_imgs],
|
| 40 |
+
nominal_descriptions=[f.name for f in nominal_files],
|
| 41 |
+
defective_images=[img.squeeze(0) for img in defective_imgs],
|
| 42 |
+
defective_descriptions=[f.name for f in defective_files],
|
| 43 |
+
num_few_shot_nominal_imgs=len(nominal_files),
|
| 44 |
+
device=device
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
for res in results:
|
| 48 |
+
st.write(f"**{res['image_path']}** ➜ {res['classification_result']} "
|
| 49 |
+
f"(Nominal: {res['non_defect_prob']}, Defective: {res['defect_prob']})")
|