WARAJA commited on
Commit
7070dcf
Β·
verified Β·
1 Parent(s): 22f555b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -51,14 +51,16 @@ MAX_DILATE_ITERS = 200
51
  _DIALECT_MAP = {"4-word (verbose)": FOUR_WORD, "3-word (classic)": THREE_WORD}
52
  _CASING_MAP = {"CAPS only": CAPS_ONLY, "Mixed case": MIXED_CASE}
53
 
54
- print("Pre-downloading Surya OCR weights to cache...")
55
  try:
56
  from huggingface_hub import snapshot_download
 
57
  local_surya_path = snapshot_download(repo_id="vikp/surya_det3", token=HF_TOKEN)
58
  os.environ["DETECTOR_MODEL_REPO"] = local_surya_path
59
- os.environ["HF_HUB_OFFLINE"] = "1"
 
60
  except Exception as e:
61
- print(f"Warning: Could not pre-download Surya weights. {e}")
62
 
63
  # ══════════════════════════════════════════════════════════════
64
  # 1. BINARIZATION
@@ -179,6 +181,8 @@ _surya_predictor = None
179
  def segment_lines_surya(img_input):
180
  global _surya_predictor
181
 
 
 
182
  # Convert numpy β†’ PIL safely
183
  if isinstance(img_input, np.ndarray):
184
  if len(img_input.shape) == 2:
@@ -193,50 +197,56 @@ def segment_lines_surya(img_input):
193
  if pil_image.mode != "RGB":
194
  pil_image = pil_image.convert("RGB")
195
 
196
- # Surya import
197
  try:
198
  from surya.detection import DetectionPredictor
199
  except ImportError:
 
200
  raise RuntimeError("surya-ocr not installed. Add 'surya-ocr' to requirements.txt.")
201
 
202
- # IMPORTANT: set thresholds BEFORE model creation
203
- os.environ["DETECTOR_TEXT_THRESHOLD"] = "0.75"
204
- os.environ["DETECTOR_BLANK_THRESHOLD"] = "0.45"
205
-
206
  # Lazy load predictor
207
  if _surya_predictor is None:
208
- _surya_predictor = DetectionPredictor()
209
-
 
 
 
 
 
 
 
 
 
 
210
  predictions = _surya_predictor([pil_image])
211
 
212
- if predictions is None or len(predictions) == 0:
 
213
  return []
214
 
215
  pred = predictions[0]
216
-
217
- # Surya version compatibility
218
  boxes = None
219
-
220
- if hasattr(pred, "bboxes") and pred.bboxes is not None:
221
- boxes = pred.bboxes
222
- elif hasattr(pred, "boxes") and pred.boxes is not None:
223
- boxes = pred.boxes
224
- elif hasattr(pred, "text_lines") and pred.text_lines is not None:
225
- boxes = pred.text_lines
226
 
227
  if boxes is None:
 
228
  return []
229
 
230
  CONF_THRESHOLD = 0.6
231
  raw = []
232
 
233
  for bbox in boxes:
234
-
235
  conf = getattr(bbox, "confidence", 1.0)
236
  if conf is not None and conf < CONF_THRESHOLD:
237
  continue
238
 
239
- # Different Surya bbox formats
240
  if hasattr(bbox, "bbox"):
241
  x1, y1, x2, y2 = bbox.bbox
242
  elif hasattr(bbox, "box"):
@@ -250,16 +260,17 @@ def segment_lines_surya(img_input):
250
  raw.append([float(x1), float(y1), float(x2), float(y2)])
251
 
252
  if not raw:
 
253
  return []
254
 
255
- # Sort by vertical center
256
  raw.sort(key=lambda b: (b[1] + b[3]) / 2)
257
 
258
  def overlaps_v(a, b):
 
259
  return a[1] < b[3] and b[1] < a[3]
260
 
261
  merged = []
262
-
263
  for box in raw:
264
  placed = False
265
  for m in merged:
@@ -270,17 +281,16 @@ def segment_lines_surya(img_input):
270
  m[3] = max(m[3], box[3])
271
  placed = True
272
  break
273
-
274
  if not placed:
275
  merged.append(list(box))
276
 
277
  merged.sort(key=lambda b: b[1])
 
278
 
279
  return [
280
  (int(b[0]), int(b[1]), int(b[2] - b[0]), int(b[3] - b[1]))
281
  for b in merged
282
- ]
283
- # ══════════════════════════════════════════════════════════════
284
  # 3. WORD SEGMENTATION
285
  # ══════════════════════════════════════════════════════════════
286
  def _get_word_boxes(dilated, min_w, min_h):
 
51
  _DIALECT_MAP = {"4-word (verbose)": FOUR_WORD, "3-word (classic)": THREE_WORD}
52
  _CASING_MAP = {"CAPS only": CAPS_ONLY, "Mixed case": MIXED_CASE}
53
 
