janasumit2911 commited on
Commit
df81d76
·
verified ·
1 Parent(s): bd0dbc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -17
app.py CHANGED
@@ -1,3 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  from ultralytics import YOLO
@@ -21,52 +103,58 @@ def detect_objects(images):
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)
@@ -76,4 +164,4 @@ 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()
 
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
 
103
  sub_arrays = [arr.tolist() for arr in masks]
104
  all_segments.append(sub_arrays)
105
 
106
+ return all_bboxes, all_bboxes2, all_segments
107
 
108
  def create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments):
109
  solutions = []
110
+ img_id = 1
111
+ box_id = 1
112
+ cat_id = 1
113
  for image_url, bbox, bbox2, segmnt in zip(image_urls, all_bboxes, all_bboxes2, all_segments):
114
 
115
  for subbox, subbox2, subsegmnt in zip(bbox, bbox2, segmnt):
 
116
  w = subbox2[2]
117
  h = subbox2[3]
118
+ area = w * h
119
 
120
  flattened_segmnt = [item for sublist in subsegmnt for item in sublist]
121
 
122
+ obj = {
123
+ "image_id": img_id,
124
+ "image_url": image_url,
125
+ "id": box_id,
126
+ "area": area,
127
+ "category_id": cat_id,
128
+ "bbox": subbox,
129
+ "segment": flattened_segmnt
130
+ }
131
+ solutions.append(obj)
132
+ box_id += 1
133
+ img_id += 1
134
  return solutions
135
 
136
  def send_results_to_api(data, result_url):
 
137
  headers = {"Content-Type": "application/json"}
138
  response = requests.post(result_url, json=data, headers=headers)
139
  if response.status_code == 200:
140
+ return response.json()
141
  else:
142
  return {"error": f"Failed to send results to API: {response.status_code}"}
143
 
144
  def process_images(params):
 
145
  params = json.loads(params)
146
 
147
  image_urls = params.get("image_urls", [])
148
  api = params.get("api", "")
149
  job_id = params.get("job_id", "")
150
 
151
+ images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls]
152
 
153
+ all_bboxes, all_bboxes2, all_segments = detect_objects(images)
154
+ solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments)
155
 
156
  result_url = f"{api}/{job_id}"
157
+ # Uncomment the next line if you want to send results to an API
158
  # send_results_to_api(solutions, result_url)
159
 
160
  return json.dumps({"solutions": solutions}, indent=4)
 
164
  outputs = gr.JSON()
165
 
166
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
167
+ application.launch()