q6 commited on
Commit
02e552f
·
1 Parent(s): aa4871b

Preserve only text mask regions under 50 pixels tall

Browse files
Files changed (1) hide show
  1. text_detection.py +7 -1
text_detection.py CHANGED
@@ -6,13 +6,15 @@ TEXT_MODEL = "PP-OCRv5_server_det"
6
  TEXT_DEVICE = "cpu"
7
  TEXT_THREADS = 1
8
  TEXT_PADDING = 2
 
9
 
10
 
11
  def main():
12
  os.environ["OMP_NUM_THREADS"] = str(TEXT_THREADS)
 
13
  import numpy as np
14
  from paddleocr import TextDetection
15
- from segmentation import character_mask
16
 
17
  model = TextDetection(
18
  model_name=TEXT_MODEL, device=TEXT_DEVICE, cpu_threads=TEXT_THREADS,
@@ -29,6 +31,10 @@ def main():
29
  for result in model.predict(input=bgr_image):
30
  polygons.extend(result["dt_polys"])
31
  mask = character_mask(rgb_image, polygons, margin=TEXT_PADDING)
 
 
 
 
32
  Image.fromarray(mask).save(output_path, format="PNG")
33
 
34
 
 
6
  TEXT_DEVICE = "cpu"
7
  TEXT_THREADS = 1
8
  TEXT_PADDING = 2
9
+ TEXT_MAX_HEIGHT = 50
10
 
11
 
12
  def main():
13
  os.environ["OMP_NUM_THREADS"] = str(TEXT_THREADS)
14
+ import cv2
15
  import numpy as np
16
  from paddleocr import TextDetection
17
+ from segmentation import MASK_VALUE, character_mask
18
 
19
  model = TextDetection(
20
  model_name=TEXT_MODEL, device=TEXT_DEVICE, cpu_threads=TEXT_THREADS,
 
31
  for result in model.predict(input=bgr_image):
32
  polygons.extend(result["dt_polys"])
33
  mask = character_mask(rgb_image, polygons, margin=TEXT_PADDING)
34
+ count, labels, stats, _ = cv2.connectedComponentsWithStats(mask, connectivity=8)
35
+ keep = np.zeros(count, dtype=np.uint8)
36
+ keep[1:] = (stats[1:, cv2.CC_STAT_HEIGHT] < TEXT_MAX_HEIGHT) * MASK_VALUE
37
+ mask = keep[labels]
38
  Image.fromarray(mask).save(output_path, format="PNG")
39
 
40