如何处理重叠/套印标签上的 OCR?

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

我正在使用下面的代码来提取信息

def text_extract(image):
    print("text_extract Tessaract OCR\n")
    img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    print(pytesseract.image_to_string(img_rgb, lang="eng+chi_sim+chi_tra+jpn"))
    text=pytesseract.image_to_string(img_rgb, lang="eng+chi_sim+chi_tra+jpn")
    text_list=text.split('\n')
    print(text_list)
    part_validate=date_validate(text)
    return text,part_validate
    

filepath=r"pathtofile\image1-0.png"
image=cv2.imread(filepath)
extracted_text=text_extract(img)

我可以提取文本,但在某些情况下我的标签会重叠或套印,如何处理这种情况。 下面显示了重叠的示例图像

python opencv ocr tesseract python-tesseract
1个回答
0
投票

解决方案是使用 python 和 opencv 在使用此代码之前使用 pip 安装以下库:

  1. pip 安装 opencv-python
  2. pip 安装 pytesseract
  3. pip 安装 tkinter

主要代码如下:

from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
import cv2
import pytesseract 

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

root = Tk()
root.title('TechVidvan Text from image project') 

newline= Label(root)
uploaded_img=Label(root)
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill = Y )


def extract(path):
    Actual_image = cv2.imread(path)
    Sample_img = cv2.resize(Actual_image,(400,350))
    Image_ht,Image_wd,Image_thickness = Sample_img.shape
    Sample_img = cv2.cvtColor(Sample_img,cv2.COLOR_BGR2RGB)
    texts = pytesseract.image_to_data(Sample_img) 
    mytext=""
    prevy=0
    for cnt,text in enumerate(texts.splitlines()):
        if cnt==0:
            continue
        text = text.split()
        if len(text)==12:
            x,y,w,h = int(text[6]),int(text[7]),int(text[8]),int(text[9])
            if(len(mytext)==0):
                prey=y
            if(prevy-y>=10 or y-prevy>=10):
                print(mytext)
                Label(root,text=mytext,font=('Times',15,'bold')).pack()
                mytext=""
            mytext = mytext + text[11]+" "
            prevy=y
    Label(root,text=mytext,font=('Times',15,'bold')).pack()

def show_extract_button(path):
    extractBtn= Button(root,text="Extract text",command=lambda: extract(path),bg="#2f2f77",fg="gray",pady=15,padx=15,font=('Times',15,'bold'))
    extractBtn.pack()

def upload():
    try:
        path=filedialog.askopenfilename()
        image=Image.open(path)
        img=ImageTk.PhotoImage(image)
        uploaded_img.configure(image=img)
        uploaded_img.image=img
        show_extract_button(path)
    except:
        pass  

uploadbtn = Button(root,text="Upload an image",command=upload,bg="#2f2f77",fg="gray",height=2,width=20,font=('Times',15,'bold')).pack()
newline.configure(text='\n')
newline.pack()
uploaded_img.pack()

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.