检测到的致命错误显示运行使用 tesseract 的 python 脚本

时间:2021-06-08 13:30:20

标签: python tesseract python-tesseract

我是 Python 新手,我正在尝试创建一个应用程序,该应用程序读取图像中的文本并使用检测到的语言重命名图像。当我运行 .py 文件时代码运行良好,但是当我创建 .exe 文件时“检测到致命错误:无法执行脚本 ImageDetector”

import os
from tkinter import *
from PIL import Image
import pytesseract
from langdetect import DetectorFactory
from langdetect import detect_langs
pytesseract.pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
App = Tk()
App.geometry("500x250")
App.configure(bg="Black")
App.title("Image Language Detector and converter")
name = Label(App, text="Detects .png files ",bg = "Black", fg="White")
name.pack()


def languages(a):
        if(a[0] == 'en'):
            return("English")
        elif(a[0] == 'de'):
            return("Greman")
        elif(a[0] == 'es'):
            return("Spanish")
        elif(a[0] == 'fr'):
            return("French")
        elif(a[0] == 'it'):
            return("Italian")
        elif(a[0] == 'ja'):
            return("Japanese")
        elif(a[0] == 'ko'):
            return("Korean")
        elif(a[0] == 'pt'):
            return("Portuguese")
        elif(a[0] == 'zh-cn'):
            return("Chinese Simplified")
        elif(a[0] == 'zh-tw'):
            return("Chinese Traditional")
        elif(a[0] == 'tr'):
            return("Turkish")
        elif(a[0] == 'fi'):
            return("Finnish")
        elif(a[0] == 'id'):
            return("Indonesian")
        elif(a[0] == 'nl'):
            return("Dutch")
        elif(a[0] == 'ru'):
            return("Russian")
        elif(a[0] == 'pl'):
            return("Polish")
        elif(a[0] == 'ar'):
            return('Arabic')
        else:
            return('Chinese Simplified / Chinese Traditional')

def conversion(INPUT,base,lang,file_path):
    i = 1
    des_base = f"{INPUT}\{lang}"
    extension = os.path.splitext(file_path)[1]
    dst=des_base+"_"+str(i)+".jpg"
    src=base+extension
    try:
        for file in os.listdir():
            if(os.path.isfile(dst)):
                i+=1
                dst = dst=des_base+"_"+str(i)+".jpg"
                os.rename(src,dst)
            else:
                dst = dst=des_base+"_"+str(i)+".jpg"
                os.rename(src,dst)
    except Exception as e:
        print(e)

def ImageLocation():
    INPUT = inputtxt.get(1.0,"end-1c")
    os.chdir(INPUT)
    processing_text = StringVar()
    processing_text.set('start')
    directory =os.listdir(INPUT)
    inte = 1
    for file in os.listdir():
        if file.endswith(".jpg") or file.endswith(".png"):
            file_path = f"{INPUT}\{file}"
            base = os.path.splitext(file_path)[0]
            im = Image.open(file_path)
            custom_config = r'-l ara+ind+eng+fra+deu+ita+jpn+kor+nld+pol+por+rus+chi_sim+spa+fin+chi_tra+tur --psm 6'
            text = pytesseract.image_to_string(im,config=custom_config)
            lan={lang.lang: lang.prob for lang in detect_langs(text)}
            two_char_iso_lang_val = list(lan.keys())
            processing_text = "Prossed "+ str(inte)+ " of "+ str(len(directory))
            Process.config(text = f"{processing_text}",bg="black",fg="White")
            lang = languages(two_char_iso_lang_val)
            conversion(INPUT,base,lang,file_path)
            Process.update()
            inte +=1
    Finish.config(text = "Converted!!",bg="black",fg="White")
    
try:
    
    l = Label(text = "PNG image location",bg="black",fg="White")
    inputtxt = Text(App,height = 2, width = 300,bg = "white")
    inputtxt.insert(END, 'Enter the location of images')
    Display = Button(App,height = 2,width = 20,text ="Convert!",command = lambda:ImageLocation())
    Process = Label(App, text = "",bg="black")
    Finish = Label(App, text = "",bg="black")
    l.pack()
    inputtxt.pack()
    Display.pack()
    Process.pack()
    Finish.pack()
except Exception as e:
    print(e)
App.mainloop()

用于将.py文件转换为.exe文件的命令:

pyinstaller ImageDetector.py --onefile --noconsole

提前致谢!

0 个答案:

没有答案
相关问题