hank1229 commited on
Commit
ca23413
·
verified ·
1 Parent(s): 763fff9

Update utils/detector.py

Browse files
Files changed (1) hide show
  1. utils/detector.py +9 -38
utils/detector.py CHANGED
@@ -1,59 +1,30 @@
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
 
 
 
 
 
1
  from PIL import Image
2
+ from io import BytesIO
3
  from models.vision import VisionModel
4
  from utils.bg_removal import remove_background
5
 
6
  vision = VisionModel()
 
 
 
 
 
 
 
 
 
 
7
 
8
  def detect_clothing(image_input, do_bg_remove: bool = False):
9
+ # 1) If filepath, load; else assume PIL.Image
10
  if isinstance(image_input, str):
11
+ img = Image.open(image_input).convert("RGB")
12
  else:
13
  img = image_input
14
 
15
+ # 2) Optional background removal
16
  if do_bg_remove:
17
  buf = BytesIO()
18
+ img.save(buf, format="JPEG")
19
  img_bytes = buf.getvalue()
20
  img = remove_background(img_bytes)
21
  else:
 
22
  img = img.convert("RGB")
23
 
24
  # 3) Run detection
25
+ detections = vision.detect(img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ if not detections:
28
+ detections = [{"label": "outfit", "score": 1.0, "box": []}]
29
 
30
+ return detections