-2

I want to use preprocessed image as an input to def infer_text that will return me annotations. SO, how can I do this, what should I pass to infer_text function?

    image_folder = Path("/home/Tasks/NM_spanish/Invoices_pdf")


def get_preprocessed_image(image_path: pathlib.Path) -> PIL.Image.Image:
    try:
        with image_path.open("rb") as image_input:
            return PIL.Image.open(io.BytesIO(pyabbyy.preprocess(image_input.read())))
    except (RuntimeError, AttributeError):
        print(image_path)


def preprocess_image_folder_(image_folder: pathlib.Path) -> None:
    for image_path in tqdm.tqdm(list(image_folder.rglob("*.pdf"))):
        try:
            get_preprocessed_image(image_path).save(image_path)
        except (RuntimeError, AttributeError):
            print(image_path)

def infer_text(image_path: pathlib.Path) -> graphanno.GraphAnnotation:
    with image_path.open("rb") as img:
        words = pyabbyy.read_text(img.read(), preprocess=False)
    nodes = []
    for word in words:
        box = geometric.Box(
            origin_x=word["origin_x"],
            origin_y=word["origin_y"],
            width=word["max_x"] - word["origin_x"],
            height=word["max_y"] - word["origin_y"],
        )

        nodes.append(graphanno.Node(text=word["text"], box=box))
    num_nodes = len(nodes)
    annotation = graphanno.GraphAnnotation(
        tuple(nodes),
        graphanno.Adjacency(np.zeros((num_nodes, num_nodes))),
        graphanno.Adjacency(np.zeros((num_nodes, num_nodes))),
        graphanno.Adjacency(np.zeros((num_nodes, num_nodes))),
    )
    return annotation

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    preprocess_image_folder_(image_folder)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
aarya
  • 83
  • 1
  • 8
  • 1
    What did you try? What's the issue with your attempt? – Gameplay Mar 02 '23 at 07:54
  • I tried to write it to disk, but could not understand how to use it as an input to def infer_text – aarya Mar 02 '23 at 08:00
  • 1
    "I'm doing some image processing, but I don't know how to use functions". Please start with the python basics tutorial before going to complex projects and asking about python basics here. – Yevhen Kuzmovych Mar 02 '23 at 08:03

1 Answers1

0

You want to use preprocessed_image in your infer_text, but preprocessed_image wants the image_path. You are already getting it inside your infer_text as a parameter. All you have to do is run get_preprocessed_image inside your infer_text and pass it inside the pathlib.Path that you take as a parameter. You can hold it in a variable, then use it wherever inside infer_text. Hope it helps

Rationalist
  • 116
  • 9
  • I tried this, but can you write the changes please ? – aarya Mar 02 '23 at 07:58
  • image_path = get_preprocessed_image(image_path) is this what you suggested? – aarya Mar 02 '23 at 08:12
  • So, when I run main.py after implementing the changes, the process is stopped, like 0%| | 0/240 [00:06, ?it/s] Process finished with exit code 0 – aarya Mar 02 '23 at 08:48