janasumit2911 commited on
Commit
ef5b9ad
·
verified ·
1 Parent(s): 2820674

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("Multiple_Object_BB_Detection_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 Detection with API Integration")
79
+ application.launch()