Layout-Parser / layout-parser

A Unified Toolkit for Deep Learning Based Document Image Analysis
https://layout-parser.github.io/
Apache License 2.0
4.75k stars 456 forks source link

AttributeError: module layoutparser has no attribute Detectron2LayoutModel #77

Closed theiman112860 closed 2 years ago

theiman112860 commented 2 years ago

Hi,

Thank you for this awesome program! I successfully installed layout-parser Detectron2 on my windows 10 laptop. When I run the following code:

import layoutparser as lp import cv2 from pdf2image import convert_from_bytes

images = convert_from_bytes(open('C:\temp\ConsigneeList\Doc 4 Distribution List.pdf', 'rb').read())

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

loop through each page

for image in images: ocr_agent = lp.ocr.TesseractAgent()

image = np.array(image)

layout = model.detect(image)

text_blocks = lp.Layout([b for b in layout if b.type == 'Text']) #loop through each text box on page.

for block in text_blocks: segment_image = (block .pad(left=5, right=5, top=5, bottom=5) .crop_image(image)) text = ocr_agent.detect(segment_image) block.set(text=text, inplace=True)

for i, txt in enumerate(text_blocks.get_texts()):
        my_file = open("OUTPUT FILE PATH/FILENAME.TXT","a+")
        my_file.write(txt)

I get the following errors:


AttributeError Traceback (most recent call last)

