Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import DetrImageProcessor, DetrForObjectDetection | |
| import torch | |
| from PIL import Image, ImageDraw | |
| # Model loading (same as before - with error handling) | |
| try: | |
| feature_extractor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50") | |
| model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", ignore_mismatched_sizes=True) | |
| except Exception as e: # Error handling during model loading | |
| print(f"Error loading model: {e}") # Log the error so you can see in HF logs | |
| raise e # Re-raise for Space to report it | |
| def predict(image): | |
| inputs = feature_extractor(images=image, return_tensors="pt") | |
| outputs = model(**inputs) | |
| target_sizes = torch.tensor([image.size[::-1]]) | |
| results = feature_extractor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0] | |
| # Draw bounding boxes on the image | |
| draw = ImageDraw.Draw(image) # Create a drawing object | |
| for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): | |
| box = [round(i) for i in box.tolist()] # Convert to integers for drawing | |
| draw.rectangle(box, outline="red", width=2) # Outline | |
| draw.text((box[0], box[1]), model.config.id2label[label.item()], fill="red") # Add a label | |
| return image # Return the image with the bounding boxes drawn | |
| # Gradio Interface (updated output type) | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Image(type="pil", label="Detected Potholes (Image)"), # Updated | |
| title="Pothole Detection POC", | |
| description="Upload an image to detect potholes." | |
| ) | |
| iface.launch() |