jocoandonob commited on
Commit
67896be
·
1 Parent(s): ba01344
Files changed (2) hide show
  1. app.py +54 -2
  2. requirements.txt +3 -0
app.py CHANGED
@@ -1,11 +1,25 @@
1
  import os
2
-
 
 
3
  import cv2
4
  import gradio as gr
5
  import mediapipe as mp
6
  import numpy as np
7
  from PIL import Image
8
  from gradio_client import Client, handle_file
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  example_path = os.path.join(os.path.dirname(__file__), 'example')
11
 
@@ -132,6 +146,40 @@ def process_image(human_img_path, garm_img_path):
132
  return generated_image
133
 
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  image_blocks = gr.Blocks().queue()
136
  with image_blocks as demo:
137
  gr.HTML("<center><h1>Virtual Try-On</h1></center>")
@@ -160,4 +208,8 @@ with image_blocks as demo:
160
  # Linking the button to the processing function
161
  try_button.click(fn=process_image, inputs=[human_img, garm_img], outputs=image_out)
162
 
163
- image_blocks.launch(show_error=True)
 
 
 
 
 
1
  import os
2
+ from fastapi import FastAPI, File, UploadFile
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import uvicorn
5
  import cv2
6
  import gradio as gr
7
  import mediapipe as mp
8
  import numpy as np
9
  from PIL import Image
10
  from gradio_client import Client, handle_file
11
+ import io
12
+
13
+ app = FastAPI()
14
+
15
+ # Add CORS middleware
16
+ app.add_middleware(
17
+ CORSMiddleware,
18
+ allow_origins=["*"],
19
+ allow_credentials=True,
20
+ allow_methods=["*"],
21
+ allow_headers=["*"],
22
+ )
23
 
24
  example_path = os.path.join(os.path.dirname(__file__), 'example')
25
 
 
146
  return generated_image
147
 
148
 
149
+ @app.post("/")
150
+ async def try_on_api(human_image: UploadFile = File(...), garment_image: UploadFile = File(...)):
151
+ try:
152
+ # Read the uploaded files
153
+ human_content = await human_image.read()
154
+ garment_content = await garment_image.read()
155
+
156
+ # Convert to PIL Image
157
+ human_img = Image.open(io.BytesIO(human_content))
158
+ garment_img = Image.open(io.BytesIO(garment_content))
159
+
160
+ # Save temporarily to process
161
+ human_path = "temp_human.jpg"
162
+ garment_path = "temp_garment.jpg"
163
+ human_img.save(human_path)
164
+ garment_img.save(garment_path)
165
+
166
+ # Process the images
167
+ result = process_image(human_path, garment_path)
168
+
169
+ # Convert result to bytes
170
+ img_byte_arr = io.BytesIO()
171
+ result.save(img_byte_arr, format='PNG')
172
+ img_byte_arr = img_byte_arr.getvalue()
173
+
174
+ # Clean up temporary files
175
+ os.remove(human_path)
176
+ os.remove(garment_path)
177
+
178
+ return {"status": "success", "image": img_byte_arr}
179
+ except Exception as e:
180
+ return {"status": "error", "message": str(e)}
181
+
182
+ # Create the Gradio interface
183
  image_blocks = gr.Blocks().queue()
184
  with image_blocks as demo:
185
  gr.HTML("<center><h1>Virtual Try-On</h1></center>")
 
208
  # Linking the button to the processing function
209
  try_button.click(fn=process_image, inputs=[human_img, garm_img], outputs=image_out)
210
 
211
+ # Mount Gradio app
212
+ app = gr.mount_gradio_app(app, demo, path="/gradio")
213
+
214
+ if __name__ == "__main__":
215
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -4,4 +4,7 @@ opencv-contrib-python==4.11.0.86
4
  opencv-python==4.11.0.86
5
  gradio==5.23.3
6
  gradio_client==1.8.0
 
 
 
7
 
 
4
  opencv-python==4.11.0.86
5
  gradio==5.23.3
6
  gradio_client==1.8.0
7
+ fastapi==0.110.0
8
+ uvicorn==0.27.1
9
+ python-multipart==0.0.9
10