Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,50 +8,85 @@ import time
|
|
| 8 |
import requests
|
| 9 |
import base64
|
| 10 |
from simple_salesforce import Salesforce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Initialize MediaPipe
|
| 13 |
mp_pose = mp.solutions.pose
|
| 14 |
pose = mp_pose.Pose(static_image_mode=True, model_complexity=2)
|
| 15 |
|
| 16 |
-
# Salesforce Configuration
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
sf = Salesforce(
|
| 24 |
-
username=SF_USERNAME,
|
| 25 |
-
password=SF_PASSWORD,
|
| 26 |
-
security_token=SF_SECURITY_TOKEN,
|
| 27 |
-
domain=SF_DOMAIN
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def generate_thumbnail(img, size=(150, 200)):
|
| 57 |
"""Generate base64 thumbnail for UI"""
|
|
@@ -61,67 +96,44 @@ def generate_thumbnail(img, size=(150, 200)):
|
|
| 61 |
img.save(buffered, format="PNG")
|
| 62 |
return f"data:image/png;base64,{base64.b64encode(buffered.getvalue()).decode()}"
|
| 63 |
|
| 64 |
-
|
| 65 |
-
"""Detect body pose using MediaPipe"""
|
| 66 |
-
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 67 |
-
if not results.pose_landmarks:
|
| 68 |
-
return None
|
| 69 |
-
|
| 70 |
-
h, w = image.shape[:2]
|
| 71 |
-
landmarks = results.pose_landmarks.landmark
|
| 72 |
-
return {
|
| 73 |
-
'left_shoulder': (landmarks[11].x * w, landmarks[11].y * h),
|
| 74 |
-
'right_shoulder': (landmarks[12].x * w, landmarks[12].y * h),
|
| 75 |
-
'left_hip': (landmarks[23].x * w, landmarks[23].y * h),
|
| 76 |
-
'right_hip': (landmarks[24].x * w, landmarks[24].y * h)
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
def warp_and_overlay(user_img, dress_img, body_pts):
|
| 80 |
-
"""Thin Plate Spline warping with alpha blending"""
|
| 81 |
-
# Warp dress
|
| 82 |
-
h, w = dress_img.shape[:2]
|
| 83 |
-
src_pts = np.float32([[0,0], [w,0], [w,h], [0,h]])
|
| 84 |
-
dst_pts = np.float32([body_pts['left_shoulder'], body_pts['right_shoulder'],
|
| 85 |
-
body_pts['right_hip'], body_pts['left_hip']])
|
| 86 |
-
|
| 87 |
-
tps = cv2.createThinPlateSplineShapeTransformer()
|
| 88 |
-
tps.estimateTransformation(dst_pts, src_pts)
|
| 89 |
-
warped_dress = tps.warpImage(dress_img)
|
| 90 |
-
|
| 91 |
-
# Blend images
|
| 92 |
-
alpha = warped_dress[:,:,3] / 255.0
|
| 93 |
-
for c in range(3):
|
| 94 |
-
user_img[:,:,c] = user_img[:,:,c] * (1 - alpha) + warped_dress[:,:,c] * alpha
|
| 95 |
-
|
| 96 |
-
return user_img
|
| 97 |
|
| 98 |
def process_try_on(user_image, dress_id):
|
| 99 |
-
"""Main processing
|
| 100 |
if user_image is None:
|
| 101 |
return None, "Please upload an image"
|
| 102 |
|
| 103 |
start_time = time.time()
|
| 104 |
-
dresses = fetch_dresses_from_salesforce()
|
| 105 |
-
|
| 106 |
-
if not dresses or dress_id not in dresses:
|
| 107 |
-
return user_image, "No dresses found in Salesforce"
|
| 108 |
|
| 109 |
try:
|
|
|
|
| 110 |
user_img = np.array(user_image)
|
|
|
|
|
|
|
| 111 |
body_pts = get_body_landmarks(user_img)
|
| 112 |
if not body_pts:
|
| 113 |
return user_image, "Stand facing camera with arms slightly away"
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
dress_img = np.array(dresses[dress_id]['image'])
|
| 116 |
result = warp_and_overlay(user_img.copy(), dress_img, body_pts)
|
| 117 |
|
| 118 |
return Image.fromarray(result), f"Done in {time.time()-start_time:.2f}s"
|
| 119 |
except Exception as e:
|
|
|
|
| 120 |
return user_image, f"Error: {str(e)}"
|
| 121 |
|
| 122 |
# Gradio Interface
|
| 123 |
-
with gr.Blocks(title="
|
| 124 |
-
gr.Markdown("# 👗 Virtual Try-On (
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column():
|
|
@@ -130,19 +142,37 @@ with gr.Blocks(title="Salesforce Virtual Try-On", css=".thumbnail { height: 100p
|
|
| 130 |
refresh_btn = gr.Button("🔄 Refresh Dresses")
|
| 131 |
try_btn = gr.Button("👗 Try On Dress", variant="primary")
|
| 132 |
dress_dropdown = gr.Dropdown(label="Select Dress", interactive=True)
|
|
|
|
|
|
|
| 133 |
|
| 134 |
with gr.Column():
|
| 135 |
output_image = gr.Image(label="Your Virtual Try-On", interactive=False)
|
| 136 |
status = gr.Textbox(label="Status")
|
| 137 |
|
| 138 |
def update_dress_dropdown():
|
| 139 |
-
dresses =
|
| 140 |
choices = [(f"{d['name']}", id) for id, d in dresses.items()]
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 8 |
import requests
|
| 9 |
import base64
|
| 10 |
from simple_salesforce import Salesforce
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
# Configure logging
|
| 14 |
+
logging.basicConfig(level=logging.INFO)
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
# Initialize MediaPipe
|
| 18 |
mp_pose = mp.solutions.pose
|
| 19 |
pose = mp_pose.Pose(static_image_mode=True, model_complexity=2)
|
| 20 |
|
| 21 |
+
# Salesforce Configuration (use environment variables in production)
|
| 22 |
+
SF_CONFIG = {
|
| 23 |
+
"username": "your_salesforce_username@example.com",
|
| 24 |
+
"password": "your_password",
|
| 25 |
+
"security_token": "your_security_token",
|
| 26 |
+
"domain": "login" # For production: "login" or "test"
|
| 27 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
class SalesforceConnector:
|
| 30 |
+
def __init__(self):
|
| 31 |
+
self.sf = None
|
| 32 |
+
self.connect()
|
| 33 |
+
|
| 34 |
+
def connect(self):
|
| 35 |
+
try:
|
| 36 |
+
self.sf = Salesforce(
|
| 37 |
+
username=SF_CONFIG["username"],
|
| 38 |
+
password=SF_CONFIG["password"],
|
| 39 |
+
security_token=SF_CONFIG["security_token"],
|
| 40 |
+
domain=SF_CONFIG["domain"]
|
| 41 |
+
)
|
| 42 |
+
logger.info("Successfully connected to Salesforce")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
logger.error(f"Salesforce connection failed: {str(e)}")
|
| 45 |
+
self.sf = None
|
| 46 |
+
|
| 47 |
+
def is_connected(self):
|
| 48 |
+
return self.sf is not None
|
| 49 |
+
|
| 50 |
+
def query_dresses(self):
|
| 51 |
+
if not self.is_connected():
|
| 52 |
+
self.connect()
|
| 53 |
+
if not self.is_connected():
|
| 54 |
+
return {}
|
| 55 |
|
| 56 |
+
try:
|
| 57 |
+
result = self.sf.query(
|
| 58 |
+
"SELECT Id, Name, Image_URL__c FROM Product2 "
|
| 59 |
+
"WHERE IsActive = TRUE AND Family = 'Dress' "
|
| 60 |
+
"LIMIT 20"
|
| 61 |
+
)
|
| 62 |
+
return result.get("records", [])
|
| 63 |
+
except Exception as e:
|
| 64 |
+
logger.error(f"Salesforce query failed: {str(e)}")
|
| 65 |
+
return []
|
| 66 |
+
|
| 67 |
+
sf_connector = SalesforceConnector()
|
| 68 |
+
|
| 69 |
+
def fetch_dresses():
|
| 70 |
+
"""Fetch dresses with automatic reconnection"""
|
| 71 |
+
dresses = {}
|
| 72 |
+
records = sf_connector.query_dresses()
|
| 73 |
+
|
| 74 |
+
for item in records:
|
| 75 |
+
if not item.get('Image_URL__c'):
|
| 76 |
+
continue
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
img_response = requests.get(item['Image_URL__c'], timeout=10)
|
| 80 |
+
img = Image.open(BytesIO(img_response.content)).convert("RGBA")
|
| 81 |
+
dresses[item['Id']] = {
|
| 82 |
+
'name': item['Name'],
|
| 83 |
+
'image': img,
|
| 84 |
+
'thumbnail': generate_thumbnail(img)
|
| 85 |
+
}
|
| 86 |
+
except Exception as e:
|
| 87 |
+
logger.warning(f"Failed to load dress {item.get('Name')}: {str(e)}")
|
| 88 |
+
|
| 89 |
+
return dresses
|
| 90 |
|
| 91 |
def generate_thumbnail(img, size=(150, 200)):
|
| 92 |
"""Generate base64 thumbnail for UI"""
|
|
|
|
| 96 |
img.save(buffered, format="PNG")
|
| 97 |
return f"data:image/png;base64,{base64.b64encode(buffered.getvalue()).decode()}"
|
| 98 |
|
| 99 |
+
# Rest of your existing functions (get_body_landmarks, warp_and_overlay, etc.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
def process_try_on(user_image, dress_id):
|
| 102 |
+
"""Main processing with enhanced error handling"""
|
| 103 |
if user_image is None:
|
| 104 |
return None, "Please upload an image"
|
| 105 |
|
| 106 |
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
# Convert to numpy array
|
| 110 |
user_img = np.array(user_image)
|
| 111 |
+
|
| 112 |
+
# Get body landmarks
|
| 113 |
body_pts = get_body_landmarks(user_img)
|
| 114 |
if not body_pts:
|
| 115 |
return user_image, "Stand facing camera with arms slightly away"
|
| 116 |
|
| 117 |
+
# Get dresses
|
| 118 |
+
dresses = fetch_dresses()
|
| 119 |
+
if not dresses:
|
| 120 |
+
return user_image, "No dresses available - check Salesforce connection"
|
| 121 |
+
|
| 122 |
+
if dress_id not in dresses:
|
| 123 |
+
return user_image, "Selected dress not found"
|
| 124 |
+
|
| 125 |
+
# Process try-on
|
| 126 |
dress_img = np.array(dresses[dress_id]['image'])
|
| 127 |
result = warp_and_overlay(user_img.copy(), dress_img, body_pts)
|
| 128 |
|
| 129 |
return Image.fromarray(result), f"Done in {time.time()-start_time:.2f}s"
|
| 130 |
except Exception as e:
|
| 131 |
+
logger.error(f"Try-on failed: {str(e)}")
|
| 132 |
return user_image, f"Error: {str(e)}"
|
| 133 |
|
| 134 |
# Gradio Interface
|
| 135 |
+
with gr.Blocks(title="Virtual Try-On", css=".thumbnail { height: 100px !important }") as demo:
|
| 136 |
+
gr.Markdown("# 👗 Virtual Try-On (Salesforce Connected)")
|
| 137 |
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column():
|
|
|
|
| 142 |
refresh_btn = gr.Button("🔄 Refresh Dresses")
|
| 143 |
try_btn = gr.Button("👗 Try On Dress", variant="primary")
|
| 144 |
dress_dropdown = gr.Dropdown(label="Select Dress", interactive=True)
|
| 145 |
+
connection_status = gr.Textbox(label="Salesforce Status",
|
| 146 |
+
value="Connected" if sf_connector.is_connected() else "Disconnected")
|
| 147 |
|
| 148 |
with gr.Column():
|
| 149 |
output_image = gr.Image(label="Your Virtual Try-On", interactive=False)
|
| 150 |
status = gr.Textbox(label="Status")
|
| 151 |
|
| 152 |
def update_dress_dropdown():
|
| 153 |
+
dresses = fetch_dresses()
|
| 154 |
choices = [(f"{d['name']}", id) for id, d in dresses.items()]
|
| 155 |
+
status = "Connected" if sf_connector.is_connected() else "Disconnected"
|
| 156 |
+
return (
|
| 157 |
+
gr.Dropdown(choices=choices, value=choices[0][1] if choices else None),
|
| 158 |
+
f"Salesforce: {status} | {len(choices)} dresses loaded"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
refresh_btn.click(
|
| 162 |
+
update_dress_dropdown,
|
| 163 |
+
outputs=[dress_dropdown, connection_status]
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
try_btn.click(
|
| 167 |
+
process_try_on,
|
| 168 |
+
inputs=[input_image, dress_dropdown],
|
| 169 |
+
outputs=[output_image, status]
|
| 170 |
+
)
|
| 171 |
|
| 172 |
+
demo.load(
|
| 173 |
+
update_dress_dropdown,
|
| 174 |
+
outputs=[dress_dropdown, connection_status]
|
| 175 |
+
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|