Spaces:
Build error
Build error
Not patching of data, only returning solutions
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("Single_Object_BB_Detection_v1.pt")
|
| 8 |
|
|
@@ -41,36 +42,45 @@ def create_solutions(image_urls, all_bboxes, all_bboxes2):
|
|
| 41 |
img_id += 1
|
| 42 |
return solutions
|
| 43 |
|
| 44 |
-
def send_results_to_api(data, result_url):
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
def process_images(params):
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
image_urls = params.get("image_urls", [])
|
| 58 |
-
api = params.get("api", "")
|
| 59 |
-
job_id = params.get("job_id", "")
|
| 60 |
-
|
| 61 |
-
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
| 62 |
-
|
| 63 |
all_bboxes, all_bboxes2 = detect_objects(images) # Perform object detection
|
| 64 |
solutions = create_solutions(image_urls, all_bboxes, all_bboxes2) # Create solutions with image URLs and bounding boxes
|
| 65 |
|
| 66 |
-
result_url = f"{api}/{job_id}"
|
| 67 |
-
send_results_to_api(solutions, result_url)
|
| 68 |
-
|
| 69 |
-
return json.dumps({"solutions": solutions}
|
| 70 |
|
| 71 |
|
| 72 |
inputt = gr.Textbox(label="Parameters (JSON format)")
|
| 73 |
-
|
| 74 |
|
| 75 |
-
application = gr.Interface(fn=process_images, inputs=inputt, outputs=
|
| 76 |
-
application.launch(
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import requests
|
| 5 |
import json
|
| 6 |
+
import logging
|
| 7 |
|
| 8 |
model = YOLO("Single_Object_BB_Detection_v1.pt")
|
| 9 |
|
|
|
|
| 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 |
+
image_urls = params.get("urls", [])
|
| 61 |
+
# api = params.get("api", "")
|
| 62 |
+
# job_id = params.get("job_id", "")
|
| 63 |
+
|
| 64 |
+
if not image_urls:
|
| 65 |
+
logging.error("Missing required parameters: 'urls'")
|
| 66 |
+
return {"error": "Missing required parameters: 'urls'"}
|
| 67 |
+
try:
|
| 68 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logging.error(f"Error loading images: {e}")
|
| 71 |
+
return {"error": f"Error loading images: {str(e)}"}
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
all_bboxes, all_bboxes2 = detect_objects(images) # Perform object detection
|
| 74 |
solutions = create_solutions(image_urls, all_bboxes, all_bboxes2) # Create solutions with image URLs and bounding boxes
|
| 75 |
|
| 76 |
+
# result_url = f"{api}/{job_id}"
|
| 77 |
+
# send_results_to_api(solutions, result_url)
|
| 78 |
+
|
| 79 |
+
return json.dumps({"solutions": solutions})
|
| 80 |
|
| 81 |
|
| 82 |
inputt = gr.Textbox(label="Parameters (JSON format)")
|
| 83 |
+
outputt = gr.JSON()
|
| 84 |
|
| 85 |
+
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputt, title="Single Object Detection with API Integration")
|
| 86 |
+
application.launch()
|