FrezzyI commited on
Commit
96ff886
·
verified ·
1 Parent(s): ba8bd02

Upload 2 files

Browse files
fashionclip_segmented_app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # fashionclip_segmented_app.py
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import torch
5
+ import numpy as np
6
+ from rembg import remove
7
+ from transformers import CLIPProcessor, CLIPModel
8
+
9
+ # Lade das Modell und den Prozessor
10
+ model = CLIPModel.from_pretrained("patrickjohncyh/fashion-clip")
11
+ processor = CLIPProcessor.from_pretrained("patrickjohncyh/fashion-clip")
12
+
13
+ # Prompts für jede Merkmalsgruppe
14
+ category_prompts = ["a t-shirt", "a long-sleeved shirt", "a hoodie", "a sweatshirt", "a pullover", "a tank top"]
15
+ color_prompts = ["a red garment", "a blue garment", "a black garment", "a white garment", "a green garment", "a yellow garment", "a gray garment", "a brown garment", "a pink garment", "a purple garment"]
16
+ pattern_prompts = ["a plain shirt", "a striped shirt", "a floral shirt", "a checked shirt", "a dotted shirt", "an abstract patterned shirt"]
17
+ fit_prompts = ["a slim fit shirt", "an oversized top", "a regular fit shirt", "a cropped shirt", "a shirt with a crew neck", "a shirt with a v-neck", "a shirt with a round neckline"]
18
+
19
+ # Funktion zur Hintergrundentfernung
20
+ def remove_background(image: Image.Image) -> Image.Image:
21
+ image_np = np.array(image)
22
+ image_no_bg_np = remove(image_np)
23
+ return Image.fromarray(image_no_bg_np)
24
+
25
+ # Hilfsfunktion: finde das passendste Prompt für eine Gruppe
26
+ def predict_best_prompt(image, prompts):
27
+ print(f"[DEBUG] Image type: {type(image)}, Prompt count: {len(prompts)}")
28
+ image_clean = remove_background(image)
29
+ inputs = processor(text=prompts, images=[image_clean], return_tensors="pt", padding=True)
30
+ with torch.no_grad():
31
+ outputs = model(**inputs)
32
+ logits_per_image = outputs.logits_per_image
33
+ probs = logits_per_image.softmax(dim=1).squeeze().tolist()
34
+ best_idx = torch.tensor(probs).argmax().item()
35
+ return prompts[best_idx], probs[best_idx]
36
+
37
+ # Hauptfunktion für die App
38
+ def analyze_image(image):
39
+ if image is None:
40
+ return "⚠️ Please upload or take a picture first."
41
+
42
+ results = {}
43
+ results["Category"], cat_score = predict_best_prompt(image, category_prompts)
44
+ results["Color"], color_score = predict_best_prompt(image, color_prompts)
45
+ results["Pattern"], pattern_score = predict_best_prompt(image, pattern_prompts)
46
+ results["Fit"], fit_score = predict_best_prompt(image, fit_prompts)
47
+
48
+ return f"""
49
+ Category: {results['Category']} ({cat_score:.2f})\n
50
+ Color: {results['Color']} ({color_score:.2f})\n
51
+ Pattern: {results['Pattern']} ({pattern_score:.2f})\n
52
+ Fit: {results['Fit']} ({fit_score:.2f})
53
+ """
54
+
55
+ # Gradio UI erstellen
56
+ iface = gr.Interface(
57
+ fn=analyze_image,
58
+ inputs=gr.Image(type="pil", label="Upload or take a picture", sources=["upload", "webcam"]),
59
+ outputs="text",
60
+ title="Fashion Attribute Predictor (Prototype 2 + Segmentation)",
61
+ description="Upload or capture an image of a t-shirt or pullover. The model first removes the background and then predicts category, color, pattern, and fit using FashionCLIP."
62
+ )
63
+
64
+ # App starten
65
+ if __name__ == "__main__":
66
+ iface.launch()
requirements_segmented.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ transformers
4
+ rembg
5
+ Pillow
6
+ numpy