Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ from PIL import Image
|
|
3 |
from ultralytics import YOLO
|
4 |
import requests
|
5 |
import json
|
|
|
|
|
|
|
6 |
|
7 |
model = YOLO("Bottles_Cans_Classify_v1.pt")
|
8 |
|
@@ -20,40 +23,50 @@ def create_solutions(image_urls, names):
|
|
20 |
for image_url, prediction in zip(image_urls, names):
|
21 |
prediction_list=[]
|
22 |
prediction_list.append(prediction)
|
23 |
-
obj = {"
|
24 |
solutions.append(obj)
|
25 |
return solutions
|
26 |
|
27 |
-
def send_results_to_api(data, result_url):
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
def process_images(params):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
-
|
|
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
names = detect_objects(images) # Perform object detection
|
47 |
solutions = create_solutions(image_urls, names) # Create solutions with image URLs and bounding boxes
|
48 |
|
49 |
-
result_url = f"{api}/{job_id}"
|
50 |
-
send_results_to_api(solutions, result_url)
|
51 |
-
|
52 |
-
return json.dumps({"solutions": solutions}, indent=4)
|
53 |
|
|
|
54 |
|
55 |
-
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'img_url':['a.jpg','b.jpg']
|
56 |
-
outputs = gr.JSON(
|
57 |
|
58 |
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Bottles Cans Classification with API Integration")
|
59 |
application.launch()
|
|
|
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("Bottles_Cans_Classify_v1.pt")
|
11 |
|
|
|
23 |
for image_url, prediction in zip(image_urls, names):
|
24 |
prediction_list=[]
|
25 |
prediction_list.append(prediction)
|
26 |
+
obj = {"url": image_url, "answer": prediction_list}
|
27 |
solutions.append(obj)
|
28 |
return solutions
|
29 |
|
30 |
+
# def send_results_to_api(data, result_url):
|
31 |
+
# # Example function to send results to an API
|
32 |
+
# headers = {"Content-Type": "application/json"}
|
33 |
+
# response = requests.post(result_url, json=data, headers=headers)
|
34 |
+
# if response.status_code == 200:
|
35 |
+
# return response.json() # Return any response from the API if needed
|
36 |
+
# else:
|
37 |
+
# return {"error": f"Failed to send results to API: {response.status_code}"}
|
38 |
|
39 |
def process_images(params):
|
40 |
+
try:
|
41 |
+
params = json.loads(params)
|
42 |
+
except json.JSONDecodeError as e:
|
43 |
+
logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
|
44 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
45 |
+
|
46 |
+
image_urls = params.get("urls", [])
|
47 |
+
# api = params.get("api", "")
|
48 |
+
# job_id = params.get("job_id", "")
|
49 |
|
50 |
+
if not image_urls:
|
51 |
+
logging.error("Missing required parameters: 'urls'")
|
52 |
+
return {"error": "Missing required parameters: 'urls'"}
|
53 |
|
54 |
+
try:
|
55 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
56 |
+
except Exception as e:
|
57 |
+
logging.error(f"Error loading images: {e}")
|
58 |
+
return {"error": f"Error loading images: {str(e)}"}
|
59 |
+
|
60 |
names = detect_objects(images) # Perform object detection
|
61 |
solutions = create_solutions(image_urls, names) # Create solutions with image URLs and bounding boxes
|
62 |
|
63 |
+
# result_url = f"{api}/{job_id}"
|
64 |
+
# send_results_to_api(solutions, result_url)
|
|
|
|
|
65 |
|
66 |
+
return json.dumps({"solutions": solutions})
|
67 |
|
68 |
+
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'img_url':['a.jpg','b.jpg']}")
|
69 |
+
outputs = gr.JSON()
|
70 |
|
71 |
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Bottles Cans Classification with API Integration")
|
72 |
application.launch()
|