structocr 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
structocr/__init__.py
ADDED
structocr/client.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import os
|
|
3
|
+
import base64
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
class StructOCR:
|
|
7
|
+
"""
|
|
8
|
+
StructOCR Python Client
|
|
9
|
+
Get your API Key at: https://structocr.com
|
|
10
|
+
"""
|
|
11
|
+
def __init__(self, api_key=None, base_url="https://api.structocr.com/v1"):
|
|
12
|
+
# Allow reading API Key from environment variables for better DX
|
|
13
|
+
self.api_key = api_key or os.environ.get('STRUCTOCR_API_KEY')
|
|
14
|
+
if not self.api_key:
|
|
15
|
+
raise ValueError("API Key is required. Get one at https://structocr.com")
|
|
16
|
+
|
|
17
|
+
self.base_url = base_url.rstrip('/')
|
|
18
|
+
self.session = requests.Session()
|
|
19
|
+
|
|
20
|
+
# Updated headers based on your API specification
|
|
21
|
+
self.session.headers.update({
|
|
22
|
+
"x-api-key": self.api_key,
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
"User-Agent": "StructOCR-Python/1.0.0"
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
def _post_image(self, endpoint, file_path):
|
|
28
|
+
"""
|
|
29
|
+
Internal method: Handle image encoding and API request.
|
|
30
|
+
"""
|
|
31
|
+
url = f"{self.base_url}/{endpoint}"
|
|
32
|
+
|
|
33
|
+
if not os.path.exists(file_path):
|
|
34
|
+
raise FileNotFoundError(f"File not found: {file_path}")
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
# 1. Encode image to Base64
|
|
38
|
+
with open(file_path, "rb") as image_file:
|
|
39
|
+
base64_image = base64.b64encode(image_file.read()).decode('utf-8')
|
|
40
|
+
|
|
41
|
+
# 2. Prepare JSON payload
|
|
42
|
+
payload = {
|
|
43
|
+
"img": base64_image
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
# 3. Send Request
|
|
47
|
+
response = self.session.post(url, json=payload)
|
|
48
|
+
response.raise_for_status() # Raise error for 4xx/5xx responses
|
|
49
|
+
|
|
50
|
+
return response.json()
|
|
51
|
+
|
|
52
|
+
except requests.exceptions.RequestException as e:
|
|
53
|
+
# Handle connection errors or API errors
|
|
54
|
+
raise Exception(f"API Request failed: {str(e)}")
|
|
55
|
+
|
|
56
|
+
# --- Public Methods for Developers ---
|
|
57
|
+
|
|
58
|
+
def scan_passport(self, file_path):
|
|
59
|
+
"""
|
|
60
|
+
Scan a Passport image.
|
|
61
|
+
path: Path to the passport image file.
|
|
62
|
+
Returns: Structured JSON data.
|
|
63
|
+
"""
|
|
64
|
+
# Endpoint: /v1/passport
|
|
65
|
+
return self._post_image('passport', file_path)
|
|
66
|
+
|
|
67
|
+
def scan_national_id(self, file_path):
|
|
68
|
+
"""
|
|
69
|
+
Scan a National ID card.
|
|
70
|
+
path: Path to the ID card image file.
|
|
71
|
+
Returns: Structured JSON data.
|
|
72
|
+
"""
|
|
73
|
+
# Endpoint: /v1/national-id (Assuming this is your endpoint naming)
|
|
74
|
+
return self._post_image('national-id', file_path)
|
|
75
|
+
|
|
76
|
+
def scan_driver_license(self, file_path):
|
|
77
|
+
"""
|
|
78
|
+
Scan a Driver License.
|
|
79
|
+
path: Path to the driver license image file.
|
|
80
|
+
Returns: Structured JSON data.
|
|
81
|
+
"""
|
|
82
|
+
# Endpoint: /v1/driver-license (Assuming this is your endpoint naming)
|
|
83
|
+
return self._post_image('driver-license', file_path)
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: structocr
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: The official Python SDK for StructOCR API - Passport, ID, and Driver License OCR.
|
|
5
|
+
Home-page: https://github.com/structocr/structocr-python
|
|
6
|
+
Author: StructOCR Team
|
|
7
|
+
Author-email: support@structocr.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
12
|
+
Requires-Python: >=3.6
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: requests>=2.25.0
|
|
15
|
+
Dynamic: author
|
|
16
|
+
Dynamic: author-email
|
|
17
|
+
Dynamic: classifier
|
|
18
|
+
Dynamic: description
|
|
19
|
+
Dynamic: description-content-type
|
|
20
|
+
Dynamic: home-page
|
|
21
|
+
Dynamic: requires-dist
|
|
22
|
+
Dynamic: requires-python
|
|
23
|
+
Dynamic: summary
|
|
24
|
+
|
|
25
|
+
# StructOCR Python SDK
|
|
26
|
+
|
|
27
|
+
[](https://badge.fury.io/py/structocr)
|
|
28
|
+
[](https://opensource.org/licenses/MIT)
|
|
29
|
+
|
|
30
|
+
**The official Python client for [StructOCR](https://structocr.com).**
|
|
31
|
+
|
|
32
|
+
StructOCR is a powerful API tailored for developers to extract structured data from identity documents with high accuracy. This SDK helps you integrate **Passport OCR**, **National ID OCR**, and **Driver License OCR** into your Python applications in minutes.
|
|
33
|
+
|
|
34
|
+
👉 **[Get your Free API Key here](https://structocr.com)**
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- **Passport OCR API**: Instantly extract MRZ, name, DOB, and expiry date from passports of 200+ countries.
|
|
39
|
+
- **National ID OCR**: Support for ID cards with automatic field mapping.
|
|
40
|
+
- **Driver License OCR**: Extract vehicle class, license number, and personal details.
|
|
41
|
+
- **Secure & Fast**: Enterprise-grade encryption and sub-second response times.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Install the package via pip:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install structocr
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
### 1. Initialize the Client
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from structocr import StructOCR
|
|
58
|
+
|
|
59
|
+
# Initialize with your API Key
|
|
60
|
+
client = StructOCR(api_key="sk_live_xxxxxxxx")
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. Scan a Passport (Passport OCR)
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
# Pass the path to the passport image file
|
|
68
|
+
result = client.scan_passport('./docs/passport_sample.jpg')
|
|
69
|
+
|
|
70
|
+
print(f"Name: {result['data']['name']}")
|
|
71
|
+
print(f"Passport Number: {result['data']['document_number']}")
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Scan a National ID or Driver License
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
# National ID OCR
|
|
79
|
+
id_data = client.scan_national_id('./docs/id_card.png')
|
|
80
|
+
|
|
81
|
+
# Driver License OCR
|
|
82
|
+
license_data = client.scan_driver_license('./docs/license.jpg')
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Documentation
|
|
87
|
+
|
|
88
|
+
For full API documentation, response examples, and error codes, please visit the [StructOCR Developer Docs](https://www.structocr.com/developers?ref=github).
|
|
89
|
+
|
|
90
|
+
## Requirements
|
|
91
|
+
|
|
92
|
+
* Python 3.6+
|
|
93
|
+
* `requests` library
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT License. See [LICENSE](https://www.google.com/search?q=LICENSE) for details.
|
|
98
|
+
|
|
99
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
structocr/__init__.py,sha256=q0dim8G5HQBYxPY6nSv9zkz1UMbAsvmmTseENFBNubc,54
|
|
2
|
+
structocr/client.py,sha256=sfEKUvZBuaZ-Uk-hC459jhYInMTT0uo59mY75klt7Jo,2820
|
|
3
|
+
structocr-1.0.0.dist-info/METADATA,sha256=ivqH-C0mI-jrD6m7kI4Ck19cvre6V7LTl9GGSl83gi0,2817
|
|
4
|
+
structocr-1.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
5
|
+
structocr-1.0.0.dist-info/top_level.txt,sha256=r71IAG7a0LhuKvYrrZDO-2QVLYuT-Dzyle4k4gHFJQw,10
|
|
6
|
+
structocr-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
structocr
|