We, Smart Engines , continue the series of articles on how to integrate our recognition technologies ( passports , bank cards and others) into your applications. Earlier we wrote about embedding on iOS and Android , showed how to embed recognition in the Telegram bot , and today we will talk about how to work with Python and PHP interfaces of the Smart IDReader recognition library for use on an in-house server.
By the way, the list of programming languages ​​that we support, in addition to those discussed here, includes C #, Objective-C, Swift, and Java. As before, we support all popular and many unpopular operating systems and architectures, and our free demo applications are available for download from the App Store and Google Play .
By tradition, the demo version of the Smart IDReader SDK for Python and PHP along with examples is available on Github and is available here.
Putting a Wrap
Since the version of the plug-in must match the version of the interpreter, we do not supply a ready-made module, but an auto-assembler that allows you to collect and deploy the necessary Smart IDReader recognition library wrapper on your machine (Python 2/3 and PHP 5/7 are supported). To do this, run the appropriate assembly script, indicating the path to our library libsmartidEngine.so and the version of the interpreter for which you need to build the module. For example, for Python, it looks like this:
bash build_python.sh ../../bin 3
PHP is going in a similar way, only the second argument is passed the path to the php-config you use:
bash build_php.sh ../../bin /usr/bin/php56-config
Important: after the assembly, the resulting module will reference libSmartidEngine.so in the absolute path, so first install the library in a directory convenient for you!
The auto assembler will unpack the SWIG supplied in the assembly, generate a module and verify it by running the test.
The library wrapper includes two files - a module written in Python / PHP, and an extension for the interpreter (_pySmartIdEngine.so for Python and phpSmartidEngine.so). To integrate recognition into the project, you need to import the module and connect the extension.
In the case of Python, for this it is enough to explicitly write the path to the module and extension:
sys.path.append(os.path.join(sys.path[0], '../../bin/')) sys.path.append(os.path.join(sys.path[0], '../../bindings/')) import pySmartIdEngine
In PHP, a module is imported the same way:
require(“phpSmartIdEngine.php");
but with the extension it’s a bit more complicated - dynamic linking doesn’t work in some versions, so to work constantly with the extension you need to put it in the folder where all the extensions for your version are located (for example / usr / lib / php56 / modules) and connect, writing in default php.ini
extension=phpSmartisEngine.so
In the GitHub example, we just tell the interpreter to use our php.ini with the extension already registered
php56 -c php.ini smartid_sample.php
So, we figured out the connection, feel free to study an example!
Learning the interface
The example starts with three arguments - the path to the image to be recognized, the path to the archive with the configuration for the engine and the type of document to be recognized (tests run the example for rus.passport.national (passport of a citizen of the Russian Federation))
python smartid_sample.py ../../testdata/passport_rf_2.jpg ../../data-zip/bundle_mock_smart_idreader.zip rus.passport.national
How it works:
# (zip-, ) engine = pySmartIdEngine.RecognitionEngine(config_path) # session_settings = engine.CreateSessionSettings() session_settings.SetEnabledDocumentTypes(document_types) # session = engine.SpawnSession(session_settings) # resultFromImageFile = session.ProcessImageFile(image_path) session.Reset() # output_recognition_result(resultFromImageFile)
We will dwell on the methods of loading the image in more detail: it can be the path to the file, a line in Base64 and a buffer in memory (both data in RGB \ YUV NV21 formats and the jpeg file itself). Buffers can be external, as well as they can be extracted from an object of the Image class (after creating it by specifying the path to the file or using the same buffer)
Important: the picture itself in RGB or YUV format can be accessed only in the Python module using a bytearray object (create an object of the required size, pass it to the CopyToBuffer method, see an example), in PHP you can only work with a Base64 format string!
Entirely it looks like this (in Python):
engine = pySmartIdEngine.RecognitionEngine(config_path) session_settings = engine.CreateSessionSettings() session_settings.SetEnabledDocumentTypes(document_types) # Image ( ) testImage = pySmartIdEngine.Image(image_path) # , binaryRGBfile = open(image_path, "rb") size = os.path.getsize(image_path) binaryRGBfileBuffer = bytearray(size) binaryRGBfile.readinto(binaryRGBfileBuffer) # , Base64 f = open(os.path.join(sys.path[0],"base64.txt"), 'r') base64BufferString = f.readline() f.close() # Base64- # base64BufferString = testImage.GetBase64String() # session = engine.SpawnSession(session_settings) # resultFromImage = session.ProcessImage(testImage) session.Reset() resultFromImageFile = session.ProcessImageFile(image_path) session.Reset() resultFromImageData = session.ProcessImageData(binaryRGBfileBuffer, size) session.Reset() resultFromBase64 = session.ProcessImageDataBase64(base64BufferString) session.Reset() # output_recognition_result(resultFromImage) output_recognition_result(resultFromImageFile) output_recognition_result(resultFromImageData) output_recognition_result(resultFromBase64)
Python and PHP modules allow you to take advantage of all the functionality provided by the C ++ interface of our library (with the exception of working with a buffer in PHP, as described above).
Conclusion
We examined working with the SmartIdEngine SDK in Python and PHP, in the examples we reflected all the difficulties that you may encounter in the process of deploying the module on a working machine. The Mock version of the library, presented on the github, allows you to demonstrate the mechanism for deploying the module and study their interface without any recognition functionality. For a trial version, please contact us: support@smartengines.ru