只有一面的OCR图像

问题描述 投票:0回答:1

我想知道有没有一种方法可以只OCR右边的文档(忽略左边的),而不用在PS或其他图像编辑器中分割图像?

问题是,有时图像上会有文字。然而,它们污染了我的结果,因为我只需要后置右侧。

enter image description here

亲切的问候,O。

## PREPROCESSING (load and read images to OCR and transform them into a DataFrame)

import pytesseract as tess
from tesserocr import PyTessBaseAPI, RIL
import os
from PIL import Image
import pandas as pd
import re
import tesserocr

path = "/Users/oliviervandhuynslager/PycharmProjects/Design Centre/assets/img/" ##path to directory (folder) where the images are located

count = 0
fileName = [] #create empty list that will contain the original filenames
fullText = [] #create empty list to store the OCR results per file
for imageName in os.listdir(path):
    count = count + 1
    fileName.append(imageName)
    # fileName.sort()#generate list from texts.

with PyTessBaseAPI(lang='eng') as api:
    for imageName in os.listdir(path):
        inputPath = os.path.join(path, imageName)
        api.SetImageFile(inputPath)
        text = api.GetUTF8Text()
        print(api.AllWordConfidences())
        fullText.append(text)

d = {"FILENAME":fileName, "OCR": fullText}
df = pd.DataFrame(d)

##Generate empty lists

search_material = []
search_product = []
search_manufacturer = []
search_designer = []
search_description = []
search_dimensions = []
search_packing = []
search_price = []
search_delivery = []

## -_-_-_-_-_-_-_-_-_-_-_-_-_-

count_material = 0
count_product = 0
count_maufacturer = 0
count_designer = 0
count_description = 0
count_dimension = 0
count_packing = 0
count_price = 0

## search for PRODUCT (NAME/TITLE)
for values in df["OCR"]:
    try:
        search_product.append((re.search(r'Product[\s\S]+', values).group()).split("\n")[0].split(":")[1])
        count_product = count_product + 1
    except:
        search_product.append("")
df["PRODUCT"] = search_product

## search for MANUFACTURER
for values in df["OCR"]:
    try:
        search_manufacturer.append((re.search(r'Manufacturer[\S\s]+', values).group()).split("\n")[0].split(":")[1])
        count_maufacturer = count_maufacturer + 1
    except:
        search_manufacturer.append("")
df["MANUFACTURER"] = search_manufacturer

## search for DESIGNER
for values in df["OCR"]:
    try:
        search_designer.append((re.search(r'Designer[\S\s]+', values).group()).split("\n")[0].lstrip().split(":")[1])
        count_designer = count_designer + 1
    except:
        search_designer.append("")
df["DESIGNER"] = search_designer

## search for MATERIALS
for values in df["OCR"]:
    try:
        search_material.append((re.search(r'Material[\S\s]+', values).group()).split("\n")[0].lstrip().split(":")[1])
        count_material = count_material + 1
    except:
        search_material.append("")
df["MATERIAL"] = search_material

#search for DESCRIPTION:
for values in df["OCR"]:
    try:
        search_description.append((re.search(r'Description[\S\s]+', values).group()).split(":")[1])
        count_description = count_description + 1
    except:
        search_description.append("")
df["DESCRIPTION"] = search_description

#search for DIMENSIONS
for values in df["OCR"]:
    try:
        search_dimensions.append((re.search(r'Dimensions[\S\s]+', values).group()).split("\n")[0].split(":")[1])
        count_dimension = count_dimension + 1
    except:
        search_dimensions.append("")
df["DIMENSIONS"] = search_dimensions

#search for PACKING
for values in df["OCR"]:
    try:
        search_packing.append((re.search(r'Packing[\S\s]+', values).group()).split('\n\n')[0].split(":")[1])
        count_packing = count_packing + 1
    except:
        search_packing.append("")
df["PACKING"] = search_packing

#search for PRICE
for values in df["OCR"]:
    try:
        search_price.append((re.search(r'Price[\S\s]+', values).group()).split("\n")[0].split(":")[1])
        count_price = count_price + 1
    except:
        search_price.append("")
df["PRICE"] = search_price

#search for DELIVERY DAYS
for values in df["OCR"]:
    try:
        search_delivery.append((re.search(r'Delivery[\S\s]+', values).group()).split("\n\n")[0].split(":")[1])
        count_delivery = count_delivery + 1
    except:
        search_delivery.append("")
df["DELIVERY"] = search_delivery

df.drop(columns="OCR", inplace=True)

print(df)
image image-processing ocr preprocessor
1个回答
1
投票

如果你的图像上的文字布局是固定的,那么你可以简单地读取完整的图像,但只将该图像数组的一半传递给魔方。

    import cv2

    img = cv2.imread(inputPath)
    _, width, _ = img.shape
    half = width//2
    cut = img[: half: , :]
    temp_path = r'path/where/you/want/your/cropped/image/to/be/saved'
    cv2.imwrite(temp_path, cut)
    api.SetImageFile(inputPath)
    text = api.GetUTF8Text()
    print(api.AllWordConfidences())
    fullText.append(text)
    os.remove(temp_path) #removing cut image from the directory 

替代方法 你可以通过图像数组 cut 到魔方中,而不是保存后再删除。在这种情况下,记得将图像数组转换为 cut 为RGB格式,因为open cv在读取图像时默认使用BGR格式。

rgb_arr = cv2.cvtColor(cut, cv2.COLOR_BGR2RGB)


所有这些事情都可以通过 PIL 也。在PIL中,您可以使用 作物() 来提取图像的所需部分。另外,默认情况下,它读取的是RGB格式的图像,如果你按照上面提到的替代方法,可以直接传给魔方。


0
投票

你可以打电话 api.SetRectangle 方法传递识别前右半部分的坐标。

© www.soinside.com 2019 - 2024. All rights reserved.