Văn bản
1. Trích xuất thông tin văn bản với đầu vào URL ảnh hoặc pdf
API:
| Method | URL | 
|---|---|
| GET | https://demo.computervision.com.vn/api/v2/ocr/document/general | 
Params:
| Key | Value | Mô tả | 
|---|---|---|
img | https://example.com/image.png | URL của ảnh hoặc pdf | 
format_type | url | Loại data truyền vào, nhận giá trị: url, file, base64 | 
get_thumb | true/false | Trả về ảnh văn bản đã được cắt và căn chỉnh | 
Demo Python:
import requestsapi_key = "YOUR_API_KEY"api_secret = "YOUR_API_SECRET"image_url = 'https://example.com/image.png'response = requests.get("https://demo.computervision.com.vn/api/v2/ocr/document/general?img=%s&format_type=url&get_thumb=false"% image_url,auth=(api_key, api_secret))print(response.json())
2. Trích xuất thông tin văn bản với đầu vào file ảnh hoặc file pdf
API:
| Method | URL | content-type | 
|---|---|---|
| POST | https://demo.computervision.com.vn/api/v2/ocr/document/general | multipart/form-data | 
Params:
| Key | Value | Mô tả | 
|---|---|---|
format_type | file | Loại data truyền vào, nhận giá trị: url, file, base64 | 
get_thumb | true/false | Trả về ảnh văn bản đã được cắt và căn chỉnh | 
Body:
| Key | Type | Value | Mô tả | 
|---|---|---|---|
img | file | example.jpg | File ảnh hoặc file pdf văn bản cần trích xuất thông tin | 
Demo Python:
import requestsapi_key = "YOUR_API_KEY"api_secret = "YOUR_API_SECRET"image_path = '/path/to/your/image.jpg'response = requests.post("https://demo.computervision.com.vn/api/v2/ocr/document/general?format_type=file&get_thumb=false",auth=(api_key, api_secret),files={'img': open(image_path, 'rb')})print(response.json())
3. Trích xuất thông tin văn bản với đầu vào JSON
API:
| Method | URL | content-type | 
|---|---|---|
| POST | https://demo.computervision.com.vn/api/v2/ocr/document/general | application/json | 
Params:
| Key | Value | Mô tả | 
|---|---|---|
format_type | base64 | Loại data truyền vào, nhận giá trị: url, file, base64 | 
get_thumb | true/false | Trả về ảnh văn bản đã được cắt và căn chỉnh | 
Body:
{"img": "iVBORw0KGgoAAAANSU..." // string base64 của ảnh hoặc pdf cần trích xuất}
Demo Python:
import base64import ioimport requestsfrom PIL import Imagedef get_byte_img(img):img_byte_arr = io.BytesIO()img.save(img_byte_arr, format='PNG')encoded_img = base64.encodebytes(img_byte_arr.getvalue()).decode('ascii')return encoded_imgapi_key = "YOUR_API_KEY"api_secret = "YOUR_API_SECRET"img_name = "path_img"encode_cmt = get_byte_img(Image.open(img_name))response = requests.post("https://demo.computervision.com.vn/api/v2/ocr/document/general?format_type=base64&get_thumb=false",auth=(api_key, api_secret),json={'img' : encode_cmt})print(response.json())
4. Thông tin trả về
Phản hồi sẽ là một JSON với định dạng sau:
{"data": [xxxx],"errorCode": string, // Mã lỗi"errorMessage": string // Thông báo lỗi}
Trường hợp trích xuất thông tin từ văn bản scan, trường data sẽ là một list, mỗi phần tử trong list sẽ tương ứng với thông tin của 1 trang trong file pdf hoặc của 1 ảnh. Mỗi phần tử trong list này được biểu thị như sau:
[// List các block trong cùng một trang[// List các line trong cùng một block[// List các text trong cùng một line (*)],],];
Mỗi phần tử text (*) bao gồm các trường sau:
{"text": string, // Nội dung của text"confidence": float, // Độ tin cậy của text"box": {"left": int, // Tọa độ bên trái của text"right": int, // Tọa độ bên phải của text"top": int, // Tọa độ bên trên của text"bottom": int // Tọa độ bên dưới của text}}
Bảng mã lỗi:
| Mã lỗi | Message | Mô tả | 
|---|---|---|
| 0 | Success | Trích xuất thông tin thành công | 
| 1 | The photo does not contain content | Ảnh đầu vào không có nội dung cần trích xuất | 
| 2 | Url is unavailable | Download ảnh bị lỗi khi dùng GET | 
| 3 | Incorrect image format | Upload ảnh bị lỗi khi dùng POST | 
| 4 | Out of requests | Hết số lượng request | 
| 5 | Incorrect api_key or api_secret | Khi api_key hoặc api_secret sai | 
| 6 | Incorrect format type | Loại format khai báo trong format_type không đúng với ảnh truyền vào |