多次运行pytesseract脚本后,笔记本电脑冻结

时间:2019-08-19 05:15:38

标签: opencv python-tesseract

我正在使用pytesseract提取PDF文件的文本。首先,我从pdf的单个页面创建图像,然后对图像进行预处理,并使用方法image_to_string()从图像中提取文本。 在 Pycharm 中运行了几次代码后,我的计算机减速直到完全死机。

我不知道问题出在Pytesseract还是我处理pdf图像的方式。很可能是我内存不足,并且某些进程没有正确完成。

class OCR:
    pytesseract.pytesseract.tesseract_cmd=r'C:\Users\Jorge\AppData\Local\Tesseract-OCR\tesseract.exe'

    def __init__(self, file_name, dpi):
        self.image_name = file_name
        self.dpi = dpi

    def convert_pdf_to_jpg(self, image_name, dpi):
        pages = convert_from_path(image_name, dpi=dpi)
        return np.array(pages[0])

    def pdf_to_text(self):

        image = self.convert_pdf_to_jpg(self.image_name, self.dpi)

        y = int(int(image.shape[0]) / 2)
        x = image.shape[1]
        crop_img = image[0:y, 0:x]
        gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
        gray = cv2.resize(gray, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
        gray = cv2.GaussianBlur(gray, (3, 3), 0)
        cv2.imwrite('img.png', gray)

        alpha = 3
        beta = -400
        gray_bright = cv2.addWeighted(gray, alpha, np.zeros(gray.shape, gray.dtype), 0, beta)


        config = ('-l spa --oem 1 --psm 3')

        text = pytesseract.image_to_string(gray_bright, config=config)
        return text


o=OCR('VGPO-IN-000255-01-19_ESCCO.pdf',400)

print(o.pdf_to_text())

0 个答案:

没有答案