UdaraChamidu commited on
Commit
ff8dfa9
·
verified ·
1 Parent(s): 113a54e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -28
app.py CHANGED
@@ -9,26 +9,38 @@ from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
9
  from tensorflow.keras.models import load_model
10
  from fpdf import FPDF
11
  import matplotlib.pyplot as plt
12
- # from ultralytics import YOLO # Commented out for now
13
 
14
  # -----------------------------
15
  # Flask Config
16
  # -----------------------------
17
  app = Flask(__name__)
18
  app.config["UPLOAD_FOLDER"] = "static/uploads"
 
19
  os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
20
 
21
  # -----------------------------
22
  # Load Keras classification model
23
  try:
24
  best_model = load_model("efficientnet_b0_best.keras")
25
- print("✅ Model loaded successfully!")
26
  except Exception as e:
27
- print(f"❌ Error loading model: {e}")
28
  best_model = None
29
 
 
 
 
 
 
 
 
 
 
 
30
  IMG_SIZE = 128
31
 
 
32
  CLASS_LABELS = ['biological', 'brown-glass', 'cardboard', 'green-glass',
33
  'metal', 'paper', 'plastic', 'shoes', 'trash', 'white-glass']
34
 
@@ -37,12 +49,83 @@ NON_RECYCLABLE = ["trash", "biological", "shoes"]
37
 
38
  stats = {}
39
 
 
 
 
40
  # -----------------------------
41
- # Load YOLOv8 model (disabled for Hugging Face Spaces)
42
- yolo_model = YOLO("best.pt") # Uncomment and add your YOLO model file if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # -----------------------------
45
- # Preprocess image for classification
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def preprocess_image(file_path):
47
  img = cv2.imread(file_path)
48
  if img is None:
@@ -96,6 +179,8 @@ def generate_pdf_report():
96
  def index():
97
  if request.method == "POST":
98
  file = request.files.get("file")
 
 
99
  if not file or file.filename == "":
100
  return redirect(request.url)
101
 
@@ -109,31 +194,68 @@ def index():
109
  file_path = os.path.join(app.config["UPLOAD_FOLDER"], file.filename)
110
  file.save(file_path)
111
 
112
- if best_model is None:
113
- return render_template("index.html", error="Model not loaded. Please check if the model file exists.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
- img_rgb, img_input = preprocess_image(file_path)
116
- preds = best_model.predict(img_input)
117
- class_idx = np.argmax(preds, axis=1)[0]
118
- class_label = CLASS_LABELS[class_idx]
119
- confidence = preds[0][class_idx]
120
 
121
- log_prediction(class_label)
122
 
123
- if class_label in RECYCLABLE:
124
- bin_type = "Recyclable ♻️"
125
- elif class_label in NON_RECYCLABLE:
126
- bin_type = "Non-Recyclable 🗑️"
127
- else:
128
- bin_type = "Unknown ⚠️"
129
-
130
- return render_template(
131
- "result.html",
132
- image=file.filename,
133
- label=class_label,
134
- confidence=f"{confidence*100:.2f}%",
135
- bin_type=bin_type
136
- )
 
137
  except Exception as e:
138
  return render_template("index.html", error=f"Error processing image: {str(e)}")
139
 
 
9
  from tensorflow.keras.models import load_model
10
  from fpdf import FPDF
11
  import matplotlib.pyplot as plt
12
+ from ultralytics import YOLO
13
 
14
  # -----------------------------
15
  # Flask Config
16
  # -----------------------------
17
  app = Flask(__name__)
18
  app.config["UPLOAD_FOLDER"] = "static/uploads"
19
+ app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024 # 16MB max file size
20
  os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)
21
 
22
  # -----------------------------
23
  # Load Keras classification model
24
  try:
25
  best_model = load_model("efficientnet_b0_best.keras")
26
+ print("✅ EfficientNet model loaded successfully!")
27
  except Exception as e:
28
+ print(f"❌ Error loading EfficientNet model: {e}")
29
  best_model = None
30
 
31
+ # -----------------------------
32
+ # Load YOLO model
33
+ try:
34
+ yolo_model = YOLO("best.pt")
35
+ print("✅ YOLO model loaded successfully!")
36
+ print(f"YOLO model classes: {yolo_model.names}")
37
+ except Exception as e:
38
+ print(f"❌ Error loading YOLO model: {e}")
39
+ yolo_model = None
40
+
41
  IMG_SIZE = 128
42
 
43
+ # EfficientNet classes
44
  CLASS_LABELS = ['biological', 'brown-glass', 'cardboard', 'green-glass',
45
  'metal', 'paper', 'plastic', 'shoes', 'trash', 'white-glass']
46
 
 
49
 
50
  stats = {}
51
 
52
+ # YOLO detection confidence threshold
53
+ YOLO_CONFIDENCE_THRESHOLD = 0.5
54
+
55
  # -----------------------------
