Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Car_Colours_Classify_v1.pt +3 -0
- app.py +75 -0
- requirements.txt +1 -0
Car_Colours_Classify_v1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:150f8d1b43a06bf22bf289dbae60660d0f91f44f3d42f7b4c15728d397a9968c
|
| 3 |
+
size 31719065
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("Car_Colours_Classify_v1.pt")
|
| 11 |
+
|
| 12 |
+
def detect_objects(images):
|
| 13 |
+
results = model(images)
|
| 14 |
+
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"}
|
| 15 |
+
names = []
|
| 16 |
+
for result in results:
|
| 17 |
+
probs = result.probs.top1
|
| 18 |
+
names.append(classes[probs])
|
| 19 |
+
return names
|
| 20 |
+
|
| 21 |
+
def create_solutions(image_urls, names):
|
| 22 |
+
solutions = []
|
| 23 |
+
for image_url, prediction in zip(image_urls, names):
|
| 24 |
+
obj = {"url": image_url, "answer": [prediction]}
|
| 25 |
+
solutions.append(obj)
|
| 26 |
+
return solutions
|
| 27 |
+
|
| 28 |
+
# def send_results_to_api(solutions, url):
|
| 29 |
+
# headers = {"Content-Type": "application/json"}
|
| 30 |
+
# try:
|
| 31 |
+
# logging.info(f"Sending results to API at {url} with data: {solutions}")
|
| 32 |
+
# # response = requests.patch(url, json = {"solutions":solutions}) # Set a timeout headers=headers,, timeout=60
|
| 33 |
+
# data = {"solutions":solutions}
|
| 34 |
+
# response = requests.patch(url, data=json.dumps(data), headers=headers)
|
| 35 |
+
# response.raise_for_status()
|
| 36 |
+
# logging.info(f"Response from API: {response.text}")
|
| 37 |
+
# return response.json()
|
| 38 |
+
# except requests.exceptions.RequestException as e:
|
| 39 |
+
# logging.error(f"Failed to send results to API: {e}")
|
| 40 |
+
# return {"error": f"Failed to send results to API: {str(e)}"}
|
| 41 |
+
|
| 42 |
+
def process_images(params):
|
| 43 |
+
try:
|
| 44 |
+
params = json.loads(params)
|
| 45 |
+
except json.JSONDecodeError as e:
|
| 46 |
+
logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
|
| 47 |
+
return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
|
| 48 |
+
|
| 49 |
+
image_urls = params.get("urls", [])
|
| 50 |
+
# api = params.get("api", "")
|
| 51 |
+
# job_id = params.get("job_id", "")
|
| 52 |
+
|
| 53 |
+
if not image_urls:
|
| 54 |
+
logging.error("Missing required parameters: 'urls'")
|
| 55 |
+
return {"error": "Missing required parameters: 'urls'"}
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls]
|
| 59 |
+
except Exception as e:
|
| 60 |
+
logging.error(f"Error loading images: {e}")
|
| 61 |
+
return {"error": f"Error loading images: {str(e)}"}
|
| 62 |
+
|
| 63 |
+
names = detect_objects(images)
|
| 64 |
+
solutions = create_solutions(image_urls, names)
|
| 65 |
+
|
| 66 |
+
# result_url = f"{api}/{job_id}"
|
| 67 |
+
# response = send_results_to_api(solutions, result_url)
|
| 68 |
+
|
| 69 |
+
return json.dumps({"solutions": solutions})
|
| 70 |
+
|
| 71 |
+
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'urls':['a.jpg','b.jpg']}")
|
| 72 |
+
outputs = gr.JSON()
|
| 73 |
+
|
| 74 |
+
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Car Colour Classification with API Integration")
|
| 75 |
+
application.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|