Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("Vehicles_Classify_v1.pt")
|
| 8 |
+
|
| 9 |
+
def detect_objects(images):
|
| 10 |
+
results = model(images)
|
| 11 |
+
classes={ 0:"bus", 1:"family sedan", 2:"fire engine", 3:"heavy truck", 4:"jeep", 5:"mini bus", 6:"racing car", 7:"SUV", 8:"taxi", 9:"truck" }
|
| 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.post(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="Bottles Cans Classification with API Integration")
|
| 59 |
+
application.launch()
|