Spaces:
Running
Running
Upload 4 files
Browse files- utils/advisor.py +7 -0
- utils/bg_removal.py +20 -0
- utils/detector.py +59 -0
- utils/test_detector.py +49 -0
utils/advisor.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from models.llm import StyleSavvy
|
2 |
+
|
3 |
+
advisor = StyleSavvy()
|
4 |
+
|
5 |
+
def get_advice(items, body_type, face_shape, occasion):
|
6 |
+
return advisor.advise(items, body_type, face_shape, occasion)
|
7 |
+
|
utils/bg_removal.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os, requests
|
2 |
+
from io import BytesIO
|
3 |
+
from PIL import Image
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
API_KEY = os.getenv("REMOVE_BG_API_KEY")
|
9 |
+
ENDPOINT = "https://api.remove.bg/v1.0/removebg"
|
10 |
+
|
11 |
+
def remove_background(image_bytes: bytes) -> Image.Image:
|
12 |
+
resp = requests.post(
|
13 |
+
ENDPOINT,
|
14 |
+
files ={"image_file": ("image.jpg", image_bytes, "image/jpeg")},
|
15 |
+
data = {"size": "auto"},
|
16 |
+
headers = {"X-Api-Key": API_KEY},
|
17 |
+
)
|
18 |
+
resp.raise_for_status()
|
19 |
+
return Image.open(BytesIO(resp.content))
|
20 |
+
|
utils/detector.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from io import BytesIO
|
3 |
+
from PIL import Image
|
4 |
+
from models.vision import VisionModel
|
5 |
+
from utils.bg_removal import remove_background
|
6 |
+
|
7 |
+
vision = VisionModel()
|
8 |
+
FASHION_LABELS = {
|
9 |
+
"shirt", "t-shirt", "blouse", "tank top", "sweater", "hoodie", "jacket",
|
10 |
+
"coat", "overcoat", "raincoat", "windbreaker", "cardigan", "blazer",
|
11 |
+
"pants", "jeans", "shorts", "leggings", "tights", "skirt", "dress",
|
12 |
+
"suit", "jumpsuit", "romper", "vest", "sports bra", "tracksuit",
|
13 |
+
"belt", "tie", "scarf", "hat", "cap", "gloves", "socks",
|
14 |
+
"shoe", "sneakers", "boots", "sandals", "heels",
|
15 |
+
"watch", "necklace", "bracelet", "earrings", "ring",
|
16 |
+
"backpack", "handbag", "purse", "wallet"
|
17 |
+
}
|
18 |
+
|
19 |
+
def detect_clothing(image_input, do_bg_remove: bool = False):
|
20 |
+
# 1) Load into a PIL.Image if it's a filepath
|
21 |
+
if isinstance(image_input, str):
|
22 |
+
img = Image.open(image_input)
|
23 |
+
else:
|
24 |
+
img = image_input
|
25 |
+
|
26 |
+
# 2) Optionally remove background (works on bytes)
|
27 |
+
if do_bg_remove:
|
28 |
+
buf = BytesIO()
|
29 |
+
img.convert("RGB").save(buf, format="JPEG")
|
30 |
+
img_bytes = buf.getvalue()
|
31 |
+
img = remove_background(img_bytes)
|
32 |
+
else:
|
33 |
+
# ensure you drop any alpha channel
|
34 |
+
img = img.convert("RGB")
|
35 |
+
|
36 |
+
# 3) Run detection
|
37 |
+
raw_detections = vision.detect(img)
|
38 |
+
|
39 |
+
# 4) Filter and deduplicate
|
40 |
+
filtered = {}
|
41 |
+
for det in raw_detections:
|
42 |
+
label = det["label"].lower()
|
43 |
+
if label in FASHION_LABELS:
|
44 |
+
# Only keep the first or highest score if multiple detected
|
45 |
+
if label not in filtered or det["score"] > filtered[label]["score"]:
|
46 |
+
filtered[label] = {
|
47 |
+
"label": label,
|
48 |
+
"score": det["score"],
|
49 |
+
"box": det.get("box", [])
|
50 |
+
}
|
51 |
+
|
52 |
+
# 5) Return dict or fallback if empty
|
53 |
+
if not filtered:
|
54 |
+
return {"outfit": {"label": "outfit", "score": 1.0, "box": []}}
|
55 |
+
|
56 |
+
return filtered
|
57 |
+
|
58 |
+
|
59 |
+
|
utils/test_detector.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# test_detector.py
|
2 |
+
|
3 |
+
from detector import detect_clothing
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
import os
|
6 |
+
|
7 |
+
def visualize_and_print(image_path, do_bg_remove=False, output_dir="vis"):
|
8 |
+
# Ensure output folder exists
|
9 |
+
os.makedirs(output_dir, exist_ok=True)
|
10 |
+
|
11 |
+
img = Image.open(image_path).convert("RGB")
|
12 |
+
print(f"\n--- Testing {os.path.basename(image_path)} (bg_remove={do_bg_remove}) ---")
|
13 |
+
|
14 |
+
# Run your detector
|
15 |
+
dets = detect_clothing(img, do_bg_remove=do_bg_remove)
|
16 |
+
if not dets:
|
17 |
+
print("No detections!")
|
18 |
+
return
|
19 |
+
|
20 |
+
# Print raw detections
|
21 |
+
# Print raw detections
|
22 |
+
for i, d in enumerate(dets.values(), 1):
|
23 |
+
lbl = d["label"]
|
24 |
+
scr = d["score"]
|
25 |
+
box = d.get("box", [])
|
26 |
+
print(f" {i}. {lbl:12s} @ {scr:.2f} → {box}")
|
27 |
+
|
28 |
+
# Draw boxes
|
29 |
+
vis = img.copy()
|
30 |
+
draw = ImageDraw.Draw(vis)
|
31 |
+
for d in dets.values():
|
32 |
+
if d.get("box"):
|
33 |
+
x0, y0, x1, y1 = d["box"]
|
34 |
+
draw.rectangle([x0, y0, x1, y1], outline="red", width=2)
|
35 |
+
draw.text((x0, y0 - 10), f"{d['label']}:{d['score']:.2f}", fill="red")
|
36 |
+
# Save visualization
|
37 |
+
out_path = os.path.join(output_dir, os.path.basename(image_path))
|
38 |
+
vis.save(out_path)
|
39 |
+
print(f" Visualization saved to {out_path}")
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
# List your test images here
|
43 |
+
samples = [
|
44 |
+
"/Users/tanzimfarhan/Desktop/Python/Codes/SLU/CS5930/FinalProject/StyleSavvy/images/casual.jpg",
|
45 |
+
"/Users/tanzimfarhan/Desktop/Python/Codes/SLU/CS5930/FinalProject/StyleSavvy/images/WomenCasual.jpg",
|
46 |
+
]
|
47 |
+
for img_path in samples:
|
48 |
+
visualize_and_print(img_path, do_bg_remove=False)
|
49 |
+
# visualize_and_print(img_path, do_bg_remove=True)
|