Zuii commited on
Commit
80c7355
·
verified ·
1 Parent(s): 88381b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -24
app.py CHANGED
@@ -1,26 +1,21 @@
1
  import os
2
- import cv2
3
- import base64
4
- import random
5
  import requests
6
  import json
7
  import time
8
- import numpy as np
9
  import gradio as gr
10
 
11
  MAX_SEED = 999999
12
 
13
- # Convert images to PNG format (ensure supported format)
14
- def convert_to_png(image):
15
- return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
16
-
17
- # Encode images to base64 (forces PNG format)
18
- def encode_image(image):
19
- # Ensure image is PNG
20
- image = convert_to_png(image)
21
- _, buffer = cv2.imencode('.png', image)
22
- return base64.b64encode(buffer).decode('utf-8')
23
 
 
 
 
 
 
24
 
25
  # Main try-on function using Pixelcut API
26
  def tryon(person_img, garment_img, seed, randomize_seed):
@@ -34,9 +29,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
34
  if randomize_seed:
35
  seed = random.randint(0, MAX_SEED)
36
 
37
- # Convert images to base64 PNG
38
- encoded_person_img = encode_image(person_img)
39
- encoded_garment_img = encode_image(garment_img)
 
 
 
40
 
41
  # Load Pixelcut API key from environment
42
  pixelcut_api_key = os.environ.get('pixelcut_api_key')
@@ -51,12 +49,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
51
  'X-API-KEY': pixelcut_api_key
52
  }
53
 
54
- # Setup request payload with PNG format
55
  data = {
56
- "person_image": encoded_person_img,
57
- "garment_image": encoded_garment_img,
58
  "seed": seed,
59
- "format": "png" # Ensure API gets PNG format
60
  }
61
 
62
  result_img = None
@@ -79,8 +77,8 @@ def tryon(person_img, garment_img, seed, randomize_seed):
79
  # If Pixelcut returns a result URL, fetch the final image
80
  if result_url:
81
  result_img_data = requests.get(result_url).content
82
- result_np = np.frombuffer(result_img_data, np.uint8)
83
- result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
84
  info = "Success"
85
  break
86
 
@@ -115,7 +113,6 @@ def tryon(person_img, garment_img, seed, randomize_seed):
115
  return result_img, seed, info
116
 
117
 
118
-
119
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
120
 
121
  garm_list = os.listdir(os.path.join(example_path,"cloth"))
 
1
  import os
 
 
 
2
  import requests
3
  import json
4
  import time
 
5
  import gradio as gr
6
 
7
  MAX_SEED = 999999
8
 
9
+ # Upload image to a temporary host (like Hugging Face or Imgur)
10
+ def upload_image_to_host(image_data):
11
+ files = {'file': ('image.png', image_data)}
12
+ upload_response = requests.post("https://tmpfiles.org/api/v1/upload", files=files)
 
 
 
 
 
 
13
 
14
+ if upload_response.status_code == 200:
15
+ return upload_response.json().get("data", {}).get("url", None)
16
+ else:
17
+ print("Image upload failed:", upload_response.text)
18
+ return None
19
 
20
  # Main try-on function using Pixelcut API
21
  def tryon(person_img, garment_img, seed, randomize_seed):
 
29
  if randomize_seed:
30
  seed = random.randint(0, MAX_SEED)
31
 
32
+ # Upload images and get URLs
33
+ person_url = upload_image_to_host(person_img)
34
+ garment_url = upload_image_to_host(garment_img)
35
+
36
+ if not person_url or not garment_url:
37
+ return None, None, "Image upload failed"
38
 
39
  # Load Pixelcut API key from environment
40
  pixelcut_api_key = os.environ.get('pixelcut_api_key')
 
49
  'X-API-KEY': pixelcut_api_key
50
  }
51
 
52
+ # Setup request payload with URLs
53
  data = {
54
+ "person_image_url": person_url,
55
+ "garment_image_url": garment_url,
56
  "seed": seed,
57
+ "format": "png"
58
  }
59
 
60
  result_img = None
 
77
  # If Pixelcut returns a result URL, fetch the final image
78
  if result_url:
79
  result_img_data = requests.get(result_url).content
80
+ result_img = gr.Image()
81
+ result_img.value = result_img_data
82
  info = "Success"
83
  break
84
 
 
113
  return result_img, seed, info
114
 
115
 
 
116
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
117
 
118
  garm_list = os.listdir(os.path.join(example_path,"cloth"))