美文网首页
5分钟内:用Python写出用于文档识别的Telegram Bo

5分钟内:用Python写出用于文档识别的Telegram Bo

作者: 头顶一根发的程序猿 | 来源:发表于2019-07-22 10:43 被阅读0次

我们将讨论如何将识别技术(用于识别护照、银行卡等)嵌入到你的应用程序中。如何使用智能IDReader识别库的Python接口并编写一个简单的Telegram bot?且听这回分解。

顺便说一下,已经扩展了受支持的编程语言列表,现在包括c++、C、c#、Objective-C、Swift、Java和Python,以及一些深奥的语言,如Visual Basic,当然还有PHP。我们支持所有流行的和小众的的操作系统和架构。

一如既往,用于Python的Smart IDReader SDK的演示版本以及Telegram bot实现的源代码都发布在Github上,可以通过引用来访问。

准备工作

我们需要几个SDK的文件:

识别库的Python接口(pySmartIdEngine.py)

动态c++内核识别(在Linux中,_pySmartIdEngine.so)

配置存档(* .zip)

编写Telegram bot,我们选择的是telepot

识别内核的交互。

你可以在文档中找到关于这个库的详细信息,在这里,我们只要最必要的信息内容。

连接库并配置识别引擎:

# We connect the python-interface of the recognition library

import pySmartIdEngine as se

# Path to the configuration filesmartid_config_path = 'bundle_mock_smart_idreader.zip'

# Create the recognition engine, it's best to do it once and keep it in memory

smartid_engine = se.RecognitionEngine (smartid_config_path)

编写图像识别函数:

def recognize_image_file (smartid_engine, image_file_path):

# Get the default settings and include the required types of documents

session_settings = smartid_engine.CreateSessionSettings () session_settings.AddEnabledDocumentTypes ('rus.passport.national')

# Create a recognition session

session = smartid_engine.SpawnSession (session_settings)

# Recognize the image result = session.ProcessImageFile (image_file_path)

# Convert recognized string fields to dict

recognized_fields = {}

for field_name in result.GetStringFieldNames ():

field = result.GetStringField (field_name)

recognized_fields [field_name] = field.GetValue (). GetUtf8String ()

# Return the JSON string representation of recognized fields

return json.dumps (recognized_fields, ensure_ascii = False, indent = 2)

创建机器人来识别发送的图像

遵循简单的路径,并以telepot文档为例。我们需要编写一个类,该类的对象将为每个聊天创建,并在其中实现on_chat_message函数。此外,在构造函数中,我们将转移之前创建的识别引擎,这样它就不会浪费时间来每次创建:

import telepot

from telepot.delegate import per_chat_id, create_open, pave_event_space

from telepot.loop import MessageLoop

class SmartIDReaderBot(telepot.helper.ChatHandler):

def __init__(self, seed_tuple, smartid_engine, **kwargs):

self.smartid_engine = smartid_engine

super(SmartIDReaderBot, self).__init__(seed_tuple, **kwargs)

def on_chat_message(self, msg):

try:

content_type, chat_type, chat_id = telepot.glance(msg)

if content_type in ['document', 'photo']: content = msg[content_type] if

content_type == 'document' \

else msg[content_type][-1]

if 'file_id' in content:

# Скачиваем файл изображения

downloads_dir = 'downloaded_images'

os.makedirs(downloads_dir, exist_ok=True)

temp_path = os.path.join(downloads_dir,

'chat_%d_id_%d_temp.png' % (chat_id, msg['message_id']))

self.bot.download_file(content['file_id'], temp_path)

# Распознаем изображение

recognition_result_str = recognize_image_file(

self.smartid_engine, temp_path)

# Посылаем сообщение с результатом распознавания

self.send_message(recognition_result_str)

else:

self.send_message("Send me a photo and I'll recognize it!")

except Exception as e:

self.send_message('Exception: %s' % e.message)

def send_message(self, message):

self.sender.sendMessage(message)

print(message)

最后,创建并运行机器人:

# Creating a bot

bot = telepot.DelegatorBot (args.token, [

pave_event_space () (

per_chat_id (), create_open,

SmartIDReaderBot, smartid_engine, timeout = 1000000)

])

# Run the bot

MessageLoop (bot) .run_as_thread ()

while 1:

time.sleep (10)

在注册后,你要获得唯一的令牌机器人。如果你从未创建过机器人,Telegram的官方网站上有详细的说明。

结论

以上便是所有关于用Python写出用于文档识别的Telegram Bot的内容!

请注意,该产品的特点是完全自主的——不需要网络。但是如果你想用网络来使用Telegram,你也可以很容易地远程识别文件。

如需更多信息,请留言。

相关文章

网友评论

      本文标题:5分钟内:用Python写出用于文档识别的Telegram Bo

      本文链接:https://www.haomeiwen.com/subject/jkjilctx.html