Hammad712 commited on
Commit
bbabac5
·
verified ·
1 Parent(s): 9c4b494

Update app/api/image_router.py

Browse files
Files changed (1) hide show
  1. app/api/image_router.py +19 -11
app/api/image_router.py CHANGED
@@ -122,37 +122,46 @@ async def update_image(
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 for try-on (PNG, JPG)"),
126
- person_image: UploadFile = File(..., description="The person image for try-on (PNG, JPG)")
127
- ) -> Response: # Return type is Response because it can be JSON or Image
 
128
  """
129
- Performs a virtual try-on using an uploaded dress image and a person image.
130
- Returns the try-on image directly or a JSON response with a summary if no image.
131
  """
132
  logger.info("Received request to /virtual-try-on with image uploads.")
133
 
 
134
  if not dress_image.content_type.startswith("image/"):
135
- raise HTTPException(status_code=400, detail="Dress image must be an image type (e.g., image/png, image/jpeg).")
136
  if not person_image.content_type.startswith("image/"):
137
- raise HTTPException(status_code=400, detail="Person image must be an image type (e.g., image/png, image/jpeg).")
 
 
 
 
138
 
139
  try:
140
  dress_image_bytes = await dress_image.read()
141
  person_image_bytes = await person_image.read()
 
 
 
 
142
 
143
  # Service returns JSON summary (dict) and BytesIO object
144
  summary, try_on_image_bytes_io = image_service.virtual_try_on(
145
  dress_image_bytes=dress_image_bytes,
146
- person_image_bytes=person_image_bytes
 
147
  )
148
 
149
  if try_on_image_bytes_io:
150
  logger.info("Virtual try-on successful. Streaming image response.")
151
- # If an image is returned, stream it directly
152
  return create_image_streaming_response(try_on_image_bytes_io)
153
  else:
154
  logger.warning("Virtual try-on returned no image. Sending JSON summary.")
155
- # If no image, return the JSON summary as a JSON response
156
  return JSONResponse(content=summary)
157
 
158
  except HTTPException:
@@ -160,7 +169,6 @@ async def virtual_try_on(
160
  except Exception as e:
161
  logger.error(f"Error in /virtual-try-on: {e}", exc_info=True)
162
  raise HTTPException(status_code=500, detail=str(e))
163
-
164
 
165
  @router.post("/generate-shoe", response_class=StreamingResponse)
166
  async def generate_shoe(request: ShoeGenerateRequest):
 
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)
163
  else:
164
  logger.warning("Virtual try-on returned no image. Sending JSON summary.")
 
165
  return JSONResponse(content=summary)
166
 
167
  except HTTPException:
 
169
  except Exception as e:
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):