Spaces:
Sleeping
Sleeping
chips
commited on
Commit
·
f88c368
1
Parent(s):
3263fff
added image combining and first version of create base
Browse files- app.py +43 -2
- functions.py +54 -0
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import redis
|
|
| 11 |
import uuid
|
| 12 |
from time import sleep
|
| 13 |
import json
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
@@ -91,10 +92,50 @@ async def run_virtual_tryon_pipeline(
|
|
| 91 |
num_images
|
| 92 |
):
|
| 93 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
r.set(request_id, "Creating base image")
|
| 95 |
-
sleep(5)
|
| 96 |
# STEP 1: make base image
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
r.set(request_id, "Combining garment images")
|
| 100 |
sleep(5)
|
|
|
|
| 11 |
import uuid
|
| 12 |
from time import sleep
|
| 13 |
import json
|
| 14 |
+
from functions import combine_images_side_by_side
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
| 92 |
num_images
|
| 93 |
):
|
| 94 |
try:
|
| 95 |
+
r.set(request_id, "checking incoming data...")
|
| 96 |
+
front_type = ""
|
| 97 |
+
back_type = ""
|
| 98 |
+
if top_garment_image is None and bottom_garment_image is None:
|
| 99 |
+
return {"error": "Missing garment images"}
|
| 100 |
+
if top_garment_image or bottom_garment_image is None:
|
| 101 |
+
#this is a single image try on
|
| 102 |
+
front_type = "single"
|
| 103 |
+
else:
|
| 104 |
+
front_type = "double"
|
| 105 |
+
if top_back_garment_image is None and bottom_back_garment_image is None:
|
| 106 |
+
back_type = "none"
|
| 107 |
+
else:
|
| 108 |
+
if top_back_garment_image or bottom_back_garment_image is None:
|
| 109 |
+
#this is a single image try on for the back
|
| 110 |
+
back_type = "single"
|
| 111 |
+
else:
|
| 112 |
+
back_type = "double"
|
| 113 |
+
print(f"front_type: {front_type}")
|
| 114 |
+
print(f"back_type: {back_type}")
|
| 115 |
+
if front_type == "double":
|
| 116 |
+
front_final_image = combine_images_side_by_side(top_garment_image.read(), bottom_garment_image.read())
|
| 117 |
+
print(f"front_combined_image: {front_final_image}")
|
| 118 |
+
if back_type == "double":
|
| 119 |
+
back_final_image = combine_images_side_by_side(top_back_garment_image.read(), bottom_back_garment_image.read())
|
| 120 |
+
print(f"back_combined_image: {back_final_image}")
|
| 121 |
+
if front_type == "single":
|
| 122 |
+
front_final_image = top_garment_image.read()
|
| 123 |
+
print(f"front_combined_image: {front_final_image}")
|
| 124 |
+
if back_type == "single":
|
| 125 |
+
back_final_image = top_back_garment_image.read()
|
| 126 |
+
print(f"back_combined_image: {back_final_image}")
|
| 127 |
+
r.set(request_id, "Garment images combined")
|
| 128 |
+
#Check incoming data. do we have the required data. Are the images what we expect? (ie flatlay image file etc.)
|
| 129 |
+
#lets use openai for this. Also, return errors where needed
|
| 130 |
r.set(request_id, "Creating base image")
|
|
|
|
| 131 |
# STEP 1: make base image
|
| 132 |
+
try:
|
| 133 |
+
base_image = base_generator.create_image(talent_lora_url, talent_trigger_word, "", 1, num_images)
|
| 134 |
+
print(f"base_image: {base_image}")
|
| 135 |
+
except Exception as e:
|
| 136 |
+
r.set(request_id, "error generating base image")
|
| 137 |
+
r.set(f"{request_id}_error", str(e))
|
| 138 |
+
return
|
| 139 |
|
| 140 |
r.set(request_id, "Combining garment images")
|
| 141 |
sleep(5)
|
functions.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import os
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import base64
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
def combine_images_side_by_side(image1: bytes, image2: bytes) -> str:
|
| 8 |
+
"""
|
| 9 |
+
Takes two images, scales them to 1280x1280, combines them side by side,
|
| 10 |
+
saves to tmp storage, and returns the base64 encoded image string.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
image1: First image as bytes
|
| 14 |
+
image2: Second image as bytes
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
Base64 encoded string of the combined image
|
| 18 |
+
"""
|
| 19 |
+
# Create temp directory if it doesn't exist
|
| 20 |
+
os.makedirs('tmp', exist_ok=True)
|
| 21 |
+
|
| 22 |
+
# Open images from bytes
|
| 23 |
+
img1 = Image.open(BytesIO(image1))
|
| 24 |
+
img2 = Image.open(BytesIO(image2))
|
| 25 |
+
|
| 26 |
+
# Scale both images to 1280x1280
|
| 27 |
+
img1 = img1.resize((1280, 1280))
|
| 28 |
+
img2 = img2.resize((1280, 1280))
|
| 29 |
+
|
| 30 |
+
# Create new image with combined width (2560) and height (1280)
|
| 31 |
+
combined = Image.new('RGB', (2560, 1280))
|
| 32 |
+
|
| 33 |
+
# Paste images side by side
|
| 34 |
+
combined.paste(img1, (0, 0))
|
| 35 |
+
combined.paste(img2, (1280, 0))
|
| 36 |
+
|
| 37 |
+
# Generate unique filename
|
| 38 |
+
filename = f"tmp/combined_{uuid.uuid4()}.png"
|
| 39 |
+
|
| 40 |
+
# Save image
|
| 41 |
+
combined.save(filename)
|
| 42 |
+
|
| 43 |
+
# Convert to base64
|
| 44 |
+
with open(filename, "rb") as image_file:
|
| 45 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 46 |
+
|
| 47 |
+
# Clean up
|
| 48 |
+
os.remove(filename)
|
| 49 |
+
|
| 50 |
+
return encoded_string
|
| 51 |
+
|
| 52 |
+
# Example usage:
|
| 53 |
+
# with open('image1.jpg', 'rb') as f1, open('image2.jpg', 'rb') as f2:
|
| 54 |
+
# combined_image = combine_images_side_by_side(f1.read(), f2.read())
|
requirements.txt
CHANGED
|
@@ -5,4 +5,5 @@ python-multipart
|
|
| 5 |
fal-client
|
| 6 |
requests
|
| 7 |
openai
|
| 8 |
-
redis
|
|
|
|
|
|
| 5 |
fal-client
|
| 6 |
requests
|
| 7 |
openai
|
| 8 |
+
redis
|
| 9 |
+
Pillow>=10.0.0
|