Spaces:
Runtime error
Runtime error
| """ | |
| Room Object Segmentation API - Placeholder Version | |
| Returns mock data for frontend development | |
| Real ML inference coming soon with ZeroGPU optimization | |
| """ | |
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image, ImageDraw, ImageFont | |
| import random | |
| def create_placeholder_annotated_image(image_path, num_objects): | |
| """Create a placeholder annotated image with bounding boxes.""" | |
| img = Image.open(image_path).convert("RGB") | |
| draw = ImageDraw.Draw(img) | |
| width, height = img.size | |
| # Draw some random boxes | |
| colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange'] | |
| for i in range(num_objects): | |
| x1 = random.randint(0, width - 200) | |
| y1 = random.randint(0, height - 200) | |
| x2 = x1 + random.randint(100, 200) | |
| y2 = y1 + random.randint(100, 200) | |
| color = colors[i % len(colors)] | |
| draw.rectangle([x1, y1, x2, y2], outline=color, width=3) | |
| return img | |
| def segment(image, text_prompt, box_threshold=0.35, text_threshold=0.25): | |
| """ | |
| PLACEHOLDER API - Returns mock detection data for frontend development. | |
| Real Grounding DINO + SAM2 inference will be enabled after ZeroGPU optimization. | |
| """ | |
| if image is None: | |
| return None, {"error": "No image provided"} | |
| try: | |
| # Parse prompt | |
| objects = [obj.strip() for obj in text_prompt.split('.') if obj.strip()] | |
| # Create mock detections (3-5 random objects from prompt) | |
| num_detections = min(random.randint(3, 5), len(objects)) | |
| detected_objects = random.sample(objects, num_detections) | |
| # Generate realistic mock data | |
| detections = [] | |
| for i, obj in enumerate(detected_objects): | |
| detections.append({ | |
| "label": obj, | |
| "score": round(random.uniform(0.7, 0.95), 2), | |
| "sam_score": round(random.uniform(0.85, 0.99), 2), | |
| "box": [ | |
| random.randint(50, 300), | |
| random.randint(50, 300), | |
| random.randint(350, 600), | |
| random.randint(350, 600) | |
| ], | |
| "area": random.randint(15000, 50000) | |
| }) | |
| # Create placeholder annotated image | |
| annotated_img = create_placeholder_annotated_image(image, num_detections) | |
| result = { | |
| "status": "placeholder", | |
| "message": "π§ Placeholder response for frontend development. Real ML inference coming soon.", | |
| "num_detections": num_detections, | |
| "prompt": text_prompt, | |
| "box_threshold": box_threshold, | |
| "text_threshold": text_threshold, | |
| "detections": detections, | |
| "cached": False, | |
| "inference_time_ms": random.randint(50, 150), | |
| "model_info": { | |
| "grounding_dino": "IDEA-Research/grounding-dino-tiny (placeholder)", | |
| "sam2": "facebook/sam2.1-hiera-small (placeholder)" | |
| } | |
| } | |
| return annotated_img, result | |
| except Exception as e: | |
| import traceback | |
| error_msg = f"Error: {str(e)}\n{traceback.format_exc()}" | |
| print(error_msg) | |
| return None, {"error": error_msg} | |
| # Gradio interface | |
| demo = gr.Interface( | |
| fn=segment, | |
| inputs=[ | |
| gr.Image(type="filepath", label="Upload room image"), | |
| gr.Textbox( | |
| value="chair . table . sofa . lamp . bed . cabinet . shelf . desk", | |
| label="Objects to detect (dot-separated)" | |
| ), | |
| gr.Slider(0, 1, value=0.35, label="Box threshold"), | |
| gr.Slider(0, 1, value=0.25, label="Text threshold") | |
| ], | |
| outputs=[ | |
| gr.Image(label="Annotated Image"), | |
| gr.JSON(label="Detection Results") | |
| ], | |
| title="π Room Object Segmentation API (Placeholder)", | |
| description=""" | |
| **π§ PLACEHOLDER VERSION FOR FRONTEND DEVELOPMENT** | |
| This API returns mock detection data so frontend developers can integrate while we optimize ZeroGPU inference. | |
| β **What works now:** | |
| - API contract matches final version | |
| - Realistic response structure | |
| - Random detections from your prompt | |
| - Placeholder annotated images | |
| π **Coming soon:** | |
| - Real Grounding DINO + SAM2 inference | |
| - Accurate object detection | |
| - Proper segmentation masks | |
| - GPU-optimized with ZeroGPU | |
| **For Frontend:** Use this to develop the UI/UX. Response structure will remain the same when we enable real ML. | |
| """, | |
| examples=[ | |
| ["resources/Images/room.jpg", "chair . table . sofa . lamp", 0.35, 0.25] | |
| ], | |
| api_name="segment", | |
| show_error=True | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True) | |