app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,95 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
AutoProcessor,
|
| 8 |
-
AutoFeatureExtractor,
|
| 9 |
-
AutoModelForImageClassification,
|
| 10 |
-
)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def set_base(image):
|
| 26 |
-
global
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
base_img = image.convert("RGB")
|
| 30 |
-
return "✅ Base image saved successfully."
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
def detect_trash(
|
| 34 |
-
global
|
| 35 |
-
if
|
| 36 |
-
return "Please
|
| 37 |
-
|
| 38 |
-
current_img = image.convert("RGB")
|
| 39 |
|
| 40 |
# Convert to numpy
|
| 41 |
-
base_np = np.array(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
diff = np.abs(
|
| 46 |
-
mask = (diff >
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
set_base_ui = gr.Interface(
|
| 75 |
fn=set_base,
|
| 76 |
-
inputs=gr.Image(type="pil", label="Upload
|
| 77 |
-
outputs=gr.Textbox(label="
|
| 78 |
-
title="
|
|
|
|
| 79 |
)
|
| 80 |
|
| 81 |
detect_trash_ui = gr.Interface(
|
| 82 |
fn=detect_trash,
|
| 83 |
inputs=gr.Image(type="pil", label="Upload Trash Image"),
|
| 84 |
-
outputs=gr.Textbox(label="
|
| 85 |
-
title="
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
demo = gr.TabbedInterface(
|
| 89 |
-
[set_base_ui, detect_trash_ui],
|
| 90 |
-
["Set Base", "Detect Trash"]
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from segment_anything import sam_model_registry, SamPredictor
|
| 5 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# ===== 1️⃣ Load models =====
|
| 8 |
+
# SAM
|
| 9 |
+
sam_checkpoint = "sam_vit_b_01ec64.pth" # 上传到Space的checkpoint
|
| 10 |
+
sam_model_type = "vit_b"
|
| 11 |
+
sam_model = sam_model_registry[sam_model_type](checkpoint=sam_checkpoint)
|
| 12 |
+
sam_predictor = SamPredictor(sam_model)
|
| 13 |
|
| 14 |
+
# BLIP
|
| 15 |
+
blip_model_name = "Salesforce/blip-vqa-base"
|
| 16 |
+
blip_processor = BlipProcessor.from_pretrained(blip_model_name)
|
| 17 |
+
blip_model = BlipForQuestionAnswering.from_pretrained(blip_model_name)
|
| 18 |
|
| 19 |
+
# ===== 2️⃣ Global base image =====
|
| 20 |
+
base_image = None
|
| 21 |
|
| 22 |
+
# ===== 3️⃣ Set base =====
|
| 23 |
def set_base(image):
|
| 24 |
+
global base_image
|
| 25 |
+
base_image = image
|
| 26 |
+
return "Base image saved successfully."
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# ===== 4️⃣ Detect trash =====
|
| 29 |
+
def detect_trash(trash_image):
|
| 30 |
+
global base_image
|
| 31 |
+
if base_image is None:
|
| 32 |
+
return "Please upload a base image first."
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Convert to numpy
|
| 35 |
+
base_np = np.array(base_image.resize(trash_image.size))
|
| 36 |
+
trash_np = np.array(trash_image)
|
| 37 |
+
|
| 38 |
+
# Compute simple difference mask
|
| 39 |
+
diff = np.abs(trash_np.astype(np.int16) - base_np.astype(np.int16))
|
| 40 |
+
mask = (diff.sum(axis=2) > 50).astype(np.uint8) # binary mask
|
| 41 |
+
|
| 42 |
+
# Find bounding box from mask
|
| 43 |
+
coords = np.argwhere(mask)
|
| 44 |
+
if coords.size == 0:
|
| 45 |
+
return "No difference detected."
|
| 46 |
+
y0, x0 = coords.min(axis=0)
|
| 47 |
+
y1, x1 = coords.max(axis=0)
|
| 48 |
+
box = np.array([[x0, y0, x1, y1]])
|
| 49 |
+
|
| 50 |
+
# Use SAM to refine mask
|
| 51 |
+
sam_predictor.set_image(trash_np)
|
| 52 |
+
masks, scores, logits = sam_predictor.predict(boxes=box)
|
| 53 |
+
# Take largest mask
|
| 54 |
+
mask_refined = masks[0]
|
| 55 |
+
|
| 56 |
+
# Crop the masked area
|
| 57 |
+
ys, xs = np.where(mask_refined)
|
| 58 |
+
if ys.size == 0:
|
| 59 |
+
return "SAM did not find any object."
|
| 60 |
+
cropped = trash_np[ys.min():ys.max(), xs.min():xs.max()]
|
| 61 |
+
|
| 62 |
+
# Convert to PIL for BLIP
|
| 63 |
+
cropped_img = Image.fromarray(cropped)
|
| 64 |
+
|
| 65 |
+
# BLIP question
|
| 66 |
+
question = "What material is this? Choose from plastic, metal, paper, cardboard, glass, trash."
|
| 67 |
+
inputs = blip_processor(cropped_img, question, return_tensors="pt")
|
| 68 |
+
out = blip_model.generate(**inputs)
|
| 69 |
+
answer = blip_processor.decode(out[0], skip_special_tokens=True)
|
| 70 |
+
|
| 71 |
+
# Only allow predefined classes
|
| 72 |
+
valid_classes = ["plastic", "metal", "paper", "cardboard", "glass", "trash"]
|
| 73 |
+
result = next((c for c in valid_classes if c in answer.lower()), "trash")
|
| 74 |
+
|
| 75 |
+
return result.capitalize()
|
| 76 |
+
|
| 77 |
+
# ===== 5️⃣ Gradio UI =====
|
| 78 |
set_base_ui = gr.Interface(
|
| 79 |
fn=set_base,
|
| 80 |
+
inputs=gr.Image(type="pil", label="Upload Base Image"),
|
| 81 |
+
outputs=gr.Textbox(label="Result"),
|
| 82 |
+
title="Set Base Image",
|
| 83 |
+
api_name="/set_base"
|
| 84 |
)
|
| 85 |
|
| 86 |
detect_trash_ui = gr.Interface(
|
| 87 |
fn=detect_trash,
|
| 88 |
inputs=gr.Image(type="pil", label="Upload Trash Image"),
|
| 89 |
+
outputs=gr.Textbox(label="Detected Material"),
|
| 90 |
+
title="Detect Trash Material",
|
| 91 |
+
api_name="/detect_trash"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
+
demo = gr.TabbedInterface([set_base_ui, detect_trash_ui], ["Set Base", "Detect Trash"])
|
| 95 |
+
demo.launch()
|