Wills17 commited on
Commit
f3b4f95
·
verified ·
1 Parent(s): ce8b075

Update FastAPI_app.py

Browse files
Files changed (1) hide show
  1. FastAPI_app.py +9 -8
FastAPI_app.py CHANGED
@@ -10,6 +10,7 @@ import asyncio
10
 
11
  import uvicorn
12
  import numpy as np
 
13
  from PIL import Image
14
  from fastapi import FastAPI, Form, UploadFile, File, Request, HTTPException
15
  from fastapi.responses import HTMLResponse
@@ -142,16 +143,16 @@ def generate_recipe_qwen(ingredient_names):
142
  # YOLOv8 model for ingredient detection
143
  def infer_image(pil_image):
144
 
145
- # Convert PIL to RGB numpy array (YOLOv8 prefers this)
146
- img_array = np.array(pil_image.convert('RGB')) # Force RGB, no alpha
 
147
 
148
- # Resize with PIL (safe and fast)
149
- pil_resized = pil_image.convert('RGB').resize((640, 640), Image.Resampling.LANCZOS)
150
- img_resized = np.array(pil_resized)
151
 
152
  # Inference with low threshold
153
- results = _yolo_model(img_resized, conf=0.2, iou=0.45, verbose=False)[0]
154
-
155
  detected = []
156
 
157
  if results.boxes is not None and len(results.boxes) > 0:
@@ -347,4 +348,4 @@ def health():
347
 
348
  # Run app
349
  if __name__ == "__main__":
350
- uvicorn.run("FastAPI_app:app", host="0.0.0.0", port=7860)
 
10
 
11
  import uvicorn
12
  import numpy as np
13
+ import cv2 as cv
14
  from PIL import Image
15
  from fastapi import FastAPI, Form, UploadFile, File, Request, HTTPException
16
  from fastapi.responses import HTMLResponse
 
143
  # YOLOv8 model for ingredient detection
144
  def infer_image(pil_image):
145
 
146
+ # Convert PIL OpenCV format
147
+ open_cv_image = np.array(pil_image)
148
+ open_cv_image = open_cv_image[:, :, ::-1].copy() # RGB → BGR
149
 
150
+ # Resize to 640x640, YOLOv8 default
151
+ img = cv.resize(open_cv_image, (640, 640))
 
152
 
153
  # Inference with low threshold
154
+ results = _yolo_model(img, conf=0.2, iou=0.45, verbose=False)[0]
155
+
156
  detected = []
157
 
158
  if results.boxes is not None and len(results.boxes) > 0:
 
348
 
349
  # Run app
350
  if __name__ == "__main__":
351
+ uvicorn.run("FastAPI_app:app", host="0.0.0.0", port=7860, reload=True)