File size: 2,164 Bytes
1471130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
from PIL import Image, ImageDraw


# Use a pipeline as a high-level helper
from transformers import pipeline

model_path = ("../Model/models--facebook--detr-resnet-50/snapshots"
              "/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b")


object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")


# object_detector = pipeline("object-detection", model=model_path)



def draw_bounding_boxes(image, detection_results):
    """
    Draws bounding boxes on the provided image based on the detection results.

    Parameters:
        image (PIL.Image): The input image to be annotated.
        detection_results (list): A list of dictionaries, each containing the detected object details.

    Returns:
        PIL.Image: The image with bounding boxes drawn around the detected objects.
    """
    # Convert the input image to ImageDraw object to draw on it
    draw = ImageDraw.Draw(image)

    # Iterate through each detection result
    for result in detection_results:
        # Extract the bounding box coordinates and label
        box = result['box']
        label = result['label']
        score = result['score']

        # Define coordinates for the bounding box
        xmin, ymin, xmax, ymax = box['xmin'], box['ymin'], box['xmax'], box['ymax']

        # Draw the bounding box (with a red outline)
        draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=3)

        # Optionally, add label with score near the bounding box
        text = f"{label} ({score * 100:.1f}%)"
        draw.text((xmin, ymin - 10), text, fill="red")

    return image

def detect_objects(image):
    raw_image = image
    output = object_detector(raw_image)
    processed_image = draw_bounding_boxes(raw_image, output)
    return processed_image



demo = gr.Interface(fn = detect_objects,
                    inputs=[gr.Image(label="Select Image",type="pil")],
                    outputs=[gr.Image(label="Summarized Text ",type="pil")],
                    title="@SherryAhuja Project : Object Detection",
                    description="This AI application will be used to Detect objects in an image.",)
demo.launch()