Skip to content

Commit 5755af3

Browse files
authored
Merge pull request #9 from regulaforensics/enchance-docs
Enhance documentation
2 parents 95a2391 + 7804a88 commit 5755af3

File tree

9 files changed

+171
-108
lines changed

9 files changed

+171
-108
lines changed

.github/workflows/run-smoke-test.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ jobs:
2121
run: |
2222
python -m pip install --upgrade pip
2323
python -m pip install --upgrade setuptools wheel
24-
python -m pip install --upgrade pipenv==2018.11.26
2524
- name: Install test dependencies
26-
run: pipenv install --dev
27-
working-directory: example
25+
run: pip install -e ./
2826
- name: Run smoke test
29-
run: pipenv run python example.py
27+
run: python example.py
3028
working-directory: example
3129
env:
3230
API_BASE_PATH: "http://test-api.regulaforensics.com"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea
22
example/portrait.jpg
33
example/document-image.jpg
4+
example/regula.license
45

56

67
# Byte-compiled / optimized / DLL files

README.md

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,67 @@
1-
# Regula Document Reader web application python client
1+
# Regula Document Reader web API Python 3.5+ client
22

3-
## Development
3+
[![pypi](https://img.shields.io/pypi/v/regula.documentreader.webclient?style=flat-square)](https://support.regulaforensics.com/hc/en-us/articles/115000916306-Documentation)
4+
[![OpenAPI](https://img.shields.io/badge/OpenAPI-defs-8c0a56?style=flat-square)](https://github.com/regulaforensics/DocumentReader-web-openapi)
5+
[![documentation](https://img.shields.io/badge/docs-en-f6858d?style=flat-square)](https://support.regulaforensics.com/hc/en-us/articles/115000916306-Documentation)
6+
[![live](https://img.shields.io/badge/live-demo-0a8c42?style=flat-square)](https://api.regulaforensics.com/)
7+
8+
Documents recognition as easy as reading two bytes.
9+
10+
If you have any problems with or questions about this client, please contact us
11+
through a [GitHub issue](https://github.com/regulaforensics/DocumentReader-web-python-client/issues).
12+
You are invited to contribute [new features, fixes, or updates](https://github.com/regulaforensics/DocumentReader-web-python-client/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22), large or small;
13+
We are always thrilled to receive pull requests, and do our best to process them as fast as we can.
14+
See [dev guide](./dev.md)
15+
16+
## Install package
17+
`regula.documentreader.webclient` is on the Python Package Index (PyPI):
418

5-
To regenerate models from openapi definition,
6-
clone [latest open api definitions](https://github.com/regulaforensics/DocumentReader-api-openapi)
7-
and set `DEFINITION_FOLDER` as path to cloned directory, for example:
819
```bash
9-
DEFINITION_FOLDER="/home/user/projects/DocumentReader-api-openapi"
20+
pip install regula.documentreader.webclient
1021
```
11-
Then use next command from the project root:
22+
23+
Or using `pipenv`
1224
```bash
13-
docker run --rm -v "${PWD}:/client" -v "${DEFINITION_FOLDER}:/definitions" \
14-
openapitools/openapi-generator-cli generate -g python \
15-
-i /definitions/index.yml -o /client -c /client/generator-config.json \
16-
-t /client/generator-templates
25+
pipenv install regula.documentreader.webclient
26+
```
27+
28+
## Example
29+
Performing request:
30+
```python
31+
from regula.documentreader.webclient.ext.api import DocumentReaderApi
32+
from regula.documentreader.webclient.ext.models import *
33+
from regula.documentreader.webclient.gen.models import *
34+
35+
with open("australia_passport.jpg", "rb") as f:
36+
input_image = f.read()
37+
38+
with DocumentReaderApi(host='http://localhost:8080') as api:
39+
params = ProcessParams(
40+
scenario=Scenario.FULL_PROCESS,
41+
result_type_output=[Result.DOCUMENT_IMAGE, Result.STATUS, Result.TEXT, Result.IMAGES]
42+
)
43+
request = RecognitionRequest(process_params=params, images=[input_image])
44+
response = api.process(request)
45+
```
46+
47+
Parsing results:
48+
```python
49+
# status examples
50+
response_status = response.status
51+
doc_overall_status = "valid" if response_status.overall_status == CheckResult.OK else "not valid"
52+
53+
# text fields example
54+
doc_number_field = response.text.get_field(TextFieldType.DOCUMENT_NUMBER)
55+
doc_number_mrz = doc_number_field.get_value()
56+
doc_number_visual = doc_number_field.get_value(Source.VISUAL)
57+
doc_number_visual_validity = doc_number_field.source_validity(Source.VISUAL)
58+
doc_number_mrz_validity = doc_number_field.source_validity(Source.MRZ)
59+
doc_number_mrz_visual_matching = doc_number_field.cross_source_comparison(Source.MRZ, Source.VISUAL)
60+
61+
# images fields example
62+
normalized_input_image = response.images.document_image()
63+
portrait_field = response.images.get_field(GraphicFieldType.PORTRAIT)
64+
portrait_from_visual = portrait_field.get_value(Source.VISUAL)
65+
portrait_from_rfid = portrait_field.get_value(Source.RFID, original=True)
1766
```
67+
You can find more detailed guide and run this sample in [example](./example) folder.

dev.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Development
2+
3+
To regenerate models, clone [latest OpenAPI definitions](https://github.com/regulaforensics/DocumentReader-web-openapi)
4+
and set `DEFINITION_FOLDER` as path to cloned directory, for example:
5+
```bash
6+
DEFINITION_FOLDER="/home/user/projects/DocumentReader-web-openapi"
7+
```
8+
Then use next command from the project root:
9+
```bash
10+
docker run --rm -v "${PWD}:/client" -v "${DEFINITION_FOLDER}:/definitions" \
11+
openapitools/openapi-generator-cli generate -g python \
12+
-i /definitions/index.yml -o /client -c /client/generator-config.json \
13+
-t /client/generator-templates
14+
```

example/Pipfile

Lines changed: 0 additions & 15 deletions
This file was deleted.

example/Pipfile.lock

Lines changed: 0 additions & 64 deletions
This file was deleted.

example/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Regula Document Reader web API Python 3.5+ client
2+
3+
:bulb: Before you start: if you just want to play with an online demo, visit our [playground](https://api.regulaforensics.com).
4+
5+
:warning: NOTE: for some systems `python3` and `pip3` commands should be used, instead of `python` and `pip`.
6+
7+
Requirements:
8+
- installed python 3.5 or higher
9+
- installed [pip](https://pip.pypa.io/en/stable/installing/)
10+
11+
Verify Python and pip versions:
12+
```bash
13+
python --version
14+
> Python 3.8.2
15+
pip --version
16+
> pip 20.2.1 from /home/user/.local/lib/python3.8/site-packages/pip (python 3.8)
17+
```
18+
19+
Cloning example:
20+
```bash
21+
git clone https://github.com/regulaforensics/DocumentReader-web-python-client.git
22+
cd DocumentReader-web-python-client
23+
```
24+
25+
Setup project and download dependencies:
26+
```bash
27+
pip install -e ./
28+
```
29+
30+
### Running with local Regula Document Reader web API installation
31+
32+
Follow [the instructions](https://docs.regulaforensics.com/web/quick-start-guide) to run Regula Document Reader web API.
33+
Assuming you have successfully launched instance, use next line command to run example:
34+
```bash
35+
cd example
36+
python example.py
37+
38+
# If Regula Document Reader web API is running not on localhost, specify host via env variable:
39+
API_BASE_PATH="http://192.168.0.101:8080" python example.py
40+
```
41+
42+
### Running using Regula Document Reader web API test SaaS
43+
44+
Get your [free trial here](https://mobile.regulaforensics.com/). You should obtain `regula.license` file.
45+
Copy it to **example** folder. You are ready for running!
46+
47+
Execute example:
48+
```bash
49+
cd example
50+
API_BASE_PATH="https://test-api.regulaforensics.com" python example.py
51+
```
52+
53+
### Output
54+
This sample generates next text output:
55+
```text
56+
---------------------------------------------------------------------------
57+
Document Overall Status: not valid
58+
Document Number Visual: U0996738
59+
Document Number MRZ: U0996738
60+
Validity Of Document Number Visual: 1
61+
Validity Of Document Number MRZ: 1
62+
MRZ-Visual values comparison: 1
63+
---------------------------------------------------------------------------
64+
```
65+
Also, it creates [portrait](portrait.jpg) and [document image](document-image.jpg) pictures inside current folder.
66+
Edit on your own [example.py](./example.py), and re-run to see your results.

example/example.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
CheckResult, GraphicFieldType
77

88
host = os.getenv("API_BASE_PATH", "http://localhost:8080")
9-
license = os.getenv("TEST_LICENSE", None) # optional, used here only for smoke test purposes
9+
regula_license = os.getenv("TEST_LICENSE", None) # optional, used here only for smoke test purposes
10+
11+
# read optional local license file
12+
if os.path.isfile('regula.license') and os.access('regula.license', os.R_OK):
13+
with open("regula.license", "rb") as f:
14+
print("Found local license file. Using it for performing request...")
15+
regula_license = f.read()
1016

1117
with open("australia_passport.jpg", "rb") as f:
1218
input_image = f.read()
1319

1420
with DocumentReaderApi(host) as api:
15-
api.license = license # used here only for smoke test purposes, most clients will attach license on server side
21+
api.license = regula_license
1622

1723
params = ProcessParams(
1824
scenario=Scenario.FULL_PROCESS,
@@ -27,21 +33,20 @@
2733

2834
# text fields example
2935
doc_number_field = response.text.get_field(TextFieldType.DOCUMENT_NUMBER)
30-
doc_number_visual = doc_number_field.get_value()
31-
doc_number_mrz = doc_number_field.get_value(Source.MRZ)
36+
doc_number_mrz = doc_number_field.get_value()
37+
doc_number_visual = doc_number_field.get_value(Source.VISUAL)
3238
doc_number_visual_validity = doc_number_field.source_validity(Source.VISUAL)
3339
doc_number_mrz_validity = doc_number_field.source_validity(Source.MRZ)
3440
doc_number_mrz_visual_matching = doc_number_field.cross_source_comparison(Source.MRZ, Source.VISUAL)
3541

3642
# images fields example
3743
document_image = response.images.document_image()
38-
portrait_Field = response.images.get_field(GraphicFieldType.PORTRAIT)
39-
portrait_From_Visual = portrait_Field.get_value(Source.VISUAL)
40-
with open('portrait.jpg', 'wb') as f: f.write(portrait_From_Visual)
41-
with open('document-image.jpg', 'wb') as f: f.write(document_image)
42-
43-
# low-lvl(original) response
44-
response.low_lvl_response
44+
portrait_field = response.images.get_field(GraphicFieldType.PORTRAIT)
45+
portrait_from_visual = portrait_field.get_value(Source.VISUAL)
46+
with open('portrait.jpg', 'wb') as f:
47+
f.write(portrait_from_visual)
48+
with open('document-image.jpg', 'wb') as f:
49+
f.write(document_image)
4550

4651
print("""
4752
---------------------------------------------------------------------------

regula/documentreader/webclient/ext/api/document_reader_api.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import base64
2+
from typing import Union
3+
14
from regula.documentreader.webclient.ext.models.recognition_response import RecognitionResponse
25
from regula.documentreader.webclient.gen import ApiClient, Configuration
36
from regula.documentreader.webclient.gen.api import DefaultApi
47
from regula.documentreader.webclient.gen.models import ProcessRequest
58

9+
Base64String = str
10+
611

712
class DocumentReaderApi(DefaultApi):
813

@@ -24,12 +29,15 @@ def __exit__(self, exc_type, exc_val, exc_tb):
2429
self.api_client.close()
2530

2631
@property
27-
def license(self) -> str:
32+
def license(self) -> Base64String:
2833
return self.__license
2934

3035
@license.setter
31-
def license(self, value: str):
32-
self.__license = value
36+
def license(self, value: Union[Base64String, bytes]):
37+
if isinstance(value, bytes):
38+
self.__license = base64.b64encode(value).decode("utf-8")
39+
else:
40+
self.__license = value
3341

3442
def process(self, process_request: ProcessRequest) -> RecognitionResponse:
3543
process_request.system_info.license = self.license

0 commit comments

Comments
 (0)