basharat8763 commited on
Commit
654aed7
·
verified ·
1 Parent(s): 8e25fd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py CHANGED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from PIL import Image, ImageDraw, ImageFont
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/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b"
9
+
10
+ object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
11
+
12
+ # object_detector = pipeline("object-detection", model=model_path)
13
+
14
+
15
+ def draw_bounding_boxes(image, detections, font_path=None, font_size=500):
16
+ """
17
+ Draws bounding boxes on the given image based on the detections.
18
+ :param image: PIL.Image object
19
+ :param detections: List of detection results, where each result is a dictionary containing
20
+ 'score', 'label', and 'box' keys. 'box' itself is a dictionary with 'xmin',
21
+ 'ymin', 'xmax', 'ymax' keys.
22
+ :param font_path: Path to the TrueType font file to use for text.
23
+ :param font_size: Size of the font to use for text.
24
+ :return: PIL.Image object with bounding boxes drawn.
25
+ """
26
+ # Make a copy of the image to draw on
27
+ draw_image = image.copy()
28
+ draw = ImageDraw.Draw(draw_image)
29
+
30
+ # Load custom font or default font if path not provided
31
+ if font_path:
32
+ font = ImageFont.truetype(font_path, font_size)
33
+ else:
34
+ # Use default font, increase font size with TTF font if necessary
35
+ font = ImageFont.load_default()
36
+
37
+ for detection in detections:
38
+ box = detection['box']
39
+ xmin = box['xmin']
40
+ ymin = box['ymin']
41
+ xmax = box['xmax']
42
+ ymax = box['ymax']
43
+
44
+ # Draw the bounding box
45
+ draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
46
+
47
+ # Optionally, draw the label and score
48
+ label = detection['label']
49
+ score = detection['score']
50
+ text = f"{label} {score:.2f}"
51
+
52
+ # Draw text with background rectangle for visibility
53
+ if font_path:
54
+ text_size = draw.textbbox((xmin, ymin), text, font=font)
55
+ else:
56
+ text_size = draw.textbbox((xmin, ymin), text)
57
+
58
+ draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
59
+ draw.text((xmin, ymin), text, fill="white", font=font)
60
+
61
+ return draw_image
62
+
63
+
64
+ # for text output
65
+ # raw_image = Image.open("../Files/cat.jpg")
66
+ # output = object_detector(raw_image)
67
+ # print(output)
68
+
69
+
70
+ # Function for object detection and bounding box drawing
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
+
78
+ demo = gr.Interface(
79
+ fn=detect_object,
80
+ inputs=[gr.Image(label="Select Image", type="pil")],
81
+ outputs=[gr.Image(label="Processed Image", type="pil")],
82
+ title="Project 05: Object Detector",
83
+ description="As understood from the title, if not already, this application will detect objects in your image"
84
+ )
85
+
86
+ demo.launch()