Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Vehicles_Classify_v1.pt +3 -0
- app.py +71 -0
- requirements.txt +1 -0
Vehicles_Classify_v1.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8764da9da87603c7dc2038295569a8a64504fc063a2e840bab121525e9d18a85
|
| 3 |
+
size 31705561
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("Vehicles_Classify_v1.pt")
|
| 11 |
+
|
| 12 |
+
def detect_objects(images):
|
| 13 |
+
results = model(images)
|
| 14 |
+
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" }
|
| 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 |
+
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 |
+
try:
|
| 54 |
+
images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
|
| 55 |
+
except Exception as e:
|
| 56 |
+
logging.error(f"Error loading images: {e}")
|
| 57 |
+
return {"error": f"Error loading images: {str(e)}"}
|
| 58 |
+
|
| 59 |
+
names = detect_objects(images) # Perform object detection
|
| 60 |
+
solutions = create_solutions(image_urls, names) # Create solutions with image URLs and bounding boxes
|
| 61 |
+
|
| 62 |
+
# result_url = f"{api}/{job_id}"
|
| 63 |
+
# send_results_to_api(solutions, result_url)
|
| 64 |
+
|
| 65 |
+
return json.dumps({"solutions": solutions})
|
| 66 |
+
|
| 67 |
+
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'img_url':['a.jpg','b.jpg']}")
|
| 68 |
+
outputs = gr.JSON()
|
| 69 |
+
|
| 70 |
+
application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Bottles Cans Classification with API Integration")
|
| 71 |
+
application.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|