D3V1L1810 commited on
Commit
d6fbe5e
·
verified ·
1 Parent(s): 49b2bc4

Upload 3 files

Browse files
Files changed (3) hide show
  1. Multiple_Object_BB_Detection_v1.pt +3 -0
  2. app.py +87 -0
  3. requirements.txt +1 -0
Multiple_Object_BB_Detection_v1.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3790075f525122a276322023210320447f931f314d9a4e60279a55e08c698829
3
+ size 52033665
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("Multiple_Object_BB_Detection_v1.pt")
11
+
12
+ def detect_objects(images):
13
+ results = model(images)
14
+ all_bboxes = []
15
+ all_bboxes2 = []
16
+ for result in results:
17
+ boxes = result.boxes.xywhn.tolist()
18
+ boxes2 = result.boxes.xywh.tolist()
19
+ all_bboxes.append(boxes)
20
+ all_bboxes2.append(boxes2)
21
+ return all_bboxes, all_bboxes2
22
+
23
+ def create_solutions(image_urls, all_bboxes, all_bboxes2):
24
+ solutions = []
25
+ img_id =1
26
+ box_id =1
27
+ cat_id =1
28
+ for image_url, bbox, bbox2 in zip(image_urls, all_bboxes, all_bboxes2): # Loop through each image ID and its corresponding prediction
29
+
30
+ for subbox, subbox2 in zip(bbox, bbox2):
31
+
32
+ w = subbox2[2]
33
+ h = subbox2[3]
34
+ area = w*h
35
+ seg=[[]]
36
+ ans = {"segmentation":seg, "area":area, 'iscrowd':0, "image_id":img_id, "bbox": subbox, "category_id":cat_id, "id":box_id }
37
+ ansx=[]
38
+ ansx.append(ans)
39
+ obj = {"url": image_url, 'answer':ansx}
40
+ box_id +=1
41
+ solutions.append(obj)
42
+ img_id +=1
43
+ return solutions
44
+
45
+ # def send_results_to_api(data, result_url):
46
+ # # Example function to send results to an API
47
+ # headers = {"Content-Type": "application/json"}
48
+ # response = requests.post(result_url, json=data, headers=headers)
49
+ # if response.status_code == 200:
50
+ # return response.json() # Return any response from the API if needed
51
+ # else:
52
+ # return {"error": f"Failed to send results to API: {response.status_code}"}
53
+
54
+ def process_images(params):
55
+ try:
56
+ params = json.loads(params)
57
+ except json.JSONDecodeError as e:
58
+ logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
59
+ return {"error":f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
60
+
61
+ image_urls = params.get("urls", [])
62
+ # api = params.get("api", "")
63
+ # job_id = params.get("job_id", "")
64
+
65
+ if not image_urls:
66
+ logging.error("Missing required parameters: 'urls'")
67
+ return {"error": "Missing required parameters: 'urls'"}
68
+ try:
69
+ images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
70
+ except Exception as e:
71
+ logging.error(f"Error loading images: {e}")
72
+ return {"error": f"Error loading images: {str(e)}"}
73
+
74
+ all_bboxes, all_bboxes2 = detect_objects(images) # Perform object detection
75
+ solutions = create_solutions(image_urls, all_bboxes, all_bboxes2) # Create solutions with image URLs and bounding boxes
76
+
77
+ # result_url = f"{api}/{job_id}"
78
+ # send_results_to_api(solutions, result_url)
79
+
80
+ return json.dumps({"solutions": solutions})
81
+
82
+
83
+ inputt = gr.Textbox(label="Parameters (JSON format)")
84
+ outputs = gr.JSON()
85
+
86
+ application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Detection with API Integration")
87
+ application.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ultralytics