📜 ⬆️ ⬇️

Smart IDReader SDK - how to write a Telegram bot in Python to recognize documents in 5 minutes

Smart IDReader by Smart Engines


We, Smart Engines , continue the cycle of articles about how to embed our recognition technologies ( passports , bank cards and others) into your applications. Earlier, we already wrote about embedding on iOS and Android , and today we’ll tell you how to work with Smart IDReader’s Python recognition interface and write a simple Telegram bot.


By the way, the list of programming languages ​​supported by us has expanded and now includes C ++, C, C #, Objective-C, Swift, Java, Python, and also such esoteric languages ​​as Visual Basic and, of course, PHP. As before, we support all popular and many unpopular operating systems and architectures, and our free applications are available for download from the App Store and Google Play .


By tradition, the demo version of Smart IDReader SDK for Python, along with the source code for the implementation of the Telegram bot, is uploaded to Github and is available via the link .


What we need


From the SDK we need several files:



For writing Telegram-bot we chose telepot .


Recognition Core Interaction


Detailed information about the library can be found in the documentation , and now we will consider only the most necessary.


Let's connect the library and configure the recognition engine:


 #  python-   import pySmartIdEngine as se #     smartid_config_path = 'bundle_mock_smart_idreader.zip' #   ,         smartid_engine = se.RecognitionEngine(smartid_config_path) 

Now you can write an image recognition function:


 def recognize_image_file(smartid_engine, image_file_path): #          session_settings = smartid_engine.CreateSessionSettings() session_settings.AddEnabledDocumentTypes('rus.passport.national') #    session = smartid_engine.SpawnSession(session_settings) #   result = session.ProcessImageFile(image_file_path) #      dict recognized_fields = {} for field_name in result.GetStringFieldNames(): field = result.GetStringField(field_name) recognized_fields[field_name] = field.GetValue().GetUtf8String() #   JSON-   return json.dumps(recognized_fields, ensure_ascii=False, indent=2) 

The implementation of the bot to recognize the images sent to it


We will follow a simple path and use the 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 pass the previously created recognition engine so as not to waste time each time creating it:


 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:


 #   bot = telepot.DelegatorBot(args.token, [ pave_event_space()( per_chat_id(), create_open, SmartIDReaderBot, smartid_engine, timeout=1000000 ) ]) #   MessageLoop(bot).run_as_thread() while 1: time.sleep(10) 

Instead of args.token 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 Smart IDReader SDK Python interface 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 using Telegram, you can very easily recognize documents remotely. However, under Russian law, only your documents can be remotely recognized. To work with other people's documents, you must not only become an operator for processing and storing personal data, have the necessary infrastructure to protect this data, but also protect all phones and computers on which recognition takes place. Therefore, our colleagues from Sum & Substance 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 issue.


')

Source: https://habr.com/ru/post/344774/


All Articles