Sahibhim commited on
Commit
0c7e22e
·
verified ·
1 Parent(s): b4f66bc

Create app.py

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