janasumit2911 commited on
Commit
0087596
·
verified ·
1 Parent(s): 0a013d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -84
app.py CHANGED
@@ -1,85 +1,3 @@
1
- # import gradio as gr
2
- # from PIL import Image
3
- # from ultralytics import YOLO
4
- # import requests
5
- # import json
6
-
7
- # model = YOLO("BP_Multiple_Objects_Complicated_v1.pt")
8
-
9
- # def detect_objects(images):
10
- # results = model(images)
11
- # all_bboxes = []
12
- # all_bboxes2 = []
13
- # all_segments = []
14
- # for result in results:
15
- # boxes = result.boxes.xywhn.tolist()
16
- # boxes2 = result.boxes.xywh.tolist()
17
- # all_bboxes.append(boxes)
18
- # all_bboxes2.append(boxes2)
19
-
20
- # masks = result.masks.xyn
21
- # sub_arrays = [arr.tolist() for arr in masks]
22
- # all_segments.append(sub_arrays)
23
-
24
- # return all_bboxes, all_bboxes2, all_segments
25
-
26
- # def create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments):
27
- # solutions = []
28
- # img_id =1
29
- # box_id =1
30
- # cat_id =1
31
- # for image_url, bbox, bbox2, segmnt in zip(image_urls, all_bboxes, all_bboxes2, all_segments):
32
-
33
- # for subbox, subbox2, subsegmnt in zip(bbox, bbox2, segmnt):
34
-
35
- # w = subbox2[2]
36
- # h = subbox2[3]
37
- # area = w*h
38
-
39
- # flattened_segmnt = [item for sublist in subsegmnt for item in sublist]
40
-
41
- # obj = {"image_id":img_id, "image_url": image_url, "id":box_id, "area":area, "category_id":cat_id, "bbox": subbox, "segment":flattened_segmnt} # Create an object for each image
42
- # box_id +=1
43
- # solutions.append(obj)
44
- # img_id +=1
45
- # return solutions
46
-
47
- # def send_results_to_api(data, result_url):
48
- # # Example function to send results to an API
49
- # headers = {"Content-Type": "application/json"}
50
- # response = requests.post(result_url, json=data, headers=headers)
51
- # if response.status_code == 200:
52
- # return response.json() # Return any response from the API if needed
53
- # else:
54
- # return {"error": f"Failed to send results to API: {response.status_code}"}
55
-
56
- # def process_images(params):
57
- # # Parse the JSON string into a dictionary
58
- # params = json.loads(params)
59
-
60
- # image_urls = params.get("image_urls", [])
61
- # api = params.get("api", "")
62
- # job_id = params.get("job_id", "")
63
-
64
- # images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
65
-
66
- # all_bboxes, all_bboxes2, all_segments = detect_objects(images) # Perform object detection
67
- # solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments) # Create solutions with image URLs and bounding boxes
68
-
69
- # result_url = f"{api}/{job_id}"
70
- # # send_results_to_api(solutions, result_url)
71
-
72
- # return json.dumps({"solutions": solutions}, indent=4)
73
-
74
-
75
- # inputt = gr.Textbox(label="Parameters (JSON format)")
76
- # outputs = gr.JSON()
77
-
78
- # application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
79
- # application.launch()
80
-
81
-
82
-
83
  import gradio as gr
84
  from PIL import Image
85
  from ultralytics import YOLO
@@ -158,8 +76,8 @@ def process_images(params):
158
  solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments)
159
 
160
  result_url = f"{api}/{job_id}"
161
- # Uncomment the next line if you want to send results to an API
162
- # send_results_to_api(solutions, result_url)
163
 
164
  return json.dumps({"solutions": solutions}, indent=4)
165
 
@@ -169,3 +87,90 @@ outputs = gr.JSON()
169
 
170
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
171
  application.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  from ultralytics import YOLO
 
76
  solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments)
77
 
78
  result_url = f"{api}/{job_id}"
79
+
80
+ send_results_to_api(solutions, result_url)
81
 
82
  return json.dumps({"solutions": solutions}, indent=4)
83
 
 
87
 
88
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
89
  application.launch()
90
+
91
+
92
+
93
+
94
+
95
+ # import gradio as gr
96
+ # from PIL import Image
97
+ # from ultralytics import YOLO
98
+ # import requests
99
+ # import json
100
+
101
+ # model = YOLO("BP_Multiple_Objects_Complicated_v1.pt")
102
+
103
+ # def detect_objects(images):
104
+ # results = model(images)
105
+ # all_bboxes = []
106
+ # all_bboxes2 = []
107
+ # all_segments = []
108
+ # for result in results:
109
+ # boxes = result.boxes.xywhn.tolist()
110
+ # boxes2 = result.boxes.xywh.tolist()
111
+ # all_bboxes.append(boxes)
112
+ # all_bboxes2.append(boxes2)
113
+
114
+ # masks = result.masks.xyn
115
+ # sub_arrays = [arr.tolist() for arr in masks]
116
+ # all_segments.append(sub_arrays)
117
+
118
+ # return all_bboxes, all_bboxes2, all_segments
119
+
120
+ # def create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments):
121
+ # solutions = []
122
+ # img_id =1
123
+ # box_id =1
124
+ # cat_id =1
125
+ # for image_url, bbox, bbox2, segmnt in zip(image_urls, all_bboxes, all_bboxes2, all_segments):
126
+
127
+ # for subbox, subbox2, subsegmnt in zip(bbox, bbox2, segmnt):
128
+
129
+ # w = subbox2[2]
130
+ # h = subbox2[3]
131
+ # area = w*h
132
+
133
+ # flattened_segmnt = [item for sublist in subsegmnt for item in sublist]
134
+
135
+ # obj = {"image_id":img_id, "image_url": image_url, "id":box_id, "area":area, "category_id":cat_id, "bbox": subbox, "segment":flattened_segmnt} # Create an object for each image
136
+ # box_id +=1
137
+ # solutions.append(obj)
138
+ # img_id +=1
139
+ # return solutions
140
+
141
+ # def send_results_to_api(data, result_url):
142
+ # # Example function to send results to an API
143
+ # headers = {"Content-Type": "application/json"}
144
+ # response = requests.post(result_url, json=data, headers=headers)
145
+ # if response.status_code == 200:
146
+ # return response.json() # Return any response from the API if needed
147
+ # else:
148
+ # return {"error": f"Failed to send results to API: {response.status_code}"}
149
+
150
+ # def process_images(params):
151
+ # # Parse the JSON string into a dictionary
152
+ # params = json.loads(params)
153
+
154
+ # image_urls = params.get("image_urls", [])
155
+ # api = params.get("api", "")
156
+ # job_id = params.get("job_id", "")
157
+
158
+ # images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
159
+
160
+ # all_bboxes, all_bboxes2, all_segments = detect_objects(images) # Perform object detection
161
+ # solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments) # Create solutions with image URLs and bounding boxes
162
+
163
+ # result_url = f"{api}/{job_id}"
164
+ # # send_results_to_api(solutions, result_url)
165
+
166
+ # return json.dumps({"solutions": solutions}, indent=4)
167
+
168
+
169
+ # inputt = gr.Textbox(label="Parameters (JSON format)")
170
+ # outputs = gr.JSON()
171
+
172
+ # application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
173
+ # application.launch()
174
+
175
+
176
+