56
+ # YOLO object detection function
57
+ def detect_objects_yolo(file_path):
58
+ """Detect objects in image using YOLO model"""
59
+ if yolo_model is None:
60
+ return None, "YOLO model not loaded"
61
+
62
+ try:
63
+ # Read image
64
+ img = cv2.imread(file_path)
65
+ if img is None:
66
+ return None, "Could not read image"
67
+
68
+ # Run YOLO detection
69
+ results = yolo_model(img)[0]
70
+
71
+ detections = []
72
+ if results.boxes is not None and len(results.boxes) > 0:
73
+ boxes = results.boxes.xyxy.cpu().numpy()
74
+ confidences = results.boxes.conf.cpu().numpy()
75
+ class_ids = results.boxes.cls.cpu().numpy().astype(int)
76
+
77
+ for i, box in enumerate(boxes):
78
+ if confidences[i] >= YOLO_CONFIDENCE_THRESHOLD:
79
+ x1, y1, x2, y2 = map(int, box)
80
+ class_name = yolo_model.names[class_ids[i]]
81
+ confidence = confidences[i]
82
+
83
+ detections.append({
84
+ 'class': class_name,
85
+ 'confidence': confidence,
86
+ 'bbox': [x1, y1, x2, y2]
87
+ })
88
+
89
+ return detections, None
90
+ except Exception as e:
91
+ return None, str(e)
92
 
93
  # -----------------------------
94
+ # Draw bounding boxes on image
95
+ def draw_detections(img_path, detections, output_path):
96
+ """Draw YOLO detections on image"""
97
+ try:
98
+ img = cv2.imread(img_path)
99
+
100
+ for detection in detections:
101
+ x1, y1, x2, y2 = detection['bbox']
102
+ class_name = detection['class']
103
+ confidence = detection['confidence']
104
+
105
+ # Choose color based on confidence
106
+ if confidence >= 0.80:
107
+ color = (0, 255, 0) # Green - high confidence
108
+ elif confidence >= 0.60:
109
+ color = (0, 255, 255) # Yellow - medium confidence
110
+ else:
111
+ color = (0, 165, 255) # Orange - low confidence
112
+
113
+ # Draw bounding box
114
+ cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
115
+
116
+ # Draw label background
117
+ label = f"{class_name} {confidence*100:.1f}%"
118
+ (text_width, text_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)
119
+ cv2.rectangle(img, (x1, y1 - text_height - 10), (x1 + text_width, y1), color, -1)
120
+
121
+ # Draw label text
122
+ cv2.putText(img, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2)
123
+
124
+ cv2.imwrite(output_path, img)
125
+ return True
126
+ except Exception as e:
127
+ print(f"Error drawing detections: {e}")
128
+ return False
129
  def preprocess_image(file_path):
130
  img = cv2.imread(file_path)
131
  if img is None:
 
179
  def index():
180
  if request.method == "POST":
181
  file = request.files.get("file")
182
+ detection_mode = request.form.get("detection_mode", "classification")
183
+
184
  if not file or file.filename == "":
185
  return redirect(request.url)
186
 
 
194
  file_path = os.path.join(app.config["UPLOAD_FOLDER"], file.filename)
195
  file.save(file_path)
196
 
197
+ if detection_mode == "yolo" and yolo_model is not None:
198
+ # YOLO Object Detection Mode
199
+ detections, error = detect_objects_yolo(file_path)
200
+
201
+ if error:
202
+ return render_template("index.html", error=f"YOLO detection error: {error}")
203
+
204
+ if detections:
205
+ # Draw detections on image
206
+ output_filename = f"detected_{file.filename}"
207
+ output_path = os.path.join(app.config["UPLOAD_FOLDER"], output_filename)
208
+ draw_detections(file_path, detections, output_path)
209
+
210
+ # Log detections
211
+ for detection in detections:
212
+ log_prediction(detection['class'])
213
+
214
+ return render_template(
215
+ "yolo_result.html",
216
+ original_image=file.filename,
217
+ detected_image=output_filename,
218
+ detections=detections,
219
+ detection_count=len(detections)
220
+ )
221
+ else:
222
+ return render_template(
223
+ "yolo_result.html",
224
+ original_image=file.filename,
225
+ detected_image=file.filename,
226
+ detections=[],
227
+ detection_count=0,
228
+ message="No objects detected with sufficient confidence."
229
+ )
230
+
231
+ else:
232
+ # EfficientNet Classification Mode
233
+ if best_model is None:
234
+ return render_template("index.html", error="Classification model not loaded. Please check if the model file exists.")
235
 
236
+ img_rgb, img_input = preprocess_image(file_path)
237
+ preds = best_model.predict(img_input)
238
+ class_idx = np.argmax(preds, axis=1)[0]
239
+ class_label = CLASS_LABELS[class_idx]
240
+ confidence = preds[0][class_idx]
241
 
242
+ log_prediction(class_label)
243
 
244
+ if class_label in RECYCLABLE:
245
+ bin_type = "Recyclable ♻️"
246
+ elif class_label in NON_RECYCLABLE:
247
+ bin_type = "Non-Recyclable 🗑️"
248
+ else:
249
+ bin_type = "Unknown ⚠️"
250
+
251
+ return render_template(
252
+ "result.html",
253
+ image=file.filename,
254
+ label=class_label,
255
+ confidence=f"{confidence*100:.2f}%",
256
+ bin_type=bin_type
257
+ )
258
+
259
  except Exception as e:
260
  return render_template("index.html", error=f"Error processing image: {str(e)}")
261