heerjtdev commited on
Commit
d5df69a
·
verified ·
1 Parent(s): 4a1e588

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -40,6 +40,7 @@ logging.basicConfig(level=logging.WARNING)
40
 
41
  WEIGHTS_PATH = 'best.pt'
42
  SCALE_FACTOR = 2.0
 
43
 
44
  # Detection parameters
45
  CONF_THRESHOLD = 0.2
@@ -229,6 +230,26 @@ def crop_and_convert_to_base64(image: np.ndarray, bbox: Tuple[float, float, floa
229
 
230
 
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
 
234
 
@@ -237,15 +258,14 @@ def run_yolo_detection_and_count(
237
  image: np.ndarray, model: YOLO, page_num: int
238
  ) -> Tuple[int, int, List[str]]:
239
  """
240
- Runs YOLO inference and returns:
241
- equations, figures, list of base64 images
242
  """
243
  global GLOBAL_FIGURE_COUNT, GLOBAL_EQUATION_COUNT
244
-
245
  yolo_detections = []
246
  page_equations = 0
247
  page_figures = 0
248
- cropped_images_base64 = []
249
 
250
  try:
251
  results = model.predict(image, conf=CONF_THRESHOLD, verbose=False)
@@ -269,22 +289,25 @@ def run_yolo_detection_and_count(
269
  final_detections = filter_nested_boxes(merged_detections, IOA_SUPPRESSION_THRESHOLD)
270
 
271
  for det in final_detections:
272
- bbox = det['coords']
273
-
274
- if det['class'] == 'figure':
275
- GLOBAL_FIGURE_COUNT += 1
276
- page_figures += 1
277
 
278
- elif det['class'] == 'equation':
279
  GLOBAL_EQUATION_COUNT += 1
280
  page_equations += 1
281
 
282
- # Only crop equations (you can include figures too if needed)
283
- b64_img = crop_and_convert_to_base64(image, bbox)
284
- cropped_images_base64.append(b64_img)
 
 
 
 
 
 
285
 
286
  logging.warning(f" -> Page {page_num}: EQs={page_equations}, Figs={page_figures}")
287
- return page_equations, page_figures, cropped_images_base64
 
288
 
289
 
290
 
 
40
 
41
  WEIGHTS_PATH = 'best.pt'
42
  SCALE_FACTOR = 2.0
43
+ OUTPUT_DIR = "yolo_extracted_regions"
44
 
45
  # Detection parameters
46
  CONF_THRESHOLD = 0.2
 
230
 
231
 
232
 
233
+ def crop_and_save(image: np.ndarray, bbox, label: str, index: int) -> str:
234
+ """Crop bounding box and save to disk. Return file path."""
235
+ x1, y1, x2, y2 = map(int, bbox)
236
+
237
+ h, w, _ = image.shape
238
+ x1 = max(0, x1)
239
+ y1 = max(0, y1)
240
+ x2 = min(w, x2)
241
+ y2 = min(h, y2)
242
+
243
+ crop = image[y1:y2, x1:x2]
244
+ filename = f"{label}{index}.png"
245
+ filepath = os.path.join(OUTPUT_DIR, filename)
246
+
247
+ cv2.imwrite(filepath, crop)
248
+
249
+ return filepath
250
+
251
+
252
+
253
 
254
 
255
 
 
258
  image: np.ndarray, model: YOLO, page_num: int
259
  ) -> Tuple[int, int, List[str]]:
260
  """
261
+ Runs YOLO inference, saves crops, and returns file paths.
 
262
  """
263
  global GLOBAL_FIGURE_COUNT, GLOBAL_EQUATION_COUNT
264
+
265
  yolo_detections = []
266
  page_equations = 0
267
  page_figures = 0
268
+ saved_images = []
269
 
270
  try:
271
  results = model.predict(image, conf=CONF_THRESHOLD, verbose=False)
 
289
  final_detections = filter_nested_boxes(merged_detections, IOA_SUPPRESSION_THRESHOLD)
290
 
291
  for det in final_detections:
292
+ bbox = det["coords"]
 
 
 
 
293
 
294
+ if det["class"] == "equation":
295
  GLOBAL_EQUATION_COUNT += 1
296
  page_equations += 1
297
 
298
+ path = crop_and_save(image, bbox, "EQUATION", GLOBAL_EQUATION_COUNT)
299
+ saved_images.append(path)
300
+
301
+ elif det["class"] == "figure":
302
+ GLOBAL_FIGURE_COUNT += 1
303
+ page_figures += 1
304
+
305
+ path = crop_and_save(image, bbox, "FIGURE", GLOBAL_FIGURE_COUNT)
306
+ saved_images.append(path)
307
 
308
  logging.warning(f" -> Page {page_num}: EQs={page_equations}, Figs={page_figures}")
309
+ return page_equations, page_figures, saved_images
310
+
311
 
312
 
313