YSMlearnsCode commited on
Commit
315be3f
·
1 Parent(s): f1b198c

added model and process file

Browse files
pyproject.toml CHANGED
@@ -6,4 +6,5 @@ readme = "README.md"
6
  requires-python = ">=3.11"
7
  dependencies = [
8
  "gradio>=5.34.2",
 
9
  ]
 
6
  requires-python = ">=3.11"
7
  dependencies = [
8
  "gradio>=5.34.2",
9
+ "ultralytics>=8.3.159",
10
  ]
src/app/interface.py CHANGED
@@ -1,4 +1,6 @@
1
  import gradio as gr
 
 
2
 
3
 
4
  def main_interface():
@@ -10,5 +12,5 @@ def main_interface():
10
  with gr.Row():
11
  input_image = gr.Image(type="numpy", label="Upload Image")
12
  output_image = gr.Image(type="numpy", label="Segmented Output")
13
- # input_image.change(fn=predict, inputs=input_image, outputs=output_image)
14
  return demo
 
1
  import gradio as gr
2
+ from ..process.process import display_result
3
+ import os
4
 
5
 
6
  def main_interface():
 
12
  with gr.Row():
13
  input_image = gr.Image(type="numpy", label="Upload Image")
14
  output_image = gr.Image(type="numpy", label="Segmented Output")
15
+ input_image.change(fn=display_result, inputs=input_image, outputs=output_image)
16
  return demo
src/process/__init__.py ADDED
File without changes
src/process/process.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import cv2
3
+ import numpy as np
4
+ import os
5
+
6
+ # Waste Bin Mapping (German Mülltrennung System)
7
+ bin_map = {
8
+ "Yellow Bin (Gelbe Tonne)": [
9
+ "Aluminium blister pack", "Aluminium foil", "Carded blister pack", "Clear plastic bottle",
10
+ "Disposable food container", "Disposable plastic cup", "Drink Carton", "Drink can",
11
+ "Foam cup", "Meal carton", "Metal lid", "Metal bottle cap", "Other plastic bottle",
12
+ "Other plastic container", "Other plastic cup", "Other plastic wrapper", "Plastic bottle cap",
13
+ "Plastic film", "Plastic glooves", "Plastic lid", "Plastic straw", "Polypropylene bag",
14
+ "Pop tab", "Single-use carrier bag", "Six pack rings", "Spread tub", "Squeezable tube",
15
+ "Tupperware"
16
+ ],
17
+ "Grey Bin (Restmüll)": [
18
+ "Cigarette", "Garbage bag", "Shoe", "Unlabeled litter", "Plastified paper bag",
19
+ "Styrofoam piece", "Rope & strings", "Foam food container", "Other plastic",
20
+ "Pizza box", "Tissues"
21
+ ],
22
+ "Green Bin (Biotonne)": [
23
+ "Food waste"
24
+ ],
25
+ "Blue Bin (Papiertonne)": [
26
+ "Egg carton", "Normal paper", "Other Carton", "Paper Bag", "Paper cup", "Paper straw",
27
+ "Pizza box", "Toilet tube", "Magazine paper", "Wrapping paper", "Corrugated carton"
28
+ ],
29
+ "Glascontainer": [
30
+ "Glass bottle", "Glass cup", "Glass jar", "Broken glass"
31
+ ],
32
+ "Hazardous Waste (Sondermüll)": [
33
+ "Battery", "Aerosol", "Scrap metal"
34
+ ],
35
+ "Deposit Return (Pfand)": [s
36
+ "Drink can", "Clear plastic bottle", "Glass bottle", "Food Can"
37
+ ]
38
+ }
39
+
40
+ # Bin colors in BGR format (for OpenCV)
41
+ bin_colors = {
42
+ "Yellow Bin (Gelbe Tonne)": (255, 255, 0), # Yellow
43
+ "Grey Bin (Restmüll)": (128, 128, 128), # Gray
44
+ "Green Bin (Biotonne)": (0, 255, 0), # Green
45
+ "Blue Bin (Papiertonne)": (0, 0, 250), # Blue (bright)
46
+ "Glascontainer": (0, 0, 0), # Black
47
+ "Hazardous Waste (Sondermüll)": (255, 0, 0), # Red
48
+ "Deposit Return (Pfand)": (255, 0, 255) # Purple/Magenta
49
+ }
50
+
51
+ class_to_bin = {cls: bin_type for bin_type, cls_list in bin_map.items() for cls in cls_list} #class name gives bin type
52
+
53
+ def display_result(image_to_segment):
54
+
55
+ # Load model
56
+ model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'models', 'best_heavy_59classes.pt'))
57
+ model = YOLO(model_path)
58
+
59
+ # Resize image
60
+ image = cv2.resize(image_to_segment, (640,640))
61
+
62
+ # Run prediction
63
+ results = model(image_to_segment)
64
+ result = results[0]
65
+
66
+ # Get class names
67
+ class_names = model.names
68
+
69
+ # Inverse mapping: class name to bin
70
+ class_to_bin = {cls: bin_name for bin_name, class_list in bin_map.items() for cls in class_list}
71
+
72
+ # Handle case with no trash
73
+ if result.masks is None or result.boxes is None or len(result.boxes) == 0:
74
+ overlay = image.copy()
75
+ cv2.putText(overlay, "No trash detected!", (30, 60), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 3, cv2.LINE_AA)
76
+ return (overlay)
77
+
78
+ masks = result.masks.data.cpu().numpy()
79
+ boxes = result.boxes.xyxy.cpu().numpy() # [N, 4]
80
+ scores = result.boxes.conf.cpu().numpy() # [N]
81
+ class_ids = result.boxes.cls.cpu().numpy() # [N]
82
+
83
+ overlay = image.copy()
84
+
85
+ for i in range(len(class_ids)):
86
+ class_id = int(class_ids[i])
87
+ score = scores[i]
88
+ mask = masks[i]
89
+ box = boxes[i].astype(int)
90
+
91
+ class_name = class_names[class_id]
92
+
93
+ bin_type = next((bin_name for bin_name, items in bin_map.items() if class_name in items), "abcd")
94
+ color = bin_colors.get(bin_type, (255, 255, 255)) # fallback to white
95
+
96
+ # Apply colored mask (blended)
97
+ mask_3c = np.stack([mask] * 3, axis=-1) # [H, W, 3]
98
+ color_array = np.array(color, dtype=np.uint8).reshape(1, 1, 3)
99
+ colored_mask = (mask_3c * color_array).astype(np.uint8)
100
+
101
+ overlay = cv2.addWeighted(overlay, 1.0, colored_mask, 0.5, 0)
102
+
103
+ # Draw bounding box
104
+ cv2.rectangle(overlay, (box[0], box[1]), (box[2], box[3]), color, 2)
105
+
106
+ # Draw label (class name + score + bin)
107
+ label = f"{class_name} ({int(score * 100)}%)\n{bin_type}"
108
+ for j, line in enumerate(label.split("\n")):
109
+ text_pos = (box[0], box[1] - 10 - 20 * j)
110
+ cv2.putText(overlay, line, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2, cv2.LINE_AA)
111
+
112
+ return (overlay)
uv.lock CHANGED
The diff for this file is too large to render. See raw diff