Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ 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 |
|
|
@@ -55,37 +56,51 @@ def create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments):
|
|
| 55 |
img_id += 1
|
| 56 |
return solutions
|
| 57 |
|
| 58 |
-
def send_results_to_api(data, result_url):
|
| 59 |
-
headers = {"Content-Type": "application/json"}
|
| 60 |
-
response = requests.post(result_url, json=data, headers=headers)
|
| 61 |
-
if response.status_code == 200:
|
| 62 |
-
return response.json()
|
| 63 |
-
else:
|
| 64 |
-
return {"error": f"Failed to send results to API: {response.status_code}"}
|
| 65 |
|
| 66 |
-
def
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls]
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
all_bboxes, all_bboxes2, all_segments = detect_objects(images)
|
| 76 |
solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments)
|
| 77 |
|
| 78 |
-
result_url = f"{api}/{job_id}"
|
| 79 |
-
send_results_to_api(solutions, result_url)
|
| 80 |
|
| 81 |
-
return json.dumps({"solutions": solutions}
|
| 82 |
|
| 83 |
|
| 84 |
inputt = gr.Textbox(label="Parameters (JSON format)")
|
| 85 |
outputs = gr.JSON()
|
| 86 |
|
| 87 |
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
|
| 88 |
-
application.launch(
|
| 89 |
|
| 90 |
|
| 91 |
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
+
import logging
|
| 7 |
|
| 8 |
model = YOLO("BP_Multiple_Objects_Complicated_v1.pt")
|
| 9 |
|
|
|
|
| 56 |
img_id += 1
|
| 57 |
return solutions
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# def send_results_to_api(data, result_url):
|
| 61 |
+
# headers = {"Content-Type": "application/json"}
|
| 62 |
+
# response = requests.post(result_url, json=data, headers=headers)
|
| 63 |
+
# if response.status_code == 200:
|
| 64 |
+
# return response.json()
|
| 65 |
+
# else:
|
| 66 |
+
# return {"error": f"Failed to send results to API: {response.status_code}"}
|
| 67 |
|
|
|
|
| 68 |
|
| 69 |
+
def process_images(params):
|
| 70 |
+
try:
|
| 71 |
+
params = json.loads(params)
|
| 72 |
+
except json.JSONDecodeError as e:
|
| 73 |
+
logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
|
| 74 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
| 75 |
+
|
| 76 |
+
image_urls = params.get("urls", [])
|
| 77 |
+
# api = params.get("api", "")
|
| 78 |
+
# job_id = params.get("job_id", "")
|
| 79 |
+
|
| 80 |
+
if not image_urls:
|
| 81 |
+
logging.error("Missing required parameters: 'urls'")
|
| 82 |
+
return {"error": "Missing required parameters: 'urls'"}
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls]
|
| 86 |
+
except Exception as e:
|
| 87 |
+
logging.error(f"Error loading images: {e}")
|
| 88 |
+
return {"error": f"Error loading images: {str(e)}"}
|
| 89 |
+
|
| 90 |
all_bboxes, all_bboxes2, all_segments = detect_objects(images)
|
| 91 |
solutions = create_solutions(image_urls, all_bboxes, all_bboxes2, all_segments)
|
| 92 |
|
| 93 |
+
# result_url = f"{api}/{job_id}"
|
| 94 |
+
# send_results_to_api(solutions, result_url)
|
| 95 |
|
| 96 |
+
return json.dumps({"solutions": solutions})
|
| 97 |
|
| 98 |
|
| 99 |
inputt = gr.Textbox(label="Parameters (JSON format)")
|
| 100 |
outputs = gr.JSON()
|
| 101 |
|
| 102 |
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Segmentation with API Integration")
|
| 103 |
+
application.launch()
|
| 104 |
|
| 105 |
|
| 106 |
|