Prathamesh Sable commited on
Commit
42b2d96
·
2 Parent(s): d93ddcb 45d3dc3

Merge branch 'main' of personal:prathameshks/FoodAnalyzer-API

Browse files
Files changed (1) hide show
  1. routers/analysis.py +11 -36
routers/analysis.py CHANGED
@@ -35,7 +35,7 @@ log_info(f"Using parallel rate limit of {PARALLEL_RATE_LIMIT}")
35
  llm_semaphore = asyncio.Semaphore(PARALLEL_RATE_LIMIT)
36
 
37
  # Load YOLO model
38
- yolo_model = YOLO("yolov8n-seg.pt") # Downloaded automatically if needed
39
  UPLOADED_IMAGES_DIR = "uploaded_images"
40
  if not os.path.exists(UPLOADED_IMAGES_DIR):
41
  os.makedirs(UPLOADED_IMAGES_DIR)
@@ -62,12 +62,8 @@ def extract_product_from_image_yolo(image_path: str) -> str | None:
62
  image = cv2.imread(image_path)
63
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
64
 
65
- # Preprocessing: Resize image
66
- target_size = (640, 640)
67
- image_resized = cv2.resize(image, target_size, interpolation=cv2.INTER_CUBIC)
68
- original_height, original_width = image.shape[:2]
69
  # Run inference with YOLO
70
- results = yolo_model(image_resized,conf=0.2,show=True)
71
 
72
  if not results:
73
  print("No objects detected by YOLO.")
@@ -75,43 +71,22 @@ def extract_product_from_image_yolo(image_path: str) -> str | None:
75
 
76
  # Process results
77
  result = results[0]
78
- masks = result.masks
79
 
80
- if masks is None or len(masks.data) == 0:
81
- print("No segmentation masks found by YOLO.")
82
  return None
83
-
84
- # Select the largest mask
85
- mask_areas = [cv2.contourArea(masks.xy[i]) for i in range(len(masks))]
86
- largest_mask_index = np.argmax(mask_areas)
87
- largest_mask_tensor = masks.data[largest_mask_index].cpu()
88
- largest_mask = largest_mask_tensor.numpy().astype(np.uint8)
89
-
90
- # Resize the mask to the original image size
91
- largest_mask = cv2.resize(largest_mask, (original_width, original_height))
92
-
93
- # Postprocessing: Basic mask cleanup (dilation/erosion)
94
- kernel = np.ones((3, 3), np.uint8)
95
- mask_cleaned = cv2.dilate(largest_mask, kernel, iterations=1)
96
- mask_cleaned = cv2.erode(mask_cleaned, kernel, iterations=1)
97
 
98
- # Create a masked image
99
- masked_image = np.zeros_like(image)
100
- masked_image[mask_cleaned.astype(bool)] = image[mask_cleaned.astype(bool)]
101
 
102
  # Crop the image
103
- y_coords, x_coords = np.where(mask_cleaned)
104
- x_min, x_max = np.min(x_coords), np.max(x_coords)
105
- y_min, y_max = np.min(y_coords), np.max(y_coords)
106
- cropped_image = masked_image[y_min:y_max, x_min:x_max]
107
-
108
- # sharpen the image
109
- sharpen_kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
110
- cropped_image_sharpened = cv2.filter2D(cropped_image, -1, sharpen_kernel)
111
 
112
  # Save the cropped image
113
  cropped_image_path = os.path.join(UPLOADED_IMAGES_DIR, f"{uuid.uuid4()}.jpg")
114
- cropped_image_bgr = cv2.cvtColor(cropped_image_sharpened, cv2.COLOR_RGB2BGR)
115
  cv2.imwrite(cropped_image_path, cropped_image_bgr)
116
 
117
  return cropped_image_path
@@ -167,7 +142,7 @@ async def get_image(image_name: str):
167
  return FileResponse(image_path, media_type="image/jpeg")
168
  else:
169
  return JSONResponse({"error": "Image not found"}, status_code=404)
170
-
171
  # process single ingredient
172
  @router.post("/process_ingredient", response_model=IngredientAnalysisResult)
173
  @traceable
 
35
  llm_semaphore = asyncio.Semaphore(PARALLEL_RATE_LIMIT)
36
 
37
  # Load YOLO model
38
+ yolo_model = YOLO("yolov8n.pt") # Downloaded automatically if needed
39
  UPLOADED_IMAGES_DIR = "uploaded_images"
40
  if not os.path.exists(UPLOADED_IMAGES_DIR):
41
  os.makedirs(UPLOADED_IMAGES_DIR)
 
62
  image = cv2.imread(image_path)
63
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
64
 
 
 
 
 
65
  # Run inference with YOLO
66
+ results = yolo_model(image, conf=0.2) # lower confidence for detection
67
 
68
  if not results:
69
  print("No objects detected by YOLO.")
 
71
 
72
  # Process results
73
  result = results[0]
74
+ boxes = result.boxes
75
 
76
+ if len(boxes) == 0:
77
+ print("No objects detected by YOLO.")
78
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ # Get the box with the highest confidence
81
+ box = boxes[0].xyxy[0].cpu().numpy().astype(int)
82
+ x_min, y_min, x_max, y_max = box
83
 
84
  # Crop the image
85
+ cropped_image = image[y_min:y_max, x_min:x_max]
 
 
 
 
 
 
 
86
 
87
  # Save the cropped image
88
  cropped_image_path = os.path.join(UPLOADED_IMAGES_DIR, f"{uuid.uuid4()}.jpg")
89
+ cropped_image_bgr = cv2.cvtColor(cropped_image, cv2.COLOR_RGB2BGR)
90
  cv2.imwrite(cropped_image_path, cropped_image_bgr)
91
 
92
  return cropped_image_path
 
142
  return FileResponse(image_path, media_type="image/jpeg")
143
  else:
144
  return JSONResponse({"error": "Image not found"}, status_code=404)
145
+
146
  # process single ingredient
147
  @router.post("/process_ingredient", response_model=IngredientAnalysisResult)
148
  @traceable