Hammad712 commited on
Commit
fe3b199
·
verified ·
1 Parent(s): 064e50b

Update app/api/image_router.py

Browse files
Files changed (1) hide show
  1. app/api/image_router.py +15 -11
app/api/image_router.py CHANGED
@@ -119,44 +119,47 @@ async def update_image(
119
  raise HTTPException(status_code=500, detail=str(e))
120
 
121
 
122
- # --- New Virtual Try-On Endpoint with File Uploads ---
123
  @router.post("/virtual-try-on")
124
  async def virtual_try_on(
125
- dress_image: UploadFile = File(..., description="The dress image (Input 1)"),
126
- person_image: UploadFile = File(..., description="The person image (Input 2)"),
127
- shoes_image: UploadFile = File(None, description="The shoes image (Input 3) - Optional")
128
- ) -> Response:
129
  """
130
- Performs a virtual try-on.
131
- Accepts: Dress, Person, and optionally Shoes.
132
  """
133
  logger.info("Received request to /virtual-try-on with image uploads.")
134
 
135
- # Validate mandatory images
136
  if not dress_image.content_type.startswith("image/"):
137
  raise HTTPException(status_code=400, detail="Dress image must be an image type.")
138
  if not person_image.content_type.startswith("image/"):
139
  raise HTTPException(status_code=400, detail="Person image must be an image type.")
140
 
141
- # Validate shoes only if provided
 
142
  if shoes_image and not shoes_image.content_type.startswith("image/"):
143
  raise HTTPException(status_code=400, detail="Shoes image must be an image type.")
144
 
145
  try:
 
146
  dress_image_bytes = await dress_image.read()
147
  person_image_bytes = await person_image.read()
148
 
149
  shoes_image_bytes = None
150
  if shoes_image:
151
- shoes_image_bytes = await shoes_image.read()
152
 
153
- # Service returns JSON summary (dict) and BytesIO object
154
  summary, try_on_image_bytes_io = image_service.virtual_try_on(
155
  dress_image_bytes=dress_image_bytes,
156
  person_image_bytes=person_image_bytes,
157
  shoes_image_bytes=shoes_image_bytes
158
  )
159
 
 
160
  if try_on_image_bytes_io:
161
  logger.info("Virtual try-on successful. Streaming image response.")
162
  return create_image_streaming_response(try_on_image_bytes_io)
@@ -170,6 +173,7 @@ async def virtual_try_on(
170
  logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
171
  raise HTTPException(status_code=500, detail=str(e))
172
 
 
173
  @router.post("/generate-shoe", response_class=StreamingResponse)
174
  async def generate_shoe(request: ShoeGenerateRequest):
175
  """
 
119
  raise HTTPException(status_code=500, detail=str(e))
120
 
121
 
122
+ # --- Corrected Virtual Try-On Endpoint with 3 Inputs ---
123
  @router.post("/virtual-try-on")
124
  async def virtual_try_on(
125
+ dress_image: UploadFile = File(..., description="The dress image for try-on (PNG, JPG)"),
126
+ person_image: UploadFile = File(..., description="The person image for try-on (PNG, JPG)"),
127
+ shoes_image: Optional[UploadFile] = File(None, description="The shoes image for try-on (PNG, JPG)")
128
+ ) -> Response:
129
  """
130
+ Performs a virtual try-on using dress, person, and optional shoes.
131
+ Returns the try-on image directly or a JSON response with a summary if no image.
132
  """
133
  logger.info("Received request to /virtual-try-on with image uploads.")
134
 
135
+ # 1. Validate mandatory images
136
  if not dress_image.content_type.startswith("image/"):
137
  raise HTTPException(status_code=400, detail="Dress image must be an image type.")
138
  if not person_image.content_type.startswith("image/"):
139
  raise HTTPException(status_code=400, detail="Person image must be an image type.")
140
 
141
+ # 2. Validate optional shoes
142
+ # Note: We check 'shoes_image' is not None before checking content_type
143
  if shoes_image and not shoes_image.content_type.startswith("image/"):
144
  raise HTTPException(status_code=400, detail="Shoes image must be an image type.")
145
 
146
  try:
147
+ # 3. Read bytes
148
  dress_image_bytes = await dress_image.read()
149
  person_image_bytes = await person_image.read()
150
 
151
  shoes_image_bytes = None
152
  if shoes_image:
153
+ shoes_image_bytes = await shoes_image.read()
154
 
155
+ # 4. Call Service
156
  summary, try_on_image_bytes_io = image_service.virtual_try_on(
157
  dress_image_bytes=dress_image_bytes,
158
  person_image_bytes=person_image_bytes,
159
  shoes_image_bytes=shoes_image_bytes
160
  )
161
 
162
+ # 5. Return Result
163
  if try_on_image_bytes_io:
164
  logger.info("Virtual try-on successful. Streaming image response.")
165
  return create_image_streaming_response(try_on_image_bytes_io)
 
173
  logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
174
  raise HTTPException(status_code=500, detail=str(e))
175
 
176
+
177
  @router.post("/generate-shoe", response_class=StreamingResponse)
178
  async def generate_shoe(request: ShoeGenerateRequest):
179
  """