abduqayum.rasulmuhamedov commited on
Commit ·
f40939d
1
Parent(s): 34f1422
done
Browse files- .gitignore +2 -0
- app.py +60 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
.DS_Store
|
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
from transformers import AutoImageProcessor, ViTForImageClassification
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
processor = AutoImageProcessor.from_pretrained("Abduqayum/Vehicle-Color-Recognition", token=HF_TOKEN)
|
| 14 |
+
model = ViTForImageClassification.from_pretrained("Abduqayum/Vehicle-Color-Recognition", token=HF_TOKEN)
|
| 15 |
+
model.eval()
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
model.to(device)
|
| 18 |
+
|
| 19 |
+
detection_model = YOLO("yolov8n.pt")
|
| 20 |
+
|
| 21 |
+
CLASSES_ID = [2, 3, 5, 7]
|
| 22 |
+
|
| 23 |
+
def classify_image(image):
|
| 24 |
+
image = image.convert("RGB")
|
| 25 |
+
|
| 26 |
+
results = detection_model(image, classes=CLASSES_ID, conf=0.5, verbose=False)
|
| 27 |
+
detections = results[0].boxes.data.cpu().numpy()
|
| 28 |
+
|
| 29 |
+
if len(detections) == 0:
|
| 30 |
+
return "No vehicle detected (car, bus, or truck).", image
|
| 31 |
+
|
| 32 |
+
largest = max(detections, key=lambda det: (det[2] - det[0]) * (det[3] - det[1]))
|
| 33 |
+
x1, y1, x2, y2, conf, cls_id = largest
|
| 34 |
+
x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
|
| 35 |
+
|
| 36 |
+
cropped = image.crop((x1, y1, x2, y2))
|
| 37 |
+
|
| 38 |
+
inputs = processor(cropped, return_tensors="pt")
|
| 39 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = model(**inputs)
|
| 42 |
+
|
| 43 |
+
logits = outputs.logits
|
| 44 |
+
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 45 |
+
pred_idx = probs.argmax(dim=-1).item()
|
| 46 |
+
label = model.config.id2label[pred_idx]
|
| 47 |
+
confidence = probs[0, pred_idx].item()
|
| 48 |
+
|
| 49 |
+
draw = ImageDraw.Draw(image)
|
| 50 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=4)
|
| 51 |
+
draw.text((x1, y1 - 10), f"{label} ({confidence:.2%})", fill="red")
|
| 52 |
+
|
| 53 |
+
return f"Prediction: {label} (confidence: {confidence:.2%})", image
|
| 54 |
+
|
| 55 |
+
gr.Interface(
|
| 56 |
+
fn=classify_image,
|
| 57 |
+
inputs=gr.Image(type="pil"),
|
| 58 |
+
outputs=["text", "image"],
|
| 59 |
+
title="YOLO Vehicle Detector + ViT Classifier to identify color of vehicles"
|
| 60 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.3.33
|
| 2 |
+
transformers==4.49.0
|
| 3 |
+
pillow==10.4.0
|
| 4 |
+
gradio
|
| 5 |
+
python-dotenv
|