TangYiJay commited on
Commit
bcd3fcc
·
verified ·
1 Parent(s): 0fe4fae
Files changed (1) hide show
  1. app.py +84 -48
app.py CHANGED
@@ -1,63 +1,99 @@
1
  import cv2
2
  import numpy as np
3
  from PIL import Image
4
- import gradio as gr
5
- from transformers import CLIPProcessor, CLIPModel
6
  import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Load CLIP model for material classification
9
- MODEL_ID = "openai/clip-vit-base-patch32"
10
- model = CLIPModel.from_pretrained(MODEL_ID)
11
- processor = CLIPProcessor.from_pretrained(MODEL_ID)
12
- LABELS = ["plastic", "metal", "paper", "cardboard", "glass", "trash"]
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- def get_clip_prediction(crop_img):
15
- inputs = processor(text=LABELS, images=crop_img, return_tensors="pt", padding=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  with torch.no_grad():
17
  outputs = model(**inputs)
18
- logits_per_image = outputs.logits_per_image
19
- probs = logits_per_image.softmax(dim=1).cpu().numpy()
20
- best_idx = np.argmax(probs)
21
- return LABELS[best_idx], float(probs[0][best_idx])
22
 
23
- def detect_diff_and_classify(base_img, target_img):
24
- if base_img is None or target_img is None:
25
- return "Please upload both images.", None
 
26
 
27
- base_np = np.array(base_img.convert("RGB"))
28
- target_np = np.array(target_img.convert("RGB"))
 
29
 
30
- diff = cv2.absdiff(base_np, target_np)
31
- gray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)
32
- _, thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)
33
- contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
34
 
35
- if not contours:
36
- return "No significant difference detected.", None
37
-
38
- # Largest contour → likely new object
39
- c = max(contours, key=cv2.contourArea)
40
- x, y, w, h = cv2.boundingRect(c)
41
- crop = target_np[y:y+h, x:x+w]
42
- crop_img = Image.fromarray(crop)
43
-
44
- # Run CLIP classification
45
- label, prob = get_clip_prediction(crop_img)
46
- return f"Detected material: {label} (confidence {prob:.2f})", crop_img
47
-
48
- demo = gr.Interface(
49
- fn=detect_diff_and_classify,
50
- inputs=[
51
- gr.Image(type="pil", label="Base Image"),
52
- gr.Image(type="pil", label="Target Image")
53
- ],
54
- outputs=[
55
- gr.Textbox(label="Result"),
56
- gr.Image(type="pil", label="Detected Object Region")
57
- ],
58
- title="Automatic Object Detection + Material Classification",
59
- description="Detect differences between base and target images, crop the changed region, and classify the material (plastic, metal, paper, cardboard, glass, trash)."
60
- )
61
 
62
  if __name__ == "__main__":
63
  demo.launch()
 
1
  import cv2
2
  import numpy as np
3
  from PIL import Image
 
 
4
  import torch
5
+ import gradio as gr
6
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
7
+
8
+ # ---- Step 1: 模型加载 ----
9
+ MODEL_ID = "prithivMLmods/Trash-Net"
10
+ processor = AutoImageProcessor.from_pretrained(MODEL_ID)
11
+ model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
12
+
13
+ # ---- Step 2: 专业裁剪(基于OpenCV结构化差异检测) ----
14
+ def smart_crop(base_img: Image.Image, new_img: Image.Image):
15
+ if base_img is None or new_img is None:
16
+ return None, "Missing image input."
17
+
18
+ # 转为OpenCV格式
19
+ base = np.array(base_img.convert("RGB"))
20
+ new = np.array(new_img.convert("RGB"))
21
+ base = cv2.resize(base, (224, 224))
22
+ new = cv2.resize(new, (224, 224))
23
 
24
+ # 转灰度 + 高斯模糊减少噪声
25
+ base_gray = cv2.GaussianBlur(cv2.cvtColor(base, cv2.COLOR_RGB2GRAY), (5,5), 0)
26
+ new_gray = cv2.GaussianBlur(cv2.cvtColor(new, cv2.COLOR_RGB2GRAY), (5,5), 0)
27
+
28
+ # 差异检测
29
+ diff = cv2.absdiff(base_gray, new_gray)
30
+ _, thresh = cv2.threshold(diff, 40, 255, cv2.THRESH_BINARY)
31
+
32
+ # 形态学操作:去噪 + 连通区域扩大
33
+ kernel = np.ones((5,5), np.uint8)
34
+ thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
35
+ thresh = cv2.dilate(thresh, kernel, iterations=2)
36
+
37
+ # 查找轮廓
38
+ contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
39
+ if not contours:
40
+ return None, "No significant object difference detected."
41
 
42
+ # 找最大差异区域
43
+ contour = max(contours, key=cv2.contourArea)
44
+ x, y, w, h = cv2.boundingRect(contour)
45
+
46
+ # 裁剪区域并略微扩大边缘
47
+ margin = 10
48
+ x1 = max(0, x - margin)
49
+ y1 = max(0, y - margin)
50
+ x2 = min(new.shape[1], x + w + margin)
51
+ y2 = min(new.shape[0], y + h + margin)
52
+
53
+ cropped = new[y1:y2, x1:x2]
54
+ cropped_pil = Image.fromarray(cropped)
55
+ return cropped_pil, None
56
+
57
+ # ---- Step 3: TrashNet 分类 ----
58
+ def classify_trash(image: Image.Image):
59
+ if image is None:
60
+ return "No image to classify.", 0.0
61
+ inputs = processor(images=image, return_tensors="pt")
62
  with torch.no_grad():
63
  outputs = model(**inputs)
64
+ preds = torch.nn.functional.softmax(outputs.logits, dim=-1)
65
+ label = model.config.id2label[preds.argmax().item()]
66
+ confidence = preds.max().item()
67
+ return label, confidence
68
 
69
+ # ---- Step 4: 主逻辑 ----
70
+ def detect_and_classify(base_img, new_img):
71
+ if base_img is None or new_img is None:
72
+ return "⚠️ Please upload both base and new images.", None, None
73
 
74
+ cropped, error = smart_crop(base_img, new_img)
75
+ if error:
76
+ return f"⚠️ {error}", None, None
77
 
78
+ label, conf = classify_trash(cropped)
79
+ return f"✅ Object classified as: {label} ({conf*100:.2f}% confidence)", cropped, label
 
 
80
 
81
+ # ---- Step 5: Gradio 界面 ----
82
+ with gr.Blocks(title="Smart Trash Detector") as demo:
83
+ gr.Markdown("# ♻️ Smart Trash Detection (with professional background subtraction)\nUpload a **base image**, then a **new image**.")
84
+
85
+ with gr.Row():
86
+ base_img = gr.Image(label="Base Image", type="pil")
87
+ new_img = gr.Image(label="New Image", type="pil")
88
+
89
+ run_btn = gr.Button("Analyze and Classify")
90
+
91
+ with gr.Row():
92
+ result_text = gr.Textbox(label="Result")
93
+ result_crop = gr.Image(label="Detected Object (Cropped)")
94
+ result_label = gr.Textbox(label="Class")
95
+
96
+ run_btn.click(detect_and_classify, [base_img, new_img], [result_text, result_crop, result_label])
 
 
 
 
 
 
 
 
 
 
97
 
98
  if __name__ == "__main__":
99
  demo.launch()