Rameshwari Siddharam Kadrame commited on
Commit
18ea73b
·
verified ·
1 Parent(s): cbf322d

Create app with boundry.py

Browse files
Files changed (1) hide show
  1. app with boundry.py +81 -0
app with boundry.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageFont
3
+
4
+
5
+ # Use a pipeline as a high-level helper
6
+ from transformers import pipeline
7
+
8
+ #model_path = ("../Models/models--facebook--detr-resnet-50/snapshots/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b")
9
+
10
+ object_detector = pipeline("object-detection",
11
+ model="facebook/detr-resnet-50")
12
+
13
+ #object_detector = pipeline("object-detection",
14
+ # model=model_path)
15
+
16
+
17
+ def draw_bounding_boxes(image, detections, font_path=None, font_size=20):
18
+ """
19
+ Draws bounding boxes on the given image based on the detections.
20
+ :param image: PIL.Image object
21
+ :param detections: List of detection results, where each result is a dictionary containing
22
+ 'score', 'label', and 'box' keys. 'box' itself is a dictionary with 'xmin',
23
+ 'ymin', 'xmax', 'ymax'.
24
+ :param font_path: Path to the TrueType font file to use for text.
25
+ :param font_size: Size of the font to use for text.
26
+ :return: PIL.Image object with bounding boxes drawn.
27
+ """
28
+ # Make a copy of the image to draw on
29
+ draw_image = image.copy()
30
+ draw = ImageDraw.Draw(draw_image)
31
+
32
+ # Load custom font or default font if path not provided
33
+ if font_path:
34
+ font = ImageFont.truetype(font_path, font_size)
35
+ else:
36
+ # When font_path is not provided, load default font but it's size is fixed
37
+ font = ImageFont.load_default()
38
+ # Increase font size workaround by using a TTF font file, if needed, can download and specify the path
39
+
40
+ for detection in detections:
41
+ box = detection['box']
42
+ xmin = box['xmin']
43
+ ymin = box['ymin']
44
+ xmax = box['xmax']
45
+ ymax = box['ymax']
46
+
47
+ # Draw the bounding box
48
+ draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
49
+
50
+ # Optionally, you can also draw the label and score
51
+ label = detection['label']
52
+ score = detection['score']
53
+ text = f"{label} {score:.2f}"
54
+
55
+ # Draw text with background rectangle for visibility
56
+ if font_path: # Use the custom font with increased size
57
+ text_size = draw.textbbox((xmin, ymin), text, font=font)
58
+ else:
59
+ # Calculate text size using the default font
60
+ text_size = draw.textbbox((xmin, ymin), text)
61
+
62
+ draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
63
+ draw.text((xmin, ymin), text, fill="white", font=font)
64
+
65
+ return draw_image
66
+
67
+
68
+ def detect_object(image):
69
+ raw_image = image
70
+ output = object_detector(raw_image)
71
+ processed_image = draw_bounding_boxes(raw_image, output)
72
+ return processed_image
73
+
74
+ demo = gr.Interface(fn=detect_object,
75
+ inputs=[gr.Image(label="Select Image",type="pil")],
76
+ outputs=[gr.Image(label="Processed Image", type="pil")],
77
+ title="GenAI Project: Object Detector",
78
+ description="THIS APPLICATION WILL BE USED TO DETECT OBJECTS INSIDE THE PROVIDED INPUT IMAGE.")
79
+ demo.launch()
80
+
81
+ # print(output)