我在 Ubuntu 上遇到 pytesseract 问题
I have a problem with pytesseract on Ubuntu
我正在尝试制作一个电报机器人,其中一个功能是从图像中识别文本,在 Windows 上一切正常,但是一旦我切换到 Linux ,我马上就遇到了同样的异常,一开始我以为是我指定的路径不对 pytesseract.pytesseract.tesseract_cmd
(因为我访问的网站都是这样写的,但是仔细检查了一切,我没有发现任何错误)
这是我的代码:
from telebot import types
from googlesearch import search
from PIL import Image
import pytesseract
import cv2
import os
import numpy as np
import telebot
import config
bot = telebot.TeleBot(config.token)
@bot.message_handler(content_types= ["photo"])
def answer_to_photo(message):
statuss = ['creator', 'administrator', 'member']
user_status = str(bot.get_chat_member(chat_id='chat id', user_id=message.from_user.id).status)
if user_status in statuss:
pytesseract.pytesseract.tesseract_cmd = r'/home/shalor1k/.local/bin/pytesseract'
file_info = bot.get_file(message.photo[len(message.photo) - 1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = r'C:\bot\photo' + message.photo[1].file_id
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
bot.reply_to(message, 'Processing your photo')
image = src
preprocess = "thresh"
image = cv2.imread(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "tresh":
gray = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif preprocess == "blur":
gray = cv2.median.Blur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename), lang = 'rus')
os.remove(filename)
os.remove(src)
异常文本:
File "main_bot_for_server.py", line 67, in answer_to_photo
text = pytesseract.image_to_string(Image.open(filename), lang = 'rus')
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 370, in image_to_string
return {
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 373, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 282, in run_and_get_output
run_tesseract(**kwargs)
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 258, in run_tesseract
raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (2, 'Usage: pytesseract [-l lang] input_file')
第一个问题是 tesseract ocr 本身的二进制文件没有安装。
第二个问题是没有安装需要的语言包
我正在尝试制作一个电报机器人,其中一个功能是从图像中识别文本,在 Windows 上一切正常,但是一旦我切换到 Linux ,我马上就遇到了同样的异常,一开始我以为是我指定的路径不对 pytesseract.pytesseract.tesseract_cmd
(因为我访问的网站都是这样写的,但是仔细检查了一切,我没有发现任何错误)
这是我的代码:
from telebot import types
from googlesearch import search
from PIL import Image
import pytesseract
import cv2
import os
import numpy as np
import telebot
import config
bot = telebot.TeleBot(config.token)
@bot.message_handler(content_types= ["photo"])
def answer_to_photo(message):
statuss = ['creator', 'administrator', 'member']
user_status = str(bot.get_chat_member(chat_id='chat id', user_id=message.from_user.id).status)
if user_status in statuss:
pytesseract.pytesseract.tesseract_cmd = r'/home/shalor1k/.local/bin/pytesseract'
file_info = bot.get_file(message.photo[len(message.photo) - 1].file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = r'C:\bot\photo' + message.photo[1].file_id
with open(src, 'wb') as new_file:
new_file.write(downloaded_file)
bot.reply_to(message, 'Processing your photo')
image = src
preprocess = "thresh"
image = cv2.imread(image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "tresh":
gray = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
elif preprocess == "blur":
gray = cv2.median.Blur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename), lang = 'rus')
os.remove(filename)
os.remove(src)
异常文本:
File "main_bot_for_server.py", line 67, in answer_to_photo
text = pytesseract.image_to_string(Image.open(filename), lang = 'rus')
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 370, in image_to_string
return {
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 373, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 282, in run_and_get_output
run_tesseract(**kwargs)
File "/home/shalor1k/.local/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 258, in run_tesseract
raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (2, 'Usage: pytesseract [-l lang] input_file')
第一个问题是 tesseract ocr 本身的二进制文件没有安装。 第二个问题是没有安装需要的语言包