Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import requests
|
| 5 |
import json
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
model = YOLO("Car_Colours_Classify_v1.pt")
|
| 8 |
|
| 9 |
def detect_objects(images):
|
| 10 |
results = model(images)
|
| 11 |
-
classes={0:"beige", 1:"black", 2:"blue", 3:"brown", 4:"gold", 5:"green", 6:"grey", 7:"orange", 8:"pink", 9:"purple", 10:"red", 11:"silver", 12:"tan", 13:"white", 14:"yellow"
|
| 12 |
-
names=[]
|
| 13 |
-
for result in results:
|
| 14 |
-
probs = result.probs.top1
|
| 15 |
names.append(classes[probs])
|
| 16 |
return names
|
| 17 |
|
| 18 |
def create_solutions(image_urls, names):
|
| 19 |
-
solutions = []
|
| 20 |
-
for image_url, prediction in zip(image_urls, names):
|
| 21 |
-
prediction_list=[]
|
| 22 |
prediction_list.append(prediction)
|
| 23 |
-
obj = {"qcUserId": "", "image": image_url, "answer": prediction_list}
|
| 24 |
-
solutions.append(obj)
|
| 25 |
return solutions
|
| 26 |
|
| 27 |
def send_results_to_api(data, result_url):
|
| 28 |
-
# Example function to send results to an API
|
| 29 |
headers = {"Content-Type": "application/json"}
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def process_images(params):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
image_urls = params.get("image_urls", [])
|
| 41 |
api = params.get("api", "")
|
| 42 |
job_id = params.get("job_id", "")
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
|
|
|
| 54 |
|
| 55 |
-
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'img_url':['a.jpg','b.jpg'], 'api':'
|
| 56 |
outputs = gr.JSON()
|
| 57 |
|
| 58 |
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Car Colour Classification with API Integration")
|
| 59 |
-
application.launch(share=True)
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# from PIL import Image
|
| 3 |
+
# from ultralytics import YOLO
|
| 4 |
+
# import requests
|
| 5 |
+
# import json
|
| 6 |
+
|
| 7 |
+
# model = YOLO("Car_Colours_Classify_v1.pt")
|
| 8 |
+
|
| 9 |
+
# def detect_objects(images):
|
| 10 |
+
# results = model(images)
|
| 11 |
+
# classes={0:"beige", 1:"black", 2:"blue", 3:"brown", 4:"gold", 5:"green", 6:"grey", 7:"orange", 8:"pink", 9:"purple", 10:"red", 11:"silver", 12:"tan", 13:"white", 14:"yellow" }
|
| 12 |
+
# names=[]
|
| 13 |
+
# for result in results:
|
| 14 |
+
# probs = result.probs.top1
|
| 15 |
+
# names.append(classes[probs])
|
| 16 |
+
# return names
|
| 17 |
+
|
| 18 |
+
# def create_solutions(image_urls, names):
|
| 19 |
+
# solutions = []
|
| 20 |
+
# for image_url, prediction in zip(image_urls, names):
|
| 21 |
+
# prediction_list=[]
|
| 22 |
+
# prediction_list.append(prediction)
|
| 23 |
+
# obj = {"qcUserId": "", "image": image_url, "answer": prediction_list}
|
| 24 |
+
# solutions.append(obj)
|
| 25 |
+
# return solutions
|
| 26 |
+
|
| 27 |
+
# def send_results_to_api(data, result_url):
|
| 28 |
+
# # Example function to send results to an API
|
| 29 |
+
# headers = {"Content-Type": "application/json"}
|
| 30 |
+
# response = requests.patch(result_url, json=data, headers=headers)
|
| 31 |
+
# if response.status_code == 200:
|
| 32 |
+
# return response.json() # Return any response from the API if needed
|
| 33 |
+
# else:
|
| 34 |
+
# return {"error": f"Failed to send results to API: {response.status_code}"}
|
| 35 |
+
|
| 36 |
+
# def process_images(params):
|
| 37 |
+
# # Parse the JSON string into a dictionary
|
| 38 |
+
# params = json.loads(params)
|
| 39 |
+
|
| 40 |
+
# image_urls = params.get("image_urls", [])
|
| 41 |
+
# api = params.get("api", "")
|
| 42 |
+
# job_id = params.get("job_id", "")
|
| 43 |
+
|
| 44 |
+
# images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
| 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'], 'api':'abc', 'job_id':'123'} ")
|
| 56 |
+
# outputs = gr.JSON()
|
| 57 |
+
|
| 58 |
+
# application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Car Colour Classification with API Integration")
|
| 59 |
+
# application.launch(share=True)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
#modified code for patch solution into api
|
| 63 |
import gradio as gr
|
| 64 |
from PIL import Image
|
| 65 |
from ultralytics import YOLO
|
| 66 |
import requests
|
| 67 |
import json
|
| 68 |
+
import logging
|
| 69 |
+
|
| 70 |
+
logging.basicConfig(level=logging.INFO)
|
| 71 |
|
| 72 |
model = YOLO("Car_Colours_Classify_v1.pt")
|
| 73 |
|
| 74 |
def detect_objects(images):
|
| 75 |
results = model(images)
|
| 76 |
+
classes = {0: "beige", 1: "black", 2: "blue", 3: "brown", 4: "gold", 5: "green", 6: "grey", 7: "orange", 8: "pink", 9: "purple", 10: "red", 11: "silver", 12: "tan", 13: "white", 14: "yellow"}
|
| 77 |
+
names = []
|
| 78 |
+
for result in results:
|
| 79 |
+
probs = result.probs.top1
|
| 80 |
names.append(classes[probs])
|
| 81 |
return names
|
| 82 |
|
| 83 |
def create_solutions(image_urls, names):
|
| 84 |
+
solutions = []
|
| 85 |
+
for image_url, prediction in zip(image_urls, names):
|
| 86 |
+
prediction_list = []
|
| 87 |
prediction_list.append(prediction)
|
| 88 |
+
obj = {"qcUserId": "", "image": image_url, "answer": prediction_list}
|
| 89 |
+
solutions.append(obj)
|
| 90 |
return solutions
|
| 91 |
|
| 92 |
def send_results_to_api(data, result_url):
|
|
|
|
| 93 |
headers = {"Content-Type": "application/json"}
|
| 94 |
+
try:
|
| 95 |
+
response = requests.patch(result_url, json=data, headers=headers)
|
| 96 |
+
response.raise_for_status()
|
| 97 |
+
return response.json()
|
| 98 |
+
except requests.exceptions.RequestException as e:
|
| 99 |
+
logging.error(f"Failed to send results to API: {e}")
|
| 100 |
+
return {"error": f"Failed to send results to API: {str(e)}"}
|
| 101 |
|
| 102 |
def process_images(params):
|
| 103 |
+
try:
|
| 104 |
+
params = json.loads(params)
|
| 105 |
+
except json.JSONDecodeError as e:
|
| 106 |
+
logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
|
| 107 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
| 108 |
+
|
| 109 |
image_urls = params.get("image_urls", [])
|
| 110 |
api = params.get("api", "")
|
| 111 |
job_id = params.get("job_id", "")
|
| 112 |
|
| 113 |
+
if not image_urls or not api or not job_id:
|
| 114 |
+
logging.error("Missing required parameters: 'image_urls', 'api', or 'job_id'")
|
| 115 |
+
return {"error": "Missing required parameters: 'image_urls', 'api', or 'job_id'"}
|
| 116 |
|
| 117 |
+
try:
|
| 118 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls]
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logging.error(f"Error loading images: {e}")
|
| 121 |
+
return {"error": f"Error loading images: {str(e)}"}
|
| 122 |
|
| 123 |
+
names = detect_objects(images)
|
| 124 |
+
solutions = create_solutions(image_urls, names)
|
| 125 |
|
| 126 |
+
result_url = f"{api}/{job_id}"
|
| 127 |
+
response = send_results_to_api(solutions, result_url)
|
| 128 |
|
| 129 |
+
return json.dumps({"solutions": solutions, "api_response": response}, indent=4)
|
| 130 |
|
| 131 |
+
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'img_url':['a.jpg','b.jpg'], 'api':'http://localhost:9000/api/v1/normalUpdateHandler', 'job_id':'123'}")
|
| 132 |
outputs = gr.JSON()
|
| 133 |
|
| 134 |
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Car Colour Classification with API Integration")
|
| 135 |
+
application.launch(share=True)
|