lulavc commited on
Commit
e625573
·
verified ·
1 Parent(s): 37ef455

Fix bbox validation and strip GLM tokens

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -294,6 +294,9 @@ CRITICAL: Find at least 20-50 text regions. This image has many text elements. S
294
  # Append reasoning content if present
295
  result_text = result_text + "\n" + msg.reasoning_content if result_text else msg.reasoning_content
296
 
 
 
 
297
  print(f"📝 GLM Response length: {len(result_text)} chars")
298
  print(f"📝 GLM Response preview: {result_text[:500] if result_text else 'EMPTY'}...")
299
 
@@ -331,12 +334,22 @@ def create_text_mask(image: Image.Image, detections: list, padding: int = 12) ->
331
  bbox = det.get('bbox', [])
332
  if len(bbox) == 4:
333
  x1, y1, x2, y2 = [int(v) for v in bbox]
 
 
 
 
 
 
 
 
334
  # Larger padding for cleaner inpainting
335
  x1 = max(0, x1 - padding)
336
  y1 = max(0, y1 - padding)
337
  x2 = min(image.width, x2 + padding)
338
  y2 = min(image.height, y2 + padding)
339
- draw.rectangle([x1, y1, x2, y2], fill=255)
 
 
340
 
341
  return mask
342
 
@@ -386,10 +399,15 @@ def add_translated_text(image: Image.Image, detections: list) -> Image.Image:
386
 
387
  if len(bbox) == 4 and translated:
388
  x1, y1, x2, y2 = [int(v) for v in bbox]
 
 
 
 
 
389
  box_width = x2 - x1
390
  box_height = y2 - y1
391
 
392
- # Skip very small boxes
393
  if box_width < 20 or box_height < 10:
394
  continue
395
 
 
294
  # Append reasoning content if present
295
  result_text = result_text + "\n" + msg.reasoning_content if result_text else msg.reasoning_content
296
 
297
+ # Strip GLM special tokens
298
+ result_text = result_text.replace('<|begin_of_box|>', '').replace('<|end_of_box|>', '')
299
+
300
  print(f"📝 GLM Response length: {len(result_text)} chars")
301
  print(f"📝 GLM Response preview: {result_text[:500] if result_text else 'EMPTY'}...")
302
 
 
334
  bbox = det.get('bbox', [])
335
  if len(bbox) == 4:
336
  x1, y1, x2, y2 = [int(v) for v in bbox]
337
+ # Ensure coordinates are valid (x2 > x1, y2 > y1)
338
+ if x2 < x1:
339
+ x1, x2 = x2, x1
340
+ if y2 < y1:
341
+ y1, y2 = y2, y1
342
+ # Skip invalid boxes
343
+ if x2 <= x1 or y2 <= y1:
344
+ continue
345
  # Larger padding for cleaner inpainting
346
  x1 = max(0, x1 - padding)
347
  y1 = max(0, y1 - padding)
348
  x2 = min(image.width, x2 + padding)
349
  y2 = min(image.height, y2 + padding)
350
+ # Final validation
351
+ if x2 > x1 and y2 > y1:
352
+ draw.rectangle([x1, y1, x2, y2], fill=255)
353
 
354
  return mask
355
 
 
399
 
400
  if len(bbox) == 4 and translated:
401
  x1, y1, x2, y2 = [int(v) for v in bbox]
402
+ # Ensure coordinates are valid
403
+ if x2 < x1:
404
+ x1, x2 = x2, x1
405
+ if y2 < y1:
406
+ y1, y2 = y2, y1
407
  box_width = x2 - x1
408
  box_height = y2 - y1
409
 
410
+ # Skip very small or invalid boxes
411
  if box_width < 20 or box_height < 10:
412
  continue
413