prograk commited on
Commit
578ff4e
·
verified ·
1 Parent(s): 708dc5b

Create app.py

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