IshanGenAI commited on
Commit
cb8b655
1 Parent(s): 251e18a

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
3
+
4
+ # Use a pipeline as a high-level helper
5
+ from transformers import pipeline
6
+
7
+
8
+ # model_path = ("../models/models--facebook--detr-resnet-50/snapshots/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b")
9
+
10
+ # object_detector = pipeline("object-detection", model=model_path)
11
+
12
+ object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
13
+
14
+
15
+
16
+ # raw_image = Image .open("../Files/child dog ball.jpg")
17
+ # output = object_detector(raw_image)
18
+ # print(output)
19
+
20
+ def draw_bounding_boxes(image, detections):
21
+ """
22
+ Draws bounding boxes around detected objects on the image.
23
+
24
+ Parameters:
25
+ image (PIL.Image): The input image.
26
+ detections (list): A list of dictionaries containing detection results.
27
+
28
+ Returns:
29
+ PIL.Image: The image with bounding boxes drawn around detected objects.
30
+ """
31
+ # Create an ImageDraw object to draw on the image
32
+ draw = ImageDraw.Draw(image)
33
+
34
+ # Iterate over each detected object
35
+ for detection in detections:
36
+ box = detection['box']
37
+ label = detection['label']
38
+ score = detection['score']
39
+
40
+ # Define the coordinates of the bounding box
41
+ xmin, ymin, xmax, ymax = box['xmin'], box['ymin'], box['xmax'], box['ymax']
42
+
43
+ # Draw the bounding box on the image
44
+ draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=2)
45
+
46
+ # Draw the label and score on the image
47
+ text = f"{label} ({score:.2f})"
48
+ draw.text((xmin, ymin - 10), text, fill="red")
49
+
50
+ return image
51
+ #raw_image = Image .open("image")
52
+ def detect_object(image):
53
+ raw_image = image
54
+ output = object_detector(raw_image)
55
+ processed_image = draw_bounding_boxes(raw_image, output)
56
+ return processed_image
57
+
58
+ # print(output)
59
+
60
+
61
+ gr.close_all()
62
+
63
+ # Create the Gradio interface
64
+ demo = gr.Interface(
65
+ fn=detect_object,
66
+ inputs=gr.Image(label="Select an Image", type="pil"),
67
+ outputs=gr.Image(label="Processed Image", type="pil"),
68
+ title="GenAI Project 6: Object Detector",
69
+ description="THIS APPLICATION WILL BE USED TO DETECT OBJECTS IN THE PROVIDED INPUT"
70
+ )
71
+
72
+ # Launch the Gradio interface
73
+ demo.launch()