Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Write a Telegram Bot in Python for Document Recognition in 5 Minutes

DZone's Guide to

How to Write a Telegram Bot in Python for Document Recognition in 5 Minutes

Learn how to work with the Python interface of the Smart IDReader recognition library and write a simple Telegram bot.

· AI Zone ·
Free Resource

Did you know that 50- 80% of your enterprise business processes can be automated with AssistEdge?  Identify processes, deploy bots and scale effortlessly with AssistEdge.

We continue the series of articles on how to embed our recognition technologies (passports, bank cards, and others) into your applications. Today, we'll tell you how to work with the Python interface of the Smart IDReader recognition library and write a simple Telegram bot.

By the way, the list of supported programming languages has expanded and now includes C++, C, C#, Objective-C, Swift, Java, and Python, as well as esoteric languages like Visual Basic and, of course, PHP. We support all popular and many unpopular operating systems and architectures.

Traditionally, the demo version of Smart IDReader SDK for Python along with the source code of the Telegram bot implementation are posted on Github and are accessible by reference.

What We Need

We need several files from the SDK:

  • Python interface of the recognition library (pySmartIdEngine.py)
  • Dynamic C++ kernel recognition (in the case of Linux, _pySmartIdEngine.so)
  • Configuration archive (* .zip)
  • For writing the Telegram bot, we chose telepot
  • Interaction with the recognition kernel.

You can find detailed information about the library in the documentation. Here, we will only consider the most necessary.

We connect the library and configure the recognition engine:

# We connect the python-interface of the recognition library
import pySmartIdEngine as se

# Path to the configuration file
smartid_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)

Now, you can write the image recognition function:

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)

Implementing a Bot to Recognize Images Sent to It

We will follow the simple path and take an example from the telepot documentation. We need to write a class whose object will be created for each chat and implement the on_chat_message function in it. Also, in the constructor, we will transfer the previously created recognition engine so that it does not waste time creating it each time:

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)

Finally, create and run the bot:

# 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)

Instead of args.token, you should substitute your unique token bot, obtained after its registration. If you have never created a bot, then on the official website of Telegram there is a detailed instruction.

Conclusion

That's all! We told how to use the Python-interface Smart IDReader SDK to write your Telegram bot for document recognition.

Note that the feature of our products is their complete autonomy — they do not need the Internet. But if you really want to, then with Telegram, you can very easily recognize documents remotely. However, according to Canada/USA legislation, you can only recognize your documents remotely. In order to work with data from other people's documents, it is necessary not only to become an operator for the processing and storage of personal data, to have the necessary infrastructure to protect this data but also to protect all phones and computers on which recognition takes place. Therefore, our colleagues with the help of our libraries have developed a platform for remote recognition and verification of these documents, while taking care of the legal side of the matter.

For more information, please leave comments.

Consuming AI in byte sized applications is the best way to transform digitally. #BuiltOnAI, EdgeVerve’s business application, provides you with everything you need to plug & play AI into your enterprise.  Learn more.

Topics:
python ,sdk ,bot development ,ai ,tutorial ,document recognition

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}