D3V1L1810 commited on
Commit
448f141
·
verified ·
1 Parent(s): f8813b6

Upload 3 files

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