2seriescs commited on
Commit
1d1e4f9
1 Parent(s): 2744a61

Create app.py

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