我正在尝试用python设置一个简单的OCR,我现在正在做测试。我使用notepad++,Python3.10.1,PIP,Pillow,pytesseract。这是我的密码:
import pytesseract
import numpy
from PIL import Image, ImageEnhance, ImageFilter
pytesseract.pytesseract.tesseract_cmd = 'Lib/site-packages/pytesseract/pytesseract.py'
im = Image.open(r"C:\Users\Leonid\AppData\Local\Programs\Python\Python310\Lib\site-packages\pytesseract\test.jpg") # Ouverture du fichier image
# Filtrage (augmentation du contraste)
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
# Lancement de la procédure de reconnaissance
text = pytesseract.image_to_string(im)
print(text)
当我执行代码时,我会得到以下错误:
Traceback (most recent call last):
File "C:\Users\Leonid\Desktop\Python\test.py", line 15, in <module>
text = pytesseract.image_to_string(im)
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 416, in image_to_string
return {
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 419, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 286, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 257, in run_tesseract
raise e
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\site-packages\pytesseract\pytesseract.py", line 254, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Leonid\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 193] %1 n’est pas une application Win32 valide
我试着安装numpy,因为我在论坛上看到它解决了这个问题,然后导入它("Import“,第2行)。但还是没用。
是什么导致了这个错误,以及如何修复它?
发布于 2022-05-11 21:48:09
问题是这条线:
pytesseract.pytesseract.tesseract_cmd = 'Lib/site-packages/pytesseract/pytesseract.py'
您需要将pytesseract.pytesseract.tesseract_cmd
设置为Tesseract可执行文件的位置。相反,您可以将其设置为Python脚本。
有关应该将其设置为什么的一些示例,请参见this question。
https://stackoverflow.com/questions/72207702
复制相似问题