Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435# app.pyfrom transformers import pipelineimport gradio as grfrom PIL import Image, ImageDraw# Load the object detection pipeline with YOLOdetector = pipeline("object-detection", model="hustvl/yolos-tiny")# Define the detection functiondef detect_objects(img):results = detector(img)# Draw bounding boxesdraw = ImageDraw.Draw(img)for obj in results:box = obj["box"]label = f"{obj['label']} ({obj['score']:.2f})"draw.rectangle([box["xmin"], box["ymin"], box["xmax"], box["ymax"]], outline="red", width=3)draw.text((box["xmin"], box["ymin"] - 10), label, fill="red")return img# Create the Gradio interfaceinterface = gr.Interface(fn=detect_objects,inputs=gr.Image(type="pil"),outputs=gr.Image(type="pil"),title="YOLO Object Detection",description="Upload an image to detect objects using a YOLOS-Tiny model.")# Launch the appif __name__ == "__main__":interface.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667# app.py# app.pyfrom transformers import pipelineimport gradio as grfrom PIL import Image, ImageDraw, ImageFontimport random# Load the YOLO-based object detection pipelinedetector = pipeline("object-detection", model="hustvl/yolos-tiny")# Generate a random color for each labellabel_colors = {}def get_color(label):if label not in label_colors:label_colors[label] = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))return label_colors[label]# Detection functiondef detect_objects(img):results = detector(img)draw = ImageDraw.Draw(img)font = ImageFont.load_default()for obj in results:label = obj["label"]score = obj["score"]box = obj["box"]color = get_color(label)# Draw bounding boxdraw.rectangle([box["xmin"], box["ymin"], box["xmax"], box["ymax"]],outline=color,width=3)# Prepare label with confidencelabel_text = f"{label} ({score:.2f})"text_size = draw.textsize(label_text, font=font)text_background = [box["xmin"], box["ymin"] - text_size[1], box["xmin"] + text_size[0], box["ymin"]]# Draw background for textdraw.rectangle(text_background, fill=color)draw.text((box["xmin"], box["ymin"] - text_size[1]), label_text, fill="black", font=font)return img# Gradio interfaceinterface = gr.Interface(fn=detect_objects,inputs=gr.Image(type="pil"),outputs=gr.Image(type="pil"),title="YOLO Object Detection with Color-coded Labels",description="Upload an image. Detected objects are shown with bounding boxes and color-coded labels using YOLOS-Tiny.")if __name__ == "__main__":interface.launch()
app.py+44-12