Zuii commited on
Commit
f7221f1
·
verified ·
1 Parent(s): 9b05d2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -54
app.py CHANGED
@@ -1,65 +1,61 @@
1
  import requests
2
- import gradio as gr
3
 
4
- # Set your Pixelcut API key directly
5
  API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Function to remove background from a local image file
8
- def remove_bg(image_path):
9
- try:
10
- with open(image_path, "rb") as image_file:
11
- response = requests.post(
12
- "https://api.developer.pixelcut.ai/v1/remove-background",
13
- headers={
14
- "Accept": "application/json",
15
- "X-API-KEY": API_KEY,
16
- },
17
- files={"image": image_file},
18
- data={"format": "png"},
19
- )
20
-
21
- # Handle API response
22
- if response.status_code == 200:
23
- result = response.json()
24
- return result["output_url"] # Return the output image URL from API
25
- else:
26
- return f"Error: {response.status_code} - {response.json()['error']}"
27
-
28
- except Exception as e:
29
- return f"Exception: {e}"
30
-
31
-
32
- # Define the Tryonn function without CSS
33
- def tryonn(person_image, garment_image):
34
- # Process the images using the Pixelcut API
35
- person_result = remove_bg(person_image)
36
- garment_result = remove_bg(garment_image)
37
 
38
- # If both images are processed successfully, combine them (mock-up display)
39
- if "Error" not in person_result and "Error" not in garment_result:
40
- return person_result, garment_result, " Success!"
41
  else:
42
- return person_result, garment_result, " Failed, check errors."
43
 
44
- # Gradio interface setup (CSS removed)
45
- with gr.Blocks() as Tryon:
46
- gr.Markdown("## Upload a Person Image and Garment Image")
47
- with gr.Row():
48
- person_input = gr.Image(type="filepath", label="Person Image")
49
- garment_input = gr.Image(type="filepath", label="Garment Image")
50
- output_person = gr.Image(label="Processed Person Image")
51
- output_garment = gr.Image(label="Processed Garment Image")
52
- status = gr.Textbox(label="Status")
53
-
54
- submit_button = gr.Button("Run Try-On")
55
-
56
- submit_button.click(
57
- fn=tryonn,
58
- inputs=[person_input, garment_input],
59
- outputs=[output_person, output_garment, status],
60
- )
61
 
62
- Tryon.launch()
63
 
64
 
65
  example_path = os.path.join(os.path.dirname(__file__), 'assets')
 
1
  import requests
2
+ import base64
3
 
4
+ # --- Configuration ---
5
  API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
6
+ PERSON_IMAGE_PATH = "path_to_person_image.jpg"
7
+ GARMENT_IMAGE_PATH = "path_to_garment_image.jpg"
8
+
9
+ # --- Helper Function: Read Image and Convert to Base64 ---
10
+ def encode_image(image_path):
11
+ with open(image_path, "rb") as image_file:
12
+ return base64.b64encode(image_file.read()).decode("utf-8")
13
+
14
+ # --- Step 1: Remove Background from Person Image ---
15
+ def remove_background(image_path):
16
+ with open(image_path, "rb") as image_file:
17
+ response = requests.post(
18
+ "https://api.developer.pixelcut.ai/v1/remove-background",
19
+ headers={
20
+ "Accept": "application/json",
21
+ "X-API-KEY": API_KEY,
22
+ },
23
+ files={"image": image_file},
24
+ data={"format": "png"},
25
+ )
26
 
27
+ if response.status_code == 200:
28
+ return response.json().get("url")
29
+ else:
30
+ print("Error:", response.json())
31
+ return None
32
+
33
+ # --- Step 2: Try-On Function ---
34
+ def tryonn(person_image_path, garment_image_path):
35
+ # Get background-removed URLs
36
+ person_url = remove_background(person_image_path)
37
+ garment_url = remove_background(garment_image_path)
38
+
39
+ if not person_url or not garment_url:
40
+ print("Failed to process images!")
41
+ return
42
+
43
+ # Make the try-on API call
44
+ tryon_response = requests.post(
45
+ "https://api.example.com/v1/tryon",
46
+ headers={"Accept": "application/json", "X-API-KEY": API_KEY},
47
+ json={"person_image_url": person_url, "garment_image_url": garment_url},
48
+ )
 
 
 
 
 
 
 
 
49
 
50
+ if tryon_response.status_code == 200:
51
+ tryon_result_url = tryon_response.json().get("result_url")
52
+ print("Try-On Success! Result:", tryon_result_url)
53
  else:
54
+ print("Try-On Failed:", tryon_response.json())
55
 
56
+ # --- Execute the try-on function ---
57
+ tryonn(PERSON_IMAGE_PATH, GARMENT_IMAGE_PATH)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
59
 
60
 
61
  example_path = os.path.join(os.path.dirname(__file__), 'assets')