import gradio as gr import pandas as pd from transformers import AutoImageProcessor, AutoModelForObjectDetection from PIL import Image, ImageDraw import torch image_processor = AutoImageProcessor.from_pretrained('hustvl/yolos-small') model = AutoModelForObjectDetection.from_pretrained('hustvl/yolos-small') def detect(image): inputs = image_processor(images=image, return_tensors="pt") outputs = model(**inputs) # convert outputs to COCO API target_sizes = torch.tensor([image.size[::-1]]) results = image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0] draw = ImageDraw.Draw(image) # label and the count counts = {} for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): box = [round(i, 4) for i in box.tolist()] label_name = model.config.id2label[label.item()] if label_name not in counts: counts[label_name] = 0 counts[label_name] += 1 x1, y1, x2, y2 = tuple(box) draw.rectangle((x1, y1, x2, y2), outline=[128, 128, 50], width=2) draw.text((x1, y1), label_name, fill="white") df = pd.DataFrame({ 'label': [label for label in counts], 'counts': [counts[label] for label in counts] }) return image, df, counts demo = gr.Interface( fn=detect, inputs=[gr.inputs.Image(label="Input image", type="pil")], outputs=["image", gr.BarPlot(x="label", y="counts", x_title="Labels", y_title="Counts"), gr.Textbox()], title="Object Counts in Image" ) demo.launch()