Image 单面只有OCR图像
我想知道是否有一种方法可以只对右边的文档进行OCR,而忽略左边的文档,而不必在PS或任何其他图像编辑器中分割图像 问题是有时图像上会出现文本。然而,它们污染了我的成绩,因为我只需要在右侧倒车 亲切问候,, OImage 单面只有OCR图像,image,image-processing,ocr,preprocessor,Image,Image Processing,Ocr,Preprocessor,我想知道是否有一种方法可以只对右边的文档进行OCR,而忽略左边的文档,而不必在PS或任何其他图像编辑器中分割图像 问题是有时图像上会出现文本。然而,它们污染了我的成绩,因为我只需要在右侧倒车 亲切问候,, O 如果图像上的文本布局是固定的,则可以简单地读取完整图像,但只将该图像数组的一半传递给tesseract 进口cv2 img=cv2.imreadinputPath _,宽度,uu=img.shape 一半=宽度//2 切割=img[:一半:,:] temp_path=r'path/wher
如果图像上的文本布局是固定的,则可以简单地读取完整图像,但只将该图像数组的一半传递给tesseract 进口cv2 img=cv2.imreadinputPath _,宽度,uu=img.shape 一半=宽度//2 切割=img[:一半:,:] temp_path=r'path/where/you/want/your/crapped/image/to/be/save' cv2.imwritetemp\u路径,剪切 api.SetImageFileinputPath text=api.GetUTF8Text printapi.AllWordConfidences 全文 os.removetemp_路径从目录中删除剪切图像 替代方法 您可以将剪切的图像阵列传递给tesseract,而不是保存它然后删除它。在这种情况下,请记住将剪切的图像阵列转换为RGB格式,因为open cv在读取图像时默认使用BGR格式 rgb_arr=cv2.CVT彩色切割,cv2.COLOR_BGR2RGB 所有这些事情都可以用PIL来完成。在PIL中,您可以使用来提取图像的所需部分。此外,默认情况下,它读取RGB格式的图像,如果您采用上述替代方法,则可以直接传递给tesseract。您可以在识别前调用api.SetRectangle方法传递右半部分的坐标
## 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)