Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Single_Object_BB_Detection_v1.pt +3 -0
- app.py +90 -0
- requirements.txt +4 -0
Single_Object_BB_Detection_v1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:82d103dde7c5c1c3d7e164ec696b8778d7478d099225a2a692ebbc938e43ff53
|
| 3 |
+
size 52030465
|
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("Single_Object_BB_Detection_v1.pt")
|
| 11 |
+
|
| 12 |
+
def detect_objects(images):
|
| 13 |
+
results = model(images, max_det=1)
|
| 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 |
+
category_id = 1
|
| 28 |
+
for image_url, bboxes, bboxes2 in zip(image_urls, all_bboxes, all_bboxes2):
|
| 29 |
+
for box, box2 in zip(bboxes, bboxes2):
|
| 30 |
+
if isinstance(box2[0], list):
|
| 31 |
+
w = box2[0][2]
|
| 32 |
+
h = box2[0][3]
|
| 33 |
+
else:
|
| 34 |
+
w = box2[2]
|
| 35 |
+
h = box2[3]
|
| 36 |
+
|
| 37 |
+
area = w * h
|
| 38 |
+
seg = [[]]
|
| 39 |
+
|
| 40 |
+
ans = {"segmentation": seg,"area": area,"iscrowd": 0,"image_id": img_id,"bbox": box,"category_id": category_id,"id": box_id}
|
| 41 |
+
obj = {"url": image_url, "answer": [ans]}
|
| 42 |
+
solutions.append(obj)
|
| 43 |
+
box_id += 1
|
| 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 |
+
try:
|
| 58 |
+
params = json.loads(params)
|
| 59 |
+
except json.JSONDecodeError as e:
|
| 60 |
+
logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
|
| 61 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
| 62 |
+
|
| 63 |
+
image_urls = params.get("urls", [])
|
| 64 |
+
# api = params.get("api", "")
|
| 65 |
+
# job_id = params.get("job_id", "")
|
| 66 |
+
|
| 67 |
+
if not image_urls:
|
| 68 |
+
logging.error("Missing required parameters: 'urls'")
|
| 69 |
+
return {"error": "Missing required parameters: 'urls'"}
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
| 73 |
+
except Exception as e:
|
| 74 |
+
logging.error(f"Error loading images: {e}")
|
| 75 |
+
return {"error": f"Error loading images: {str(e)}"}
|
| 76 |
+
|
| 77 |
+
all_bboxes, all_bboxes2 = detect_objects(images) # Perform object detection
|
| 78 |
+
solutions = create_solutions(image_urls, all_bboxes, all_bboxes2) # Create solutions with image URLs and bounding boxes
|
| 79 |
+
|
| 80 |
+
# result_url = f"{api}/{job_id}"
|
| 81 |
+
# send_results_to_api(solutions, result_url)
|
| 82 |
+
|
| 83 |
+
return json.dumps({"solutions": solutions})
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
inputt = gr.Textbox(label="Parameters (JSON format)")
|
| 87 |
+
outputt = gr.JSON()
|
| 88 |
+
|
| 89 |
+
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputt, title="Single Object Detection with API Integration")
|
| 90 |
+
application.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
gradio
|
| 3 |
+
pillow
|
| 4 |
+
requests
|