Spaces:
Sleeping
Sleeping
wracell
commited on
Commit
Β·
05527ba
1
Parent(s):
6d8e2d8
added features
Browse files- .gitattributes +1 -0
- app.py +54 -38
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.pyc filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -2,10 +2,13 @@ import streamlit as st
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
|
|
|
|
|
|
| 6 |
from segment_anything import sam_model_registry, SamPredictor
|
| 7 |
from io import BytesIO
|
| 8 |
import base64
|
|
|
|
| 9 |
from google.generativeai import configure, GenerativeModel
|
| 10 |
|
| 11 |
# Configure Gemini API
|
|
@@ -14,53 +17,57 @@ model = GenerativeModel("gemini-2.0-flash")
|
|
| 14 |
|
| 15 |
# Load SAM model with ViT-Base
|
| 16 |
def load_sam_model():
|
| 17 |
-
sam = sam_model_registry["vit_b"](checkpoint="sam_vit_b.pth")
|
| 18 |
predictor = SamPredictor(sam)
|
| 19 |
return predictor
|
| 20 |
|
| 21 |
# Preprocess image using OpenCV (Edge Detection & Background Removal)
|
| 22 |
def preprocess_image(image):
|
| 23 |
-
image = np.array(image.convert("RGB"))
|
| 24 |
-
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 25 |
-
edges = cv2.Canny(gray, 100, 200)
|
| 26 |
-
return Image.fromarray(edges)
|
| 27 |
|
| 28 |
# Segment garment using SAM
|
| 29 |
def segment_garment(image, predictor):
|
| 30 |
image_np = np.array(image.convert("RGB"))
|
| 31 |
predictor.set_image(image_np)
|
| 32 |
|
| 33 |
-
# Use center point of image as prompt
|
| 34 |
height, width, _ = image_np.shape
|
| 35 |
input_point = np.array([[width // 2, height // 2]])
|
| 36 |
-
input_label = np.array([1])
|
| 37 |
|
| 38 |
masks, _, _ = predictor.predict(point_coords=input_point, point_labels=input_label)
|
| 39 |
-
mask = masks[0]
|
| 40 |
-
|
| 41 |
-
# Resize mask to match image
|
| 42 |
mask_resized = cv2.resize(mask.astype(np.uint8) * 255, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 43 |
-
mask_resized = np.stack([mask_resized] * 3, axis=-1)
|
| 44 |
|
| 45 |
-
# Apply segmentation mask
|
| 46 |
segmented = np.where(mask_resized > 0, image_np, 0)
|
| 47 |
-
|
| 48 |
return Image.fromarray(segmented)
|
| 49 |
|
| 50 |
# AI garment analysis
|
| 51 |
-
def analyze_garment(image):
|
| 52 |
image_bytes = BytesIO()
|
| 53 |
image.save(image_bytes, format="PNG")
|
| 54 |
encoded_image = base64.b64encode(image_bytes.getvalue()).decode("utf-8")
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
prompt = {
|
| 57 |
"parts": [
|
| 58 |
-
{
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
{"inline_data": {"mime_type": "image/png", "data": encoded_image}}
|
| 61 |
]
|
| 62 |
}
|
| 63 |
-
|
| 64 |
response = model.generate_content(prompt)
|
| 65 |
return response.text if response else "Analysis failed."
|
| 66 |
|
|
@@ -68,42 +75,51 @@ def analyze_garment(image):
|
|
| 68 |
sam_predictor = load_sam_model()
|
| 69 |
|
| 70 |
# Streamlit UI
|
| 71 |
-
st.title("π AI Fashion Analysis
|
| 72 |
|
| 73 |
-
# Description and Instructions
|
| 74 |
st.markdown("""
|
| 75 |
### π How to Use this App:
|
| 76 |
-
1.
|
| 77 |
-
2.
|
| 78 |
-
3.
|
| 79 |
-
4.
|
| 80 |
""")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 84 |
|
| 85 |
-
if uploaded_file
|
| 86 |
image = Image.open(uploaded_file)
|
| 87 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
if "processed_image" not in st.session_state:
|
| 91 |
st.session_state.processed_image = None
|
| 92 |
if "segmented_image" not in st.session_state:
|
| 93 |
st.session_state.segmented_image = None
|
| 94 |
|
| 95 |
-
|
| 96 |
-
if st.button("Preprocess"):
|
| 97 |
st.session_state.processed_image = preprocess_image(image)
|
| 98 |
st.session_state.segmented_image = segment_garment(image, sam_predictor)
|
| 99 |
|
| 100 |
-
# Display Preprocessed Images if Available
|
| 101 |
if st.session_state.processed_image:
|
| 102 |
-
st.image(st.session_state.processed_image, caption="Edge Detection", use_container_width=True)
|
| 103 |
if st.session_state.segmented_image:
|
| 104 |
-
st.image(st.session_state.segmented_image, caption="Segmented Garment", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
from torchvision import transforms, models
|
| 8 |
from segment_anything import sam_model_registry, SamPredictor
|
| 9 |
from io import BytesIO
|
| 10 |
import base64
|
| 11 |
+
import requests
|
| 12 |
from google.generativeai import configure, GenerativeModel
|
| 13 |
|
| 14 |
# Configure Gemini API
|
|
|
|
| 17 |
|
| 18 |
# Load SAM model with ViT-Base
|
| 19 |
def load_sam_model():
|
| 20 |
+
sam = sam_model_registry["vit_b"](checkpoint="sam_vit_b.pth")
|
| 21 |
predictor = SamPredictor(sam)
|
| 22 |
return predictor
|
| 23 |
|
| 24 |
# Preprocess image using OpenCV (Edge Detection & Background Removal)
|
| 25 |
def preprocess_image(image):
|
| 26 |
+
image = np.array(image.convert("RGB"))
|
| 27 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 28 |
+
edges = cv2.Canny(gray, 100, 200)
|
| 29 |
+
return Image.fromarray(edges)
|
| 30 |
|
| 31 |
# Segment garment using SAM
|
| 32 |
def segment_garment(image, predictor):
|
| 33 |
image_np = np.array(image.convert("RGB"))
|
| 34 |
predictor.set_image(image_np)
|
| 35 |
|
|
|
|
| 36 |
height, width, _ = image_np.shape
|
| 37 |
input_point = np.array([[width // 2, height // 2]])
|
| 38 |
+
input_label = np.array([1])
|
| 39 |
|
| 40 |
masks, _, _ = predictor.predict(point_coords=input_point, point_labels=input_label)
|
| 41 |
+
mask = masks[0]
|
|
|
|
|
|
|
| 42 |
mask_resized = cv2.resize(mask.astype(np.uint8) * 255, (width, height), interpolation=cv2.INTER_NEAREST)
|
| 43 |
+
mask_resized = np.stack([mask_resized] * 3, axis=-1)
|
| 44 |
|
|
|
|
| 45 |
segmented = np.where(mask_resized > 0, image_np, 0)
|
|
|
|
| 46 |
return Image.fromarray(segmented)
|
| 47 |
|
| 48 |
# AI garment analysis
|
| 49 |
+
def analyze_garment(image, style_pref=None, feedback=None, generate_variations=False):
|
| 50 |
image_bytes = BytesIO()
|
| 51 |
image.save(image_bytes, format="PNG")
|
| 52 |
encoded_image = base64.b64encode(image_bytes.getvalue()).decode("utf-8")
|
| 53 |
|
| 54 |
+
style_input = f"\nStyle preference: {style_pref}" if style_pref else ""
|
| 55 |
+
feedback_input = f"\nUser feedback: {feedback}" if feedback else ""
|
| 56 |
+
variation_input = "\nGenerate 3 variations of this design based on the given preferences." if generate_variations else ""
|
| 57 |
+
|
| 58 |
prompt = {
|
| 59 |
"parts": [
|
| 60 |
+
{
|
| 61 |
+
"text": (
|
| 62 |
+
"Analyze the garment in this image, describing its style, fabric, and design elements. "
|
| 63 |
+
"Suggest the best occasions to wear it and recommend complementary fashion pieces." +
|
| 64 |
+
style_input + feedback_input + variation_input
|
| 65 |
+
)
|
| 66 |
+
},
|
| 67 |
{"inline_data": {"mime_type": "image/png", "data": encoded_image}}
|
| 68 |
]
|
| 69 |
}
|
| 70 |
+
|
| 71 |
response = model.generate_content(prompt)
|
| 72 |
return response.text if response else "Analysis failed."
|
| 73 |
|
|
|
|
| 75 |
sam_predictor = load_sam_model()
|
| 76 |
|
| 77 |
# Streamlit UI
|
| 78 |
+
st.title("π AI Fashion Analysis & Design Studio")
|
| 79 |
|
|
|
|
| 80 |
st.markdown("""
|
| 81 |
### π How to Use this App:
|
| 82 |
+
1. Upload a fashion image or sketch.
|
| 83 |
+
2. Preprocess to see edge detection and garment segmentation.
|
| 84 |
+
3. Provide your style preferences or feedback.
|
| 85 |
+
4. Analyze and generate new fashion ideas using AI!
|
| 86 |
""")
|
| 87 |
|
| 88 |
+
uploaded_file = st.file_uploader("π€ Upload a fashion image (PNG, JPG, JPEG)", type=["jpg", "png", "jpeg"])
|
|
|
|
| 89 |
|
| 90 |
+
if uploaded_file:
|
| 91 |
image = Image.open(uploaded_file)
|
| 92 |
+
st.image(image, caption="πΌοΈ Uploaded Image", use_container_width=True)
|
| 93 |
|
| 94 |
+
# Session state setup
|
| 95 |
if "processed_image" not in st.session_state:
|
| 96 |
st.session_state.processed_image = None
|
| 97 |
if "segmented_image" not in st.session_state:
|
| 98 |
st.session_state.segmented_image = None
|
| 99 |
|
| 100 |
+
if st.button("βοΈ Preprocess Image"):
|
|
|
|
| 101 |
st.session_state.processed_image = preprocess_image(image)
|
| 102 |
st.session_state.segmented_image = segment_garment(image, sam_predictor)
|
| 103 |
|
|
|
|
| 104 |
if st.session_state.processed_image:
|
| 105 |
+
st.image(st.session_state.processed_image, caption="π§ Edge Detection", use_container_width=True)
|
| 106 |
if st.session_state.segmented_image:
|
| 107 |
+
st.image(st.session_state.segmented_image, caption="βοΈ Segmented Garment", use_container_width=True)
|
| 108 |
+
|
| 109 |
+
# π§βπ€βπ§ Human-AI Interaction
|
| 110 |
+
st.markdown("### π§ Customize Your Style & Get AI Suggestions")
|
| 111 |
+
style_pref = st.text_input("π Describe your style preference (e.g., elegant, streetwear, minimalist)")
|
| 112 |
+
feedback = st.text_input("π£οΈ Provide feedback to AI (e.g., Make it more vibrant, Use denim)")
|
| 113 |
|
| 114 |
+
generate_variations = st.checkbox("π¨ Generate design variations")
|
| 115 |
+
|
| 116 |
+
if st.button("π€ Analyze & Collaborate with AI"):
|
| 117 |
+
result = analyze_garment(image, style_pref, feedback, generate_variations)
|
| 118 |
+
st.success(result)
|
| 119 |
+
|
| 120 |
+
# Fabric suggestion extraction (Optional basic parsing)
|
| 121 |
+
st.markdown("### π§΅ Suggested Fabrics / Materials:")
|
| 122 |
+
for line in result.splitlines():
|
| 123 |
+
if "fabric" in line.lower() or "material" in line.lower():
|
| 124 |
+
st.info(line)
|
| 125 |
+
|