# !pip install timm # !pip install transformers # !pip install pillow # !pip install gradio from PIL import Image, ImageDraw import gradio as gr from transformers import pipeline # Initialize the object detection pipeline object_detector = pipeline("object-detection", model="facebook/detr-resnet-50") def detect_objects(image): # Perform object detection on the input image results = object_detector(image) # Create a draw object draw = ImageDraw.Draw(image) # Draw bounding boxes around detected objects for result in results: label = result['label'] score = result['score'] box = result['box'] # Draw the bounding box draw.rectangle( [(box['xmin'], box['ymin']), (box['xmax'], box['ymax'])], outline="red", width=2 ) # Draw the label and score draw.text((box['xmin'], box['ymin']), f"{label} {score:.2f}", fill="white",size=20) return image # Create the Gradio interface interface = gr.Interface( fn=detect_objects, inputs=gr.Image(type="pil"), outputs=gr.Image(type="pil"), title="Image Object Detector", description="Upload an image to detect objects using the DETR model." ) # Launch the app interface.launch()