54
+ print("--- Initializing Surya Setup ---")
55
  try:
56
  from huggingface_hub import snapshot_download
57
+ print(f"Attempting to download Surya weights from vikp/surya_det3...")
58
  local_surya_path = snapshot_download(repo_id="vikp/surya_det3", token=HF_TOKEN)
59
  os.environ["DETECTOR_MODEL_REPO"] = local_surya_path
60
+ # os.environ["HF_HUB_OFFLINE"] = "1" # Optional: disable if you want it to check for updates
61
+ print(f"Surya weights cached at: {local_surya_path}")
62
  except Exception as e:
63
+ print(f"Warning: Could not pre-download/cache Surya weights. {e}")
64
 
65
  # ══════════════════════════════════════════════════════════════
66
  # 1. BINARIZATION
 
181
  def segment_lines_surya(img_input):
182
  global _surya_predictor
183
 
184
+ print("\n[Surya Seg] Starting Line Segmentation...")
185
+
186
  # Convert numpy β†’ PIL safely
187
  if isinstance(img_input, np.ndarray):
188
  if len(img_input.shape) == 2:
 
197
  if pil_image.mode != "RGB":
198
  pil_image = pil_image.convert("RGB")
199
 
200
+ # Surya import check
201
  try:
202
  from surya.detection import DetectionPredictor
203
  except ImportError:
204
+ print("[Surya Seg] ERROR: 'surya-ocr' library not found.")
205
  raise RuntimeError("surya-ocr not installed. Add 'surya-ocr' to requirements.txt.")
206
 
 
 
 
 
207
  # Lazy load predictor
208
  if _surya_predictor is None:
209
+ print("[Surya Seg] Instantiating DetectionPredictor (this may take a moment)...")
210
+ try:
211
+ # Set thresholds immediately before creation
212
+ os.environ["DETECTOR_TEXT_THRESHOLD"] = "0.75"
213
+ os.environ["DETECTOR_BLANK_THRESHOLD"] = "0.45"
214
+ _surya_predictor = DetectionPredictor()
215
+ print("[Surya Seg] DetectionPredictor loaded successfully.")
216
+ except Exception as e:
217
+ print(f"[Surya Seg] ERROR during predictor init: {e}")
218
+ return []
219
+
220
+ print("[Surya Seg] Running inference on image...")
221
  predictions = _surya_predictor([pil_image])
222
 
223
+ if not predictions:
224
+ print("[Surya Seg] WARNING: No predictions returned from model.")
225
  return []
226
 
227
  pred = predictions[0]
228
+
229
+ # Robustly find the boxes in the prediction object
230
  boxes = None
231
+ for attr in ["bboxes", "boxes", "text_lines"]:
232
+ if hasattr(pred, attr) and getattr(pred, attr) is not None:
233
+ boxes = getattr(pred, attr)
234
+ print(f"[Surya Seg] Found {len(boxes)} raw boxes via attribute: {attr}")
235
+ break
 
 
236
 
237
  if boxes is None:
238
+ print("[Surya Seg] ERROR: Prediction object has no recognizable box attributes.")
239
  return []
240
 
241
  CONF_THRESHOLD = 0.6
242
  raw = []
243
 
244
  for bbox in boxes:
245
+ # Handle both object-based bboxes and list-based bboxes
246
  conf = getattr(bbox, "confidence", 1.0)
247
  if conf is not None and conf < CONF_THRESHOLD:
248
  continue
249
 
 
250
  if hasattr(bbox, "bbox"):
251
  x1, y1, x2, y2 = bbox.bbox
252
  elif hasattr(bbox, "box"):
 
260
  raw.append([float(x1), float(y1), float(x2), float(y2)])
261
 
262
  if not raw:
263
+ print("[Surya Seg] WARNING: No boxes passed size/confidence filters.")
264
  return []
265
 
266
+ # Sort and merge logic
267
  raw.sort(key=lambda b: (b[1] + b[3]) / 2)
268
 
269
  def overlaps_v(a, b):
270
+ # Vertical overlap check to group fragments of the same line
271
  return a[1] < b[3] and b[1] < a[3]
272
 
273
  merged = []
 
274
  for box in raw:
275
  placed = False
276
  for m in merged:
 
281
  m[3] = max(m[3], box[3])
282
  placed = True
283
  break
 
284
  if not placed:
285
  merged.append(list(box))
286
 
287
  merged.sort(key=lambda b: b[1])
288
+ print(f"[Surya Seg] Successfully processed {len(merged)} lines.")
289
 
290
  return [
291
  (int(b[0]), int(b[1]), int(b[2] - b[0]), int(b[3] - b[1]))
292
  for b in merged
293
+ ]# ══════════════════════════════════════════════════════════════
 
294
  # 3. WORD SEGMENTATION
295
  # ══════════════════════════════════════════════════════════════
296
  def _get_word_boxes(dilated, min_w, min_h):