Giter Site home page Giter Site logo

2 Column Layout about paper-qa HOT 2 CLOSED

whitead avatar whitead commented on July 28, 2024
2 Column Layout

from paper-qa.

Comments (2)

suny-sht avatar suny-sht commented on July 28, 2024

Hi, I have a hacky fix where an object detector is used to detect regions in the paper, and text regions are separately parsed. I use the layout parsers package to detect regions in images, and tessaract to OCR the images. For that, the pdf is first converted to a list of images. Some libraries needed are

pip install layoutparser
pip install pytesseract
pip install pdf2image

Here is a working example of such a pipeline that prints all text regions

import layoutparser as lp
import pytesseract
from pdf2image import convert_from_path

model = lp.Detectron2LayoutModel(
        config_path="lp://PubLayNet/mask_rcnn_X_101_32x8d_FPN_3x/config", 
        label_map={
            0: "Text",
            1: "Title",
            2: "List",
            3: "Table",
            4: "Figure",
        }, 
        extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],  
    )

def detect_regions(image):
        blocks = model.detect(image)._blocks

        detected_regions = {
            "Text": [],
            "Title": [],
            "List": [],
            "Table": [],
            "Figure": [],
        }

        for block in blocks:
            detected_regions[block.type].append(block.coordinates)

        return detected_regions

filepath = 'path_to_your_pdf'
images = convert_from_path(filepath)

for image in images:
    regions = detect_regions(image)

    for region_type, region_list in regions.items():
        if region_type == "Text":
            for region in region_list:
                # add very small padding to the region to make sure 
                # all the text is included, e.g. 0.5%
                padding = int(max(image.size) * 0.005)
                region = (
                    region[0] - padding,
                    region[1] - padding,
                    region[2] + padding,
                    region[3] + padding,
                )
                cropped_image = image.crop(region)
                text = pytesseract.image_to_string(cropped_image)
                print(text)

I found this to be quite robust for 2 column documents. An added benefit is it only parses text regions, skipping figures/tables. However, there is quite a bit of computational overhead

from paper-qa.

whitead avatar whitead commented on July 28, 2024

Fixed in v3

from paper-qa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.