Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- OAB_Blood_Grp_Detection_v1.pt +3 -0
- app.py +77 -0
- requirements.txt +1 -0
OAB_Blood_Grp_Detection_v1.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fa6442d64f1f5b3dc475df5f8e2dfc88ffe5a4e762e42ef0eb82d07e51741c84
|
3 |
+
size 52024961
|
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("OAB_Blood_Grp_Detection_v1.pt")
|
11 |
+
|
12 |
+
def detect_objects(images):
|
13 |
+
results = model(images, conf=0.3)
|
14 |
+
classes={ 0: "O +ve", 1: "A +ve", 2: "B +ve" }
|
15 |
+
names = []
|
16 |
+
|
17 |
+
for result in results:
|
18 |
+
if len(result.boxes) > 0:
|
19 |
+
class_index = result.boxes.cls[0].item()
|
20 |
+
class_name = classes[class_index]
|
21 |
+
names.append([class_name])
|
22 |
+
else:
|
23 |
+
names.append(['None'])
|
24 |
+
return names
|
25 |
+
|
26 |
+
def create_solutions(image_urls, names):
|
27 |
+
solutions = [] #list to store all the objects
|
28 |
+
|
29 |
+
for image_url, class_name in zip(image_urls, names):
|
30 |
+
obj = {"url": image_url, "answer": [class_name] }
|
31 |
+
solutions.append(obj)
|
32 |
+
return solutions
|
33 |
+
|
34 |
+
# def send_results_to_api(data, result_url):
|
35 |
+
# # Example function to send results to an API
|
36 |
+
# headers = {"Content-Type": "application/json"}
|
37 |
+
# response = requests.post(result_url, json=data, headers=headers)
|
38 |
+
# if response.status_code == 200:
|
39 |
+
# return response.json() # Return any response from the API if needed
|
40 |
+
# else:
|
41 |
+
# return {"error": f"Failed to send results to API: {response.status_code}"}
|
42 |
+
|
43 |
+
def process_images(params):
|
44 |
+
try:
|
45 |
+
params = json.loads(params)
|
46 |
+
except json.JSONDecodeError as e:
|
47 |
+
logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
|
48 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
49 |
+
|
50 |
+
image_urls = params.get("urls", [])
|
51 |
+
# api = params.get("api", "")
|
52 |
+
# job_id = params.get("job_id", "")
|
53 |
+
|
54 |
+
if not image_urls:
|
55 |
+
logging.error("Missing required parameters: 'urls'")
|
56 |
+
return {"error": "Missing required parameters: 'urls'"}
|
57 |
+
|
58 |
+
try:
|
59 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
60 |
+
except Exception as e:
|
61 |
+
logging.error(f"Error loading images: {e}")
|
62 |
+
return {"error": f"Error loading images: {str(e)}"}
|
63 |
+
|
64 |
+
names = detect_objects(images) # Perform object detection
|
65 |
+
solutions = create_solutions(image_urls, names) # Create solutions with image URLs and bounding boxes
|
66 |
+
|
67 |
+
# result_url = f"{api}/{job_id}"
|
68 |
+
# send_results_to_api(solutions, result_url)
|
69 |
+
|
70 |
+
return json.dumps({"solutions": solutions})
|
71 |
+
|
72 |
+
|
73 |
+
inputt = gr.Textbox(label="Parameters (JSON format) Eg. img_url:['','']")
|
74 |
+
outputs = gr.JSON()
|
75 |
+
|
76 |
+
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="OAB Blood Group Detection with API Integration")
|
77 |
+
application.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
ultralytics
|