Prathamesh Sable commited on
Commit
534624d
·
1 Parent(s): 06674a9
Files changed (2) hide show
  1. main.py +2 -1
  2. routers/product.py +14 -5
main.py CHANGED
@@ -38,6 +38,7 @@ def read_root():
38
  return RedirectResponse("/api")
39
 
40
  # print every request data for request using middleware
 
41
  @app.middleware("http")
42
  async def log_requests(request: Request, call_next):
43
  # Store the body content before sending to the next handler
@@ -46,7 +47,7 @@ async def log_requests(request: Request, call_next):
46
  request._body = body_content
47
  response = await call_next(request)
48
  print(f"Request: {request.method} {request.url}")
49
- # print(f"Data: {body_content}")
50
  print(f"Headers: {request.headers}")
51
  return response
52
 
 
38
  return RedirectResponse("/api")
39
 
40
  # print every request data for request using middleware
41
+
42
  @app.middleware("http")
43
  async def log_requests(request: Request, call_next):
44
  # Store the body content before sending to the next handler
 
47
  request._body = body_content
48
  response = await call_next(request)
49
  print(f"Request: {request.method} {request.url}")
50
+ print(f"Data: {body_content}")
51
  print(f"Headers: {request.headers}")
52
  return response
53
 
routers/product.py CHANGED
@@ -164,14 +164,14 @@ async def create_product(
164
 
165
 
166
  @router.post("/process_image")
167
- async def process_image_endpoint(file: UploadFile = File(...), db: Session = Depends(get_db), request: Request = None):
168
  """
169
  Receives an image file, performs object detection, and returns information about detected objects.
170
  """
171
  log_info("Process image endpoint called")
172
  try:
173
  # Read image from the uploaded file
174
- image_data = await file.read()
175
  image = Image.open(io.BytesIO(image_data)).convert("RGB")
176
 
177
  # Run object detection with the request object
@@ -179,8 +179,16 @@ async def process_image_endpoint(file: UploadFile = File(...), db: Session = Dep
179
 
180
  # Get filtered class boxes
181
  box, class_name, score = get_filtered_class_boxes(results)
182
-
183
- # Crop the detected object
 
 
 
 
 
 
 
 
184
  cropped_img = crop_image(image_np, box)
185
 
186
  # Save the cropped image temporarily
@@ -194,7 +202,8 @@ async def process_image_endpoint(file: UploadFile = File(...), db: Session = Dep
194
  return JSONResponse({
195
  "class_name": class_name,
196
  "score": float(score),
197
- "image_name": cropped_image_name
 
198
  })
199
  except Exception as e:
200
  log_error(f"Error processing image: {e}", e)
 
164
 
165
 
166
  @router.post("/process_image")
167
+ async def process_image_endpoint(image: UploadFile = File(...), db: Session = Depends(get_db), request: Request = None):
168
  """
169
  Receives an image file, performs object detection, and returns information about detected objects.
170
  """
171
  log_info("Process image endpoint called")
172
  try:
173
  # Read image from the uploaded file
174
+ image_data = await image.read()
175
  image = Image.open(io.BytesIO(image_data)).convert("RGB")
176
 
177
  # Run object detection with the request object
 
179
 
180
  # Get filtered class boxes
181
  box, class_name, score = get_filtered_class_boxes(results)
182
+
183
+ # Check if any objects were detected
184
+ if box is None:
185
+ log_info("No food objects detected in image")
186
+ return JSONResponse({
187
+ "error": "No food objects detected in the image",
188
+ "detected": False
189
+ }, status_code=400)
190
+
191
+ # Crop the detected object
192
  cropped_img = crop_image(image_np, box)
193
 
194
  # Save the cropped image temporarily
 
202
  return JSONResponse({
203
  "class_name": class_name,
204
  "score": float(score),
205
+ "image_name": cropped_image_name,
206
+ "detected": True
207
  })
208
  except Exception as e:
209
  log_error(f"Error processing image: {e}", e)