Ifeanyi commited on
Commit
c963191
·
verified ·
1 Parent(s): 3910587

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -129
app.py CHANGED
@@ -1,137 +1,12 @@
1
- import supervision as sv
2
- from ultralytics import YOLO
3
- from PIL import Image
4
  import gradio as gr
5
- import numpy as np
6
- import cv2
7
- import urllib
8
 
9
- # load pre-trained vision model
10
- model = YOLO("yolo12s.pt")
11
-
12
- def image_annotate(image:str, annotator:str) -> Image.Image:
13
- """
14
- Args:
15
- image: the path to the image file
16
- annotator: the type of annotator to use
17
- Returns:
18
- annotated image
19
- """
20
- # load the input image
21
- image = cv2.imread(image)
22
-
23
- # run object detection on the image
24
- result = model(image)[0]
25
-
26
- # convert YOLO output to a Supervision-compatible detections format
27
- detections = sv.Detections.from_ultralytics(result)
28
-
29
- annotated_image_show = None
30
-
31
- # select annotator
32
- if annotator == "Box":
33
- box_annotator = sv.BoxAnnotator()
34
- annotated_image_show = box_annotator.annotate(
35
- scene=image.copy(),
36
- detections=detections)
37
-
38
- elif annotator == "Roundbox":
39
- round_box_annotator = sv.RoundBoxAnnotator()
40
- annotated_image_show = round_box_annotator.annotate(
41
- scene=image.copy(),
42
- detections=detections)
43
-
44
- elif annotator == "Boxcorner":
45
- corner_annotator = sv.BoxCornerAnnotator()
46
- annotated_image_show = corner_annotator.annotate(
47
- scene=image.copy(),
48
- detections=detections)
49
-
50
- elif annotator == "Color":
51
- color_annotator = sv.ColorAnnotator()
52
- annotated_image_show = color_annotator.annotate(
53
- scene=image.copy(),
54
- detections=detections)
55
-
56
- elif annotator == "Circle":
57
- circle_annotator = sv.CircleAnnotator()
58
- annotated_image_show = circle_annotator.annotate(
59
- scene=image.copy(),
60
- detections=detections)
61
-
62
- elif annotator == "Dot":
63
- dot_annotator = sv.DotAnnotator()
64
- annotated_image_show = dot_annotator.annotate(
65
- scene=image.copy(),
66
- detections=detections)
67
-
68
- elif annotator == "Triangle":
69
- triangle_annotator = sv.TriangleAnnotator()
70
- annotated_image_show = triangle_annotator.annotate(
71
- scene=image.copy(),
72
- detections=detections)
73
-
74
- elif annotator == "Ellipse":
75
- ellipse_annotator = sv.EllipseAnnotator()
76
- annotated_image_show = ellipse_annotator.annotate(
77
- scene=image.copy(),
78
- detections=detections)
79
-
80
- elif annotator == "Percentage":
81
- percentage_bar_annotator = sv.PercentageBarAnnotator()
82
- annotated_image_show = percentage_bar_annotator.annotate(
83
- scene=image.copy(),
84
- detections=detections)
85
-
86
- elif annotator == "Heatmap":
87
- heatmap_annotator = sv.HeatMapAnnotator()
88
- annotated_image_show = heatmap_annotator.annotate(
89
- scene=image.copy(),
90
- detections=detections)
91
-
92
- elif annotator == "Label":
93
- labels = [
94
- f"{class_id} {confidence:.2f}"
95
- for class_id, confidence
96
- in zip(detections.class_id, detections.confidence)
97
- ]
98
-
99
- rich_label_annotator = sv.RichLabelAnnotator(
100
- text_color=sv.Color.BLACK,
101
- text_padding=10,
102
- text_position=sv.Position.CENTER)
103
-
104
- annotated_image_show = rich_label_annotator.annotate(
105
- scene=image.copy(),
106
- detections=detections,
107
- labels=labels )
108
-
109
- elif annotator == "Blur":
110
- blur_annotator = sv.BlurAnnotator()
111
- annotated_image_show = blur_annotator.annotate(
112
- scene=image.copy(),
113
- detections=detections)
114
-
115
- elif annotator == "Pixelate":
116
- pixelate_annotator = sv.PixelateAnnotator()
117
- annotated_image_show = pixelate_annotator.annotate(
118
- scene=image.copy(),
119
- detections=detections)
120
-
121
- elif annotator == "Backgroundcolor":
122
- background_overlay_annotator = sv.BackgroundOverlayAnnotator()
123
- annotated_image_show = background_overlay_annotator.annotate(
124
- scene=image.copy(),
125
- detections=detections)
126
-
127
- # return annotated image
128
- return annotated_image_show
129
-
130
- app = gr.Interface(
131
  fn = image_annotate,
132
  theme = "gstaff/sketch",
133
  title="Object Detection",
134
- inputs = [gr.Image(type="filepath",label="Image"),gr.Radio(label="Select Annotator",
135
  choices=["Box","Roundbox","Boxcorner","Color","Circle","Dot","Triangle",
136
  "Ellipse","Percentage","Heatmap","Label","Blur",
137
  "Pixelate","Backgroundcolor"],
@@ -145,5 +20,16 @@ app = gr.Interface(
145
  ["bowling_ball.jpg","Percentage"]]
146
  )
147
 
 
 
 
 
 
 
 
 
 
 
 
148
  if __name__ == "__main__":
149
  app.launch(mcp_server = True)
 
 
 
 
1
  import gradio as gr
2
+ from ImageAnnotate import image_annotate
3
+ from PoseAnnotate import pose_annotate
 
4
 
5
+ objectDetector = gr.Interface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  fn = image_annotate,
7
  theme = "gstaff/sketch",
8
  title="Object Detection",
9
+ inputs = [gr.Image(type="filepath",label="Image"), gr.Radio(label="Select Annotator",
10
  choices=["Box","Roundbox","Boxcorner","Color","Circle","Dot","Triangle",
11
  "Ellipse","Percentage","Heatmap","Label","Blur",
12
  "Pixelate","Backgroundcolor"],
 
20
  ["bowling_ball.jpg","Percentage"]]
21
  )
22
 
23
+ poseDetector = gr.Interface(
24
+ fn = pose_annotate,
25
+ title="Pose Detection",
26
+ inputs = [gr.Image(type="filepath",label="Image"), gr.Radio(label="Select Annotator",
27
+ choices=["Vertex","Edge","Vertexlabel"],
28
+ value = "Vertex")],
29
+ outputs = gr.Image(label = "Annotated Image"),
30
+ )
31
+
32
+ app = gr.TabbedInterface([objectDetector,poseDetector],["Object Detection","Pose Detection"])
33
+
34
  if __name__ == "__main__":
35
  app.launch(mcp_server = True)