Spaces:
Sleeping
Sleeping
Commit ·
1ab9732
1
Parent(s): fe34a0d
Add initial implementation of Fashion Item Detector with Gradio interface
Browse files- README.md +3 -1
- app.py +68 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -11,4 +11,6 @@ license: unknown
|
|
| 11 |
short_description: upload image and get outfit price tags
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
| 11 |
short_description: upload image and get outfit price tags
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# Fashion Item Detector
|
| 15 |
+
Upload an image to detect clothing, accessories, and details using the YOLOS-Fashionpedia model.
|
| 16 |
+
Built with Gradio and Transformers.
|
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForObjectDetection
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the model and processor
|
| 7 |
+
model_name = "valentinafeve/yolos-fashionpedia"
|
| 8 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForObjectDetection.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Define the 46 fashion categories (id2label mapping)
|
| 12 |
+
CATS = [
|
| 13 |
+
'shirt, blouse', 'top, t-shirt, sweatshirt', 'sweater', 'cardigan', 'jacket', 'vest', 'pants', 'shorts', 'skirt',
|
| 14 |
+
'coat', 'dress', 'jumpsuit', 'cape', 'glasses', 'hat', 'headband, head covering, hair accessory', 'tie', 'glove',
|
| 15 |
+
'watch', 'belt', 'leg warmer', 'tights, stockings', 'sock', 'shoe', 'bag, wallet', 'scarf', 'umbrella', 'hood',
|
| 16 |
+
'collar', 'lapel', 'epaulette', 'sleeve', 'pocket', 'neckline', 'buckle', 'zipper', 'applique', 'bead', 'bow',
|
| 17 |
+
'flower', 'fringe', 'ribbon', 'rivet', 'ruffle', 'sequin', 'tassel'
|
| 18 |
+
]
|
| 19 |
+
model.config.id2label = {i: label for i, label in enumerate(CATS)}
|
| 20 |
+
model.config.label2id = {label: i for i, label in model.config.id2label.items()}
|
| 21 |
+
|
| 22 |
+
def detect_fashion_items(image):
|
| 23 |
+
# Prepare inputs
|
| 24 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 25 |
+
|
| 26 |
+
# Move to GPU if available
|
| 27 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 28 |
+
model.to(device)
|
| 29 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 30 |
+
|
| 31 |
+
# Run inference
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model(**inputs)
|
| 34 |
+
|
| 35 |
+
# Post-process
|
| 36 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 37 |
+
results = processor.post_process_object_detection(
|
| 38 |
+
outputs, threshold=0.5, target_sizes=target_sizes
|
| 39 |
+
)[0]
|
| 40 |
+
|
| 41 |
+
# Draw on the image
|
| 42 |
+
draw = ImageDraw.Draw(image)
|
| 43 |
+
try:
|
| 44 |
+
font = ImageFont.truetype("DejaVuSans.ttf", 200) # Colab-compatible font
|
| 45 |
+
except:
|
| 46 |
+
font = ImageFont.load_default() # Fallback if font fails
|
| 47 |
+
|
| 48 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 49 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 50 |
+
# Draw bounding box (blue rectangle)
|
| 51 |
+
draw.rectangle(box, outline="blue", width=3)
|
| 52 |
+
# Draw label with confidence
|
| 53 |
+
label_text = f"{model.config.id2label[label.item()]}: {score:.2f}"
|
| 54 |
+
draw.text((box[0], box[1] - 20), label_text, fill="blue", font=font)
|
| 55 |
+
|
| 56 |
+
return image
|
| 57 |
+
|
| 58 |
+
# Gradio interface
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=detect_fashion_items,
|
| 61 |
+
inputs=gr.Image(type="pil", label="Upload a fashion image"),
|
| 62 |
+
outputs=gr.Image(type="pil", label="Detected fashion items"),
|
| 63 |
+
title="Fashion Item Detector",
|
| 64 |
+
description="Upload an image to detect clothing, accessories, and details using YOLOS-Fashionpedia."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Launch Gradio (Colab-compatible)
|
| 68 |
+
iface.launch(share=True) # Creates a public URL
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
pillow
|