GenAILearniverse commited on
Commit
cf76589
·
verified ·
1 Parent(s): 67082ee

Create app.py

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