WARAJA commited on
Commit
e9e8a33
Β·
verified Β·
1 Parent(s): 5f5b2c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -161,10 +161,12 @@ def segment_lines_yolo(bin_arr, yolo_model):
161
 
162
  _surya_predictor = None
163
 
164
- def segment_lines_surya(bin_arr):
165
  global _surya_predictor
166
- os.environ.setdefault("DETECTOR_TEXT_THRESHOLD", "0.75")
167
- os.environ.setdefault("DETECTOR_BLANK_THRESHOLD", "0.45")
 
 
168
 
169
  try:
170
  from surya.detection import DetectionPredictor
@@ -174,16 +176,18 @@ def segment_lines_surya(bin_arr):
174
  if _surya_predictor is None:
175
  _surya_predictor = DetectionPredictor()
176
 
177
- img_rgb = cv2.cvtColor(bin_arr, cv2.COLOR_GRAY2RGB) if len(bin_arr.shape) == 2 else bin_arr
178
- pil_image = Image.fromarray(img_rgb)
 
 
179
  predictions = _surya_predictor([pil_image])
180
 
181
- CONF_THRESHOLD = 0.6
182
  raw = []
183
  if predictions and predictions[0].bboxes:
184
  for bbox in predictions[0].bboxes:
185
  conf = getattr(bbox, "confidence", 1.0)
186
- if conf < CONF_THRESHOLD:
187
  continue
188
  x1, y1, x2, y2 = bbox.bbox
189
  if (x2 - x1) > 5 and (y2 - y1) > 5:
@@ -192,6 +196,7 @@ def segment_lines_surya(bin_arr):
192
  raw.sort(key=lambda b: (b[1] + b[3]) / 2)
193
 
194
  def overlaps_v(a, b):
 
195
  return a[1] < b[3] and b[1] < a[3]
196
 
197
  merged = []
@@ -205,9 +210,9 @@ def segment_lines_surya(bin_arr):
205
  if not placed:
206
  merged.append(list(box))
207
 
 
208
  merged.sort(key=lambda b: b[1])
209
  return [(int(b[0]), int(b[1]), int(b[2]-b[0]), int(b[3]-b[1])) for b in merged]
210
-
211
  # ══════════════════════════════════════════════════════════════
212
  # 3. WORD SEGMENTATION
213
  # ══════════════════════════════════════════════════════════════
 
161
 
162
  _surya_predictor = None
163
 
164
+ def segment_lines_surya(pil_image):
165
  global _surya_predictor
166
+
167
+ # 1. Ensure the image is exactly RGB as Surya expects (like your local script)
168
+ if pil_image.mode != "RGB":
169
+ pil_image = pil_image.convert("RGB")
170
 
171
  try:
172
  from surya.detection import DetectionPredictor
 
176
  if _surya_predictor is None:
177
  _surya_predictor = DetectionPredictor()
178
 
179
+ # Set thresholds globally right before inference (replicating your local os.environ setup)
180
+ os.environ["DETECTOR_TEXT_THRESHOLD"] = "0.75"
181
+ os.environ["DETECTOR_BLANK_THRESHOLD"] = "0.45"
182
+
183
  predictions = _surya_predictor([pil_image])
184
 
185
+ CONF_THRESHOLD = 0.6 # Matching your local script
186
  raw = []
187
  if predictions and predictions[0].bboxes:
188
  for bbox in predictions[0].bboxes:
189
  conf = getattr(bbox, "confidence", 1.0)
190
+ if conf is not None and conf < CONF_THRESHOLD:
191
  continue
192
  x1, y1, x2, y2 = bbox.bbox
193
  if (x2 - x1) > 5 and (y2 - y1) > 5:
 
196
  raw.sort(key=lambda b: (b[1] + b[3]) / 2)
197
 
198
  def overlaps_v(a, b):
199
+ # Your local script uses this exact simple overlap logic
200
  return a[1] < b[3] and b[1] < a[3]
201
 
202
  merged = []
 
210
  if not placed:
211
  merged.append(list(box))
212
 
213
+ # Sort top-to-bottom, output as [x, y, w, h] (Matching your local script output format)
214
  merged.sort(key=lambda b: b[1])
215
  return [(int(b[0]), int(b[1]), int(b[2]-b[0]), int(b[3]-b[1])) for b in merged]
 
216
  # ══════════════════════════════════════════════════════════════
217
  # 3. WORD SEGMENTATION
218
  # ══════════════════════════════════════════════════════════════