in ----> 1 model = lp.Detectron2LayoutModel( 2 config_path ='lp://PubLayNet/mask_rcnn_X_101_32x8d_FPN_3x/config', # In model catalog 3 label_map = {0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, # In model`label_map` 4 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8] # Optional 5 ) C:\ProgramData\Anaconda3\lib\site-packages\layoutparser\file_utils.py in __getattr__(self, name) 224 value = getattr(module, name) 225 else: --> 226 raise AttributeError(f"module {self.__name__} has no attribute {name}") 227 228 setattr(self, name, value) AttributeError: module layoutparser has no attribute Detectron2LayoutModel Any ideas on what is wrong? Thank you!! Sincerely, tom **Checklist** 1. I have searched related issues but cannot get the expected help. 2. The bug has not been fixed in the latest version, see the [Layout Parser Releases](https://github.com/Layout-Parser/layout-parser/releases/) **To Reproduce** Steps to reproduce the behavior: 1. What command or script did you run? ```none A placeholder for the command. ``` **Environment** 1. Please describe your Platform [Windows/MacOS/Linux] 2. Please show the Layout Parser version 2. You may add addition that may be helpful for locating the problem, such as - How you installed PyTorch [e.g., pip, conda, source] - Other environment variables that may be related (such as `$PATH`, `$LD_LIBRARY_PATH`, `$PYTHONPATH`, etc.) **Error traceback** If applicable, paste the error traceback here. **Screenshots** If applicable, add screenshots to help explain your problem. **Additional context** Add any other context about the problem here.
lolipopshock commented 2 years ago

Hi @theiman112860 ! It seems you haven't installed the detectron2 backend: you can follow the tutorials here to have it installed. PS: now layoutparser provides the support for directly loading PDF documents, and you could find the example here.

theiman112860 commented 2 years ago

Hi Shannon, I reinstalled it and this worked perfectly:

import layoutparser as lp

pdf_layout = lp.load_pdf("C:\temp\ConsigneeList\Doc 4 Distribution List.pdf")

pdf_layout = lp.load_pdf("C:\temp\ConsigneeList\extable.pdf") pdf_layout[0] # the layout for page 0

pdf_layout, pdf_images = lp.load_pdf("path/to/pdf", load_images=True)

pdf_layout, pdf_images = lp.load_pdf("C:\temp\ConsigneeList\extable.pdf", load_images=True) lp.draw_box(pdf_images[0], pdf_layout[0])

However, when I use the original code: import layoutparser as lp import cv2 from pdf2image import convert_from_bytes

images = convert_from_bytes(open('C:\\temp\\ConsigneeList\\Doc 4 Distribution List.pdf', 'rb').read())

model = lp.Detectron2LayoutModel(
config_path ='lp://PubLayNet/mask_rcnn_X_101_32x8d_FPN_3x/config', # In model catalog
label_map = {0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, # In modellabel_map
extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8] # Optional
)
#loop through each page
for image in images:
    ocr_agent = lp.ocr.TesseractAgent()

    image = np.array(image)

    layout = model.detect(image)
    text_blocks = lp.Layout([b for b in layout if b.type == 'Text']) #loop through each text box on page.

for block in text_blocks:
    segment_image = (block
    .pad(left=5, right=5, top=5, bottom=5)
    .crop_image(image))
    text = ocr_agent.detect(segment_image)
    block.set(text=text, inplace=True)

for i, txt in enumerate(text_blocks.get_texts()):
        my_file = open("OUTPUT FILE PATH/FILENAME.TXT","a+")
        my_file.write(txt)

I get the error below.. Any ideas? Thank you!!

Sincerely,

tom

lolipopshock commented 2 years ago

Thanks for the updates @theiman112860 . It seems the error message was not shown in your post -- could you share them again?

theiman112860 commented 2 years ago

Hi Shannon, Sorry about that!! Below is the error output that I am getting. Thank you again!! -th


ValueError Traceback (most recent call last)

in ----> 1 model = lp.Detectron2LayoutModel( 2 config_path ='lp://PubLayNet/mask_rcnn_X_101_32x8d_FPN_3x/config', # In model catalog 3 label_map = {0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"}, # In modellabel_map 4 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8] # Optional 5 ) C:\ProgramData\Anaconda3\lib\site-packages\layoutparser\file_utils.py in __getattr__(self, name) 221 value = self._get_module(name) 222 elif name in self._class_to_module.keys(): --> 223 module = self._get_module(self._class_to_module[name]) 224 value = getattr(module, name) 225 else: C:\ProgramData\Anaconda3\lib\site-packages\layoutparser\file_utils.py in _get_module(self, module_name) 230 231 def _get_module(self, module_name: str): --> 232 return importlib.import_module("." + module_name, self.__name__) 233 234 def __reduce__(self): C:\ProgramData\Anaconda3\lib\importlib\__init__.py in import_module(name, package) 125 break 126 level += 1 --> 127 return _bootstrap._gcd_import(name[level:], package, level) 128 129 C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _gcd_import(name, package, level) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _find_and_load(name, import_) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _gcd_import(name, package, level) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _find_and_load(name, import_) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _load_unlocked(spec) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap_external.py in exec_module(self, module) C:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds) C:\ProgramData\Anaconda3\lib\site-packages\layoutparser\models\__init__.py in 13 # limitations under the License. 14 ---> 15 from .detectron2.layoutmodel import Detectron2LayoutModel 16 from .paddledetection.layoutmodel import PaddleDetectionLayoutModel 17 from .effdet.layoutmodel import EfficientDetLayoutModel C:\ProgramData\Anaconda3\lib\site-packages\layoutparser\models\detectron2\__init__.py in 16 # A trick learned from 17 # https://github.com/facebookresearch/detectron2/blob/62cf3a2b6840734d2717abdf96e2dd57ed6612a6/detectron2/checkpoint/__init__.py#L6 ---> 18 from .layoutmodel import Detectron2LayoutModel C:\ProgramData\Anaconda3\lib\site-packages\layoutparser\models\detectron2\layoutmodel.py in 24 25 if is_detectron2_available(): ---> 26 import detectron2.engine 27 import detectron2.config 28 ~\detectron2\detectron2\engine\__init__.py in 9 # prefer to let hooks and defaults live in separate namespaces (therefore not in __all__) 10 # but still make them available here ---> 11 from .hooks import * 12 from .defaults import * ~\detectron2\detectron2\engine\hooks.py in 17 18 import detectron2.utils.comm as comm ---> 19 from detectron2.evaluation.testing import flatten_results_dict 20 from detectron2.solver import LRMultiplier 21 from detectron2.utils.events import EventStorage, EventWriter ~\detectron2\detectron2\evaluation\__init__.py in 1 # Copyright (c) Facebook, Inc. and its affiliates. ----> 2 from .cityscapes_evaluation import CityscapesInstanceEvaluator, CityscapesSemSegEvaluator 3 from .coco_evaluation import COCOEvaluator 4 from .rotated_coco_evaluation import RotatedCOCOEvaluator 5 from .evaluator import DatasetEvaluator, DatasetEvaluators, inference_context, inference_on_dataset ~\detectron2\detectron2\evaluation\cityscapes_evaluation.py in 9 from PIL import Image 10 ---> 11 from detectron2.data import MetadataCatalog 12 from detectron2.utils import comm 13 from detectron2.utils.file_io import PathManager ~\detectron2\detectron2\data\__init__.py in 2 from . import transforms # isort:skip 3 ----> 4 from .build import ( 5 build_batch_data_loader, 6 build_detection_test_loader, ~\detectron2\detectron2\data\build.py in 10 11 from detectron2.config import configurable ---> 12 from detectron2.structures import BoxMode 13 from detectron2.utils.comm import get_world_size 14 from detectron2.utils.env import seed_all_rng ~\detectron2\detectron2\structures\__init__.py in 5 from .instances import Instances 6 from .keypoints import Keypoints, heatmaps_to_keypoints ----> 7 from .masks import BitMasks, PolygonMasks, polygons_to_bitmask, ROIMasks 8 from .rotated_boxes import RotatedBoxes 9 from .rotated_boxes import pairwise_iou as pairwise_iou_rotated ~\detectron2\detectron2\structures\masks.py in 4 import numpy as np 5 from typing import Any, Iterator, List, Union ----> 6 import pycocotools.mask as mask_util 7 import torch 8 from torch import device C:\ProgramData\Anaconda3\lib\site-packages\pycocotools\mask.py in 1 __author__ = 'tsungyi' 2 ----> 3 import pycocotools._mask as _mask 4 5 # Interface for manipulating masks stored in RLE format. pycocotools\_mask.pyx in init pycocotools._mask() ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject
lolipopshock commented 2 years ago

Oh I see - this seems to be an issue from the pycoco module. What's the pycoco version on your machine pip show pycocotools?

theiman112860 commented 2 years ago

Hi Shannon, Its 2.0.2 ..Thank you!

lolipopshock commented 2 years ago

I see -- could you check this https://stackoverflow.com/a/66138833 ?

theiman112860 commented 2 years ago

Hi Shannon, Thank you!! I will check that out!!

lolipopshock commented 2 years ago

I'll have the issue closed as it seems we've found the solution -- feel free to reopen if you have more questions.

theiman112860 commented 2 years ago

Hi Shannon,

Thank you!! I hope that you have a great weekend!!

Sincerely,

Tom

Sent from my iPhone

On Sep 24, 2021, at 3:12 PM, Shannon Shen @.***> wrote:

 Closed #77.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.