6
$\begingroup$

ORIGINAL IMAGE:

enter image description here

GOAL:

enter image description here

I want to separate texts into individual paragraphs by placing bounding boxes over them (as shown above).

I tried it do this via traditional computer vision approach using opencv.

  1. I plotted character level bounding box
  2. Next, I gray-scaled the image, binarized it.
  3. Applied dilation
  4. And finally placed bbox over the dilated image.

This is what I get:

enter image description here

> #Morphological Transformation

kernel = np.ones((3,4),np.int8)

dilation = cv2.dilate(im_bw, kernel)

cv2.imwrite('dilated.png', dilation)

Plotting rectangular box

ret,thresh = cv2.threshold(im_bw, 127,255,0)
image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE )

for c in contours:
    rect = cv2.boundingRect(c)
    if rect[2] < 50 or rect[3] < 50 : continue

    print (cv2.contourArea(c))
    x,y,w,h = rect
    cv2.rectangle(im_new,(x,y),(x+w,y+h),(0,255,0),2)

cv2.imwrite('sample_res_inner.jpg',im_new)

Since the image is scanned image plus the line-spaces between them are small I couldn't able to segment them based on paragraphs.

How can I get my desired result?

$\endgroup$
  • $\begingroup$ May you post the original image without any processing? $\endgroup$ – Edmund Mar 14 '19 at 21:28
  • $\begingroup$ @Edmund It's done now. $\endgroup$ – DGS Mar 15 '19 at 4:18
  • $\begingroup$ I think you should refer to the following link: getting-paragraph-sections $\endgroup$ – Benjamin Frazier Nov 8 '19 at 19:45
  • $\begingroup$ It is a year old post.. wondering if the solution was found. Can you post here please $\endgroup$ – Sandeep Bhutani Apr 2 at 4:24
2
$\begingroup$

There are two options :

  1. Scan the images with a higher DPI. This should accentuate vertical separation between paragraphs.
  2. Train a Deep learning model for Text Detection in scene. Examples : https://github.com/qjadud1994/CRNN-Keras and https://github.com/mvoelk/ssd_detectors
|improve this answer|||||
$\endgroup$
  • $\begingroup$ Well, the above mentioned methods are scene text detectors. It will detect the texts word by word. It doesn't perform segmentation by paragraphs which is want. $\endgroup$ – DGS Mar 15 '19 at 9:54
  • $\begingroup$ youtube.com/watch?v=qIs1SUH_3Lw&feature=youtu.be $\endgroup$ – Shamit Verma Mar 15 '19 at 10:38
  • $\begingroup$ Video mentioned above is a demo of model that extends the approach to paragraphs. arxiv.org/abs/1604.03286 . It starts with word boundaries and trains an "attention" layer to identify paragraph boundaries. $\endgroup$ – Shamit Verma Mar 15 '19 at 10:39
  • $\begingroup$ I just want to detect paragraphs and i don't want to recognize it now. Will this method help to detect the paragraphs or group of text that lies together as mentioned under GOAL section. If so, can u please provide the implementation of this paper $\endgroup$ – DGS Mar 19 '19 at 5:31
1
$\begingroup$

Have you looked into Tesseract (and its Python wrapper/interface: pytesseract)? I don't guarantee that it will solve your problems entirely, but it offers bounding box and OCR features.

On this Tesseract site it lists possible page segmentation modes that you could play around with.

There is also this page that provides some quality improvement suggestions.

There are many questions/answers on Stack Overflow about specific usage cases. In this answer, for example, there is a recommendation to use OSM mode to detect multiple columns.

And there is this SO answer that offers a way to break text into paragraphs.

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.