sbby commited on
Commit
ca70a0b
·
verified ·
1 Parent(s): 8f42de3

Create app.py

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