mayankchugh-learning commited on
Commit
d6edd1f
·
verified ·
1 Parent(s): 35939e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw
3
+
4
+ # Use a pipeline as a high-level helper
5
+ from transformers import pipeline
6
+
7
+ # pipe = pipeline("object-detection", model="facebook/detr-resnet-50")
8
+
9
+ model_path = "../Models/models--facebook--detr-resnet-50/snapshots/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b"
10
+
11
+ object_detector = pipeline("object-detection", model=model_path)
12
+
13
+ def draw_bounding_boxes(image, object_detections):
14
+ """
15
+ Draws bounding boxes around detected objects on a PIL image.
16
+
17
+ Args:
18
+ image (PIL.Image): The input image.
19
+ object_detections (list): A list of dictionaries, where each dictionary represents a detected object.
20
+ Each dictionary should have the following keys:
21
+ - 'score': the confidence score of the detection
22
+ - 'label': the label of the detected object
23
+ - 'box': a dictionary with keys 'xmin', 'ymin', 'xmax', 'ymax'
24
+ representing the bounding box coordinates.
25
+
26
+ Returns:
27
+ PIL.Image: The input image with bounding boxes drawn around the detected objects.
28
+ """
29
+ draw = ImageDraw.Draw(image)
30
+ for detection in object_detections:
31
+ box = detection['box']
32
+ label = detection['label']
33
+ score = detection['score']
34
+
35
+ # Draw the bounding box
36
+ draw.rectangle((box['xmin'], box['ymin'], box['xmax'], box['ymax']), outline=(255, 0, 0), width=2)
37
+
38
+ # Draw the label and score
39
+ text = f"{label} ({score:.2f})"
40
+ draw.text((box['xmin'], box['ymin'] - 20), text, fill=(255, 0, 0))
41
+
42
+ return image
43
+
44
+ def detect_object(image):
45
+ # raw_image = Image.open(image)
46
+ output = object_detector(image)
47
+ processed_image = draw_bounding_boxes(image, output)
48
+ return processed_image
49
+
50
+ gr.close_all()
51
+
52
+ demo = gr.Interface(fn=detect_object,
53
+ inputs=[gr.Image(label="Select Image", type="pil")],
54
+ outputs=[gr.Image(label="Processed Image", type="pil")],
55
+ title="@IT AI Enthusiast (https://www.youtube.com/@itaienthusiast/) - Project 6: Object Detector",
56
+ description="THIS APPLICATION WILL BE USED TO DETECT OBJECT INSIDE THE PROVIDED INPUT IMGAES",
57
+ # examples=['Hello Friends, Welcome to my channel. I hope this video helps you understand AI.','Hello friends how are you?'],
58
+ concurrency_limit=16)
59
+ demo.launch()