Commit ·
22bb5ba
1
Parent(s): b75ea7b
Update garb cls to render on colors and infer 3 models together
Browse files422 unmodified lines423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471422 unmodified linesimg_id = _uid()img_path = f"{UPLOAD_DIR}/{img_id}_{file.filename}"out_path = f"{OUTPUT_DIR}/{img_id}_classified.jpg"# Load filewith open(img_path, "wb") as f:shutil.copyfileobj(file.file, f)# Read imageprint(f"[Classification] Received image: {img_path}")image = cv2.imread(img_path)rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)pil = Image.fromarray(rgb)# DETR for garbage detection boxesdetections = []inp = processor_detr(images=pil, return_tensors="pt")with torch.no_grad():out = model_detr(**inp)results = processor_detr.post_process_object_detection(outputs=out,target_sizes=torch.tensor([pil.size[::-1]]),threshold=0.5)[0]# Bbox returnboxes = results["boxes"]print(f"[Classification] {len(boxes)} garbage objects detected by DETR.")# Mapping in betweenfor i, box in enumerate(boxes):x1, y1, x2, y2 = map(int, box.tolist())crop = image[y1:y2, x1:x2]if crop.shape[0] < 10 or crop.shape[1] < 10:continue # skip tiny crops# Convert crop to RGB and classifypil_crop = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))pred = model_garbage_cls(pil_crop, verbose=False)[0]class_id = int(pred.probs.top1)class_name = model_garbage_cls.names[class_id]conf = pred.probs.top1conf# Labelling on output imagelabel = f"{class_name} ({conf:.2f})"cv2.rectangle(image, (x1, y1), (x2, y2), (0, 165, 255), 2)cv2.putText(image, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 165, 255), 2)# Write image on rendercv2.imwrite(out_path, image)print(f"[Classification] Output saved: {out_path}")return FileResponse(out_path, media_type="image/jpeg")# ── Core pipeline (runs in background thread) ───────────────────────────def _pipeline(uid,img_path):print(f"▶️ [{uid}] processing")422 unmodified lines423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492422 unmodified linesimg_id = _uid()img_path = f"{UPLOAD_DIR}/{img_id}_{file.filename}"out_path = f"{OUTPUT_DIR}/{img_id}_classified.jpg"# Save uploaded filewith open(img_path, "wb") as f:shutil.copyfileobj(file.file, f)# Read fileprint(f"[Classification] Received image: {img_path}")image = cv2.imread(img_path)rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)pil = Image.fromarray(rgb)# ─── Detection from 3 models ─────────────────────────────detections = []# YOLOv11 (self-trained)for r in model_self(image):detections += [b.xyxy[0].tolist() for b in r.boxes]# YOLOv5r = model_yolo5(image)if hasattr(r, 'pred') and len(r.pred) > 0:detections += [p[:4].tolist() for p in r.pred[0]]# DETRwith torch.no_grad():out = model_detr(**processor_detr(images=pil, return_tensors="pt"))results = processor_detr.post_process_object_detection(outputs=out,target_sizes=torch.tensor([pil.size[::-1]]),threshold=0.5)[0]detections += [b.tolist() for b in results["boxes"]]print(f"[Classification] Total detections from 3 models: {len(detections)}")# ─── Classification & Rendering ─────────────────────────for box in detections:x1, y1, x2, y2 = map(int, box)x1, x2 = max(0, min(x1, 639)), max(0, min(x2, 639))y1, y2 = max(0, min(y1, 639)), max(0, min(y2, 639))# Stack all cropscrop = image[y1:y2, x1:x2]if crop.shape[0] < 10 or crop.shape[1] < 10:continue# Image processingpil_crop = Image.fromarray(cv2.cvtColor(crop, cv2.COLOR_BGR2RGB))with torch.no_grad():pred = model_garbage_cls(pil_crop, verbose=False)[0]class_id = int(pred.probs.top1)class_name = model_garbage_cls.names[class_id]conf = float(pred.probs.top1conf)# Label formatlabel = f"{class_name} ({conf:.2f})"# Dynamic color codingif conf < 0.4:color = (0, 0, 255) # Redelif conf < 0.6:color = (0, 255, 0) # Greenelif conf < 0.8:color = (255, 255, 0) # Sky Blueelse:color = (255, 0, 255) # Purple# Labellingcv2.rectangle(image, (x1, y1), (x2, y2), color, 2)cv2.putText(image, label, (x1, y1 - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)# Save resultcv2.imwrite(out_path, image)print(f"[Classification] Output saved: {out_path}")return FileResponse(out_path, media_type="image/jpeg")# ── Core pipeline (runs in background thread) ───────────────────────────def _pipeline(uid,img_path):print(f"▶️ [{uid}] processing")
app.py+40-19