humbleakh commited on
Commit
cf48499
·
verified ·
1 Parent(s): 00296d7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from transformers import pipeline
4
+ from PIL import ImageDraw
5
+
6
+ object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
7
+
8
+ # model_path = "C:\\Users\\abdul\\Documents\\genaiproj\\genai\\Models\\models--facebook--detr-resnet-50\\snapshots\\1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b"
9
+ # object_detector = pipeline("object-detection", model=model_path)
10
+
11
+ def draw_bounding_boxes(image, detections):
12
+ draw = ImageDraw.Draw(image)
13
+
14
+ for detection in detections:
15
+ # Extract bounding box coordinates and label
16
+ box = detection['box']
17
+ label = detection['label']
18
+ xmin, ymin, xmax, ymax = box['xmin'], box['ymin'], box['xmax'], box['ymax']
19
+
20
+ # Draw rectangle and label
21
+ draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=3)
22
+ draw.text((xmin, ymin - 10), label, fill="red")
23
+
24
+ return image
25
+
26
+
27
+ def detect_object(image):
28
+ raw_image = Image.open(image)
29
+ output = object_detector(raw_image)
30
+ processed_image = draw_bounding_boxes(raw_image, output)
31
+ return processed_image
32
+
33
+ # print(output)
34
+
35
+ gr.close_all()
36
+
37
+ # demo = gr.Interface(fn=summary, inputs="text", outputs="text")
38
+
39
+ demo = gr.Interface(
40
+ fn=detect_object,
41
+ inputs=[gr.Image(label="Select Image", type="filepath")],
42
+ outputs=[gr.Image(label="Image with Bounding Box", type="pil")],
43
+ title="Object Detector",
44
+ theme="soft",
45
+ description="This is an object detection model that detects objects in an image and draws bounding boxes around them.")
46
+
47
+ demo.launch(share=True)