从 URL 打开图像文件以使用 pytesseract 进行文本识别

时间:2021-01-12 14:14:02

标签: python python-3.x ocr tesseract python-tesseract

我对 Python 真的很陌生(真的,真的很新)。 这是我需要帮助解决的问题:

我在 txt 文件中有一个图像 URL 列表。该文件中有大约 80.000 个 URL。 我需要使用 pytesseract 扫描所有这些图像并将结果保存在 csv 文件中。 我找到了一个解决方案,但我想优化它。

我现在这样做: • 我使用 PowerShell 将所有图像下载到我的计算机(是的,我使用的是 Windows) • 将它们全部保存在一个文件夹中后(这需要很长时间),我使用以下代码(我在互联网上找到的)扫描所有图像并将提取的文本和图像文件名保存到 . .csv 文件:

from PIL import Image 
from pytesseract import image_to_string
import pytesseract
import os 
import csv

def main(): 
    # path for the folder for getting the raw images 
    path =r"C:\Users\raphaelgomes\Desktop\Projeto OCR - Connect Marketplace\Imagens - Powershell"
  
    # link to the file in which output needs to be kept 
    fullTempPath =r"C:\Users\raphaelgomes\Desktop\Projeto OCR - Connect Marketplace\OCR Checker Python\results\outputFile.csv"
  
    # iterating the images inside the folder 
    for imageName in os.listdir(path): 
        inputPath = os.path.join(path, imageName) 
        img = Image.open(inputPath) 
  
        # applying ocr using pytesseract for python
        pytesseract.pytesseract.tesseract_cmd = r"C:\Users\raphaelgomes\AppData\Local\Programs\Tesseract-OCR\tesseract.exe"
        text = pytesseract.image_to_string(img, lang ="eng") 
  
        # saving the  text for appending it to the output.txt file 
        # a + parameter used for creating the file if not present 
        # and if present then append the text content 
        file1 = open(fullTempPath, "a+") 
  
        # providing the name of the image 
        file1.write(imageName+"\n") 
  
        # providing the content in the image 
        file1.write(text+"\n") 
        file1.close()  
  
    # for printing the output file 
    file2 = open(fullTempPath, 'r') 
    print(file2.read()) 
    file2.close()         
  
  
if __name__ == '__main__': 
    main() 

关键是:有没有办法可以跳过我正在使用 PowerShell 执行的下载过程? 我真的很感激这样做的任何帮助。这个想法是在Python中完成整个过程:正如我所说,我已经在.txt中拥有所有文件链接,所以我需要一个Python代码来一一读取它们并保存文件名和从图像中提取的文本在 .csv 中

非常感谢:)

0 个答案:

没有答案