Spaces:
Running
Running
import replicate | |
import requests | |
import os | |
def virtual_try_on(garm_img_url, human_img_url, garment_des): | |
""" | |
Perform virtual try-on using the IDM-VTON model on Replicate. | |
Args: | |
garm_img_url (str): Path or URL of the garment image | |
human_img_url (str): Path or URL of the human image | |
garment_des (str): Description of the garment | |
Returns: | |
str: Path to the saved output image | |
""" | |
# Convert local files to data URLs if needed | |
def file_to_data_url(file_path): | |
if file_path.startswith('http'): | |
response = requests.get(file_path) | |
img_data = response.content | |
else: | |
with open(file_path, 'rb') as f: | |
img_data = f.read() | |
import base64 | |
encoded = base64.b64encode(img_data).decode('utf-8') | |
return f"data:image/png;base64,{encoded}" | |
# Prepare input for the model | |
input_data = { | |
"garm_img": file_to_data_url(garm_img_url), | |
"human_img": file_to_data_url(human_img_url), | |
"garment_des": garment_des | |
} | |
print("Sending request to IDM-VTON model...") | |
# Run the model on Replicate | |
output = replicate.run( | |
"cuuupid/idm-vton:c871bb9b046607b680449ecbae55fd8c6d945e0a1948644bf2361b3d021d3ff4", | |
input=input_data | |
) | |
# Save the output image | |
output_path = "output.jpg" | |
with open(output_path, "wb") as file: | |
file.write(output.read()) | |
print(f"Virtual try-on complete! Output saved to {output_path}") | |
return output_path | |
# Example usage | |
if __name__ == "__main__": | |
# Example inputs | |
garm_img = "https://replicate.delivery/pbxt/KgwTlZyFx5aUU3gc5gMiKuD5nNPTgliMlLUWx160G4z99YjO/sweater.webp" | |
human_img = "https://replicate.delivery/pbxt/KgwTlhCMvDagRrcVzZJbuozNJ8esPqiNAIJS3eMgHrYuHmW4/KakaoTalk_Photo_2024-04-04-21-44-45.png" | |
garment_des = "cute pink top" | |
# Check if REPLICATE_API_TOKEN is set | |
if "REPLICATE_API_TOKEN" not in os.environ: | |
print("Warning: REPLICATE_API_TOKEN environment variable is not set.") | |
print("Please set it using: export REPLICATE_API_TOKEN='your_token_here'") | |
try: | |
output_path = virtual_try_on(garm_img, human_img, garment_des) | |
print(f"Success! Try-on image saved to {output_path}") | |
except Exception as e: | |
print(f"Error occurred: {e}") |