Synchronous detection

更新时间:
复制 MD 格式

This topic describes how to use the synchronous image detection API to recognize structured receipts with Optical Character Recognition (OCR). The API recognizes text in receipt images and returns the text content in a structured format. Before you use this feature, you must know the type of receipt in the image.

This feature supports the following receipt types: VAT invoices, quota invoices, train tickets, and taxi receipts.

Usage notes

API endpoint: /green/image/scan. This API performs synchronous image detection.

You can call this API to create a synchronous image detection task. For information about how to construct an HTTP request, see Request structure. You can also use a pre-constructed HTTP request. For more information, see SDK overview.

  • Billing information

    You are charged for calling this operation. For more information about the billing methods, see

  • Detection timeout

    The maximum response time that is allowed for a synchronous moderation request is 6 seconds. If the moderation is not completed within 6 seconds, a timeout error is returned. If you do not require moderation results in real time, you can send asynchronous moderation requests. In most cases, we recommend that you send synchronous moderation requests because synchronous moderation operations are easier to call. We recommend that you set the timeout period to 6 seconds for calling synchronous moderation operations.

  • Return results

    In general, moderation results are returned within 1 second after you send a synchronous moderation request. The time may increase in special scenarios where a large number of requests are to be processed in the system, the size of images is large, or the images contain a large number of words. The speed of OCR is inversely relevant to the number of words in the images. If the images to be moderated contain a large number of words, we recommend that you send asynchronous moderation requests.

  • Image requirements
    • The URLs of images must be HTTP or HTTPS URLs.

    • The images must be in PNG, JPG, JPEG, BMP, GIF, or WEBP format.

    • An image can be up to 20 MB in size. The limit for the image size is applicable to both synchronous and asynchronous moderation operations.

      ,高度或者宽度不能超过30,000像素(px),且图像总像素不超过2.5亿(px)

      Note

      其中,GIF格式的图片,图像总像素不超过4,194,304(px),高度或者宽度不能超过30,000像素(px)。

    • The duration for downloading an image is limited to 3 seconds. If an image fails to be downloaded within 3 seconds, a timeout error is returned.

    • We recommend that you submit images of at least 256 × 256 pixels to ensure the moderation effect.

    • The response time of an operation for moderating images varies based on the duration for downloading these images. Make sure that you use a stable and reliable storage service to store the images to be moderated. We recommend that you use Object Storage Service (OSS) or Content Delivery Network (CDN).

QPS limits

You can call this operation up to 10 times per second per account. If the number of calls per second exceeds the limit, throttling is triggered. As a result, your business may be affected. We recommend that you take note of the limit when you call this operation.

Request parameters

NameTypeRequiredExampleDescription
bizTypeStringNodefault

The business scenario. You can create a business scenario in the

内容安全控制台. For more information, see Customize policies for machine-assisted moderation.

scenesStringArrayYes["ocr"]The detection scenario. Set the value to ocr.
tasksJSONArrayYesThe detection objects. Each element in the JSON array is a detection task struct. You can submit up to 100 tasks in a single request. To submit 100 tasks, you must set the number of concurrent tasks to more than 100. For more information about the structure of each element, see task.
extrasJSONObjectYes{"card":"invoice"}The type of receipt to recognize. The format is {"card":"${CardType}"}. CardType is the receipt type. Valid values:
  • invoice: VAT invoice
  • quota-account-permit: Quota invoice
  • train-ticket: Train ticket
  • taxi-ticket: Taxi receipt
Table 1. task
NameTypeRequiredExampleDescription
dataIdStringNotest_data_xxxxThe data ID. All IDs in a request must be unique.
urlStringYeshttps://aliyundoc.com/test_image_xxxx.pngThe URL of the image to detect.

Response data

NameTypeExampleDescription
codeInteger200

The returned HTTP status code.

For more information, see Common error codes.

msgStringOKThe response message for the request.
dataIdStringtest_data_xxxx

The ID of the moderation object.

Note

If you set the dataId parameter in the moderation request, the value of the dataId request parameter is returned here.

taskIdStringimg1CVBf$Vgrno7wF75H5LaRm-1pP****The ID of the detection task.
urlStringhttps://aliyundoc.com/test_image_xxxx.pngThe URL of the detection object.
extrasJSONObject{"card":"invoice"}

If you set the extras parameter in the moderation request, the value of the extras request parameter is returned here.

Note

This parameter may be subject to changes. Use the latest value of this parameter.

resultsArrayThe returned results. If the call is successful (code=200), the results contain one or more elements. Each element is a struct. For a description of the structure, see result.
Table 2. result
NameTypeExampleDescription
sceneStringocrThe detection scenario. The value is ocr.
labelStringocrThe classification of the detection result. Valid values:
  • normal: No text is recognized in the image.
  • ocr: Text is recognized in the image.
suggestionStringreviewThe recommended user action. Valid values:
  • pass: The return value is ignored.
  • review: Review the recognized text.
rateFloat99.91In the OCR scenario, you can ignore this return value.
invoiceInfoObjectThe recognized VAT invoice information. For a description of the structure, see invoiceInfo.
Note This parameter is returned only when you set extras to {"card":"invoice"} in the request.
ocrCardInfoObjectThe OCR information of other recognized receipts. For a description of the structure, see ocrCardInfo.
Note This parameter is returned only when you specify one of the following values for extras in the request:
  • {"card":"quota-account-permit"}: Returns quota invoice information.
  • {"card":"train-ticket"}: Returns train ticket information.
  • {"card":"taxi-ticket"}: Returns taxi receipt information.
Table 3. VAT invoice information (invoiceInfo)
NameTypeExampleDescription
invoice_nameString**VAT Special InvoiceInvoice name.
invoice_idInteger110015****Invoice code.
invoice_numberInteger5795****Invoice number.
invoice_dateInteger20170422Invoice date.
invoice_bcompanyString**** Software Co., Ltd.Purchaser name.
invoice_bcodeString91330000000000****Purchaser's taxpayer identification number.
invoice_baddressString****Purchaser's address and phone number.
invoice_baccountString** Bank, a certain branch ****Purchaser's bank and account number.
invoice_passwordString1<000+0000-0000>79*27<>7295<-123020-8>07+63*683-80+>314-9+0+/<0-<-/1++35*958*6>98709/+9323858+8745<72461****Invoice password.
invoice_detailArrayInvoice details.
invoice_totalamountFloat4222.22Total amount.
invoice_totaltaxFloat717.78Total tax.
invoice_totalamountupperStringFour Thousand Nine Hundred and Forty Yuan OnlyTotal price and tax (in words).
invoice_totalamountlowerString4940.00Total price and tax (in figures).
invoice_scompanyString**** Co., Ltd.Seller name.
invoice_scodeString91110000000000****Seller's taxpayer identification number.
invoice_saddressString****Seller's address and phone number.
invoice_saccountString**** Branch 1105011012340000****Seller's bank and account number.
invoice_remarkStringKP12****Remarks.
invoice_payeeStringxxxPayee.
invoice_reviewerStringxxxReview
invoice_drawerStringMs. ZhangDrawer.
Table 4. VAT invoice details (invoice_detail)
NameTypeExampleDescription
detail_nameStringRongli Longya Flower TeaName of goods or taxable services.
detail_typeStringxxxSpecifications.
detail_unitStringxxxUnit.
detail_countFloat20Quantity.
detail_unitpriceFloat44.4444444Unit price.
detail_amountFloat888.89Amount.
detail_rateString17%Tax rate.
detail_taxFloat151.11Tax amount.
ocrCardInfo
  • Table 5. Quota invoice information
    NameTypeExampleDescription
    amountInWordsStringOne Hundred YuanAmount in words.
    invoiceSerialStringParsed invoice code.
    amountInFiguresString100Amount in figures.
    invoiceNumberString0145****Invoice number.
    invoiceCodeString15113159****Invoice code.
    batchNumberString01Batch number.
    yearString2015Year.
    taxBureauCodeStringNational TaxTax bureau code.
    invoiceIndustryCodeStringNoneInvoice industry code.
    amountVersionStringComputerized versionAmount version.
    regionalismCodeStringNanchong CityAdministrative region code.
    invoiceCategoryCodeStringInvoice Exchange VoucherInvoice category code.
    typeStringquota-invoiceThe type of the receipt. The value is fixed at quota-invoice, which indicates a quota invoice.
  • Table 6. Train ticket information
    NameTypeExampleDescription
    priceString0.0Price.
    ticketNumberStringA000001Ticket number.
    departureTimeStringDeparts at 00:00 on January 01, 2019Departure time.
    seatTypeStringSecond Class SeatSeat type.
    trainNumberStringG0000Train number.
    departureStringOrigin StationDeparture station.
    departurePhonicsStringShifaDeparture station (Pinyin).
    seatNumberStringCar 01, Seat 01FSeat number.
    destinationStringDestination StationArrival station.
    destinationPhonicsStringZhongdaoArrival station (Pinyin).
    passengerInfoString1234567890****0000NamePassenger information.
    typeStringtrain-ticketThe type of the receipt. The value is fixed at train-ticket, which indicates a train ticket.
  • Table 7. Taxi receipt information
    NameTypeExampleDescription
    actualCollectionString100Amount received.
    carNumberStringA•T****License plate number.
    dateStringSeptember 16, 2008Date.
    invoiceCodeString25000071****Invoice code.
    invoiceNumbeString5485G0****Invoice number.
    mileageString42.0kmMileage.
    amountString100Amount.
    getOffTimeString16:58Drop-off time.
    boardingTimeString[K068016:50Pick-up time.
    totalAmountString100Total.
    typeStringtaxi-ticketThe type of the receipt. The value is fixed at taxi-ticket, which indicates a taxi receipt.

Examples

Request example
Note This example shows how to recognize a VAT invoice (extras is set to {"card": "invoice"}). To recognize other types of receipts, set `extras` to the corresponding receipt type. For more information, see Request parameters.
http(s)://[Endpoint]/green/image/scan
&<Common request parameters>
{
    "scenes": [
        "ocr"
    ],
    "extras": {
        "card": "invoice"
    },
    "tasks": [
        {
            "dataId": "test_data_xxxx",
            "url": "https://aliyundoc.com/test_image_xxxx.png"
        }
    ]
}
Successful response examples
  • VAT invoice information
    {
        "msg":"OK",
        "code":200,
        "data":[
            {
                "msg":"OK",
                "code":200,
                "dataId":"test_data_xxxx",
                "extras":{
                    "md5":"303f7159179153a94033724ffd28ea44"
                },
                "results":[
                    {
                        "invoiceInfo":{
                            "invoice_bcompany":"**** Software Co., Ltd.",
                            "invoice_baccount":"** Bank, a certain branch ****",
                            "invoice_drawer":"Ms. Zhang",
                            "invoice_totalamountupper":"Four Thousand Nine Hundred and Forty Yuan Only",
                            "invoice_name":"** VAT Special Invoice",
                            "invoice_date":"20170422",
                            "invoice_totaltax":"717.78",
                            "invoice_reviewer":"",
                            "invoice_totalamountlower":"4940.00",
                            "invoice_id":"110015****",
                            "invoice_number":"5795****",
                            "invoice_scompany":"**** Co., Ltd.",
                            "invoice_saccount":"**** Branch 1105011012340000****",
                            "invoice_saddress":"****",
                            "invoice_remark":"KP12****",
                            "invoice_totalamount":"4222.22",
                            "invoice_password":"1<000+0000-0000>79*27<>7295<-123020-8>07+63*683-80+>314-9+0+/<0-<-/1++35*958*6>98709/+9323858+8745<72461****",
                            "invoice_payee":"",
                            "invoice_detail":[
                                {
                                    "detail_amount":"888.89",
                                    "detail_name":"Rongli Longya Flower Tea",
                                    "detail_rate":"17%",
                                    "detail_count":"20",
                                    "detail_type":"",
                                    "detail_unitprice":"44.4444444",
                                    "detail_tax":"151.11",
                                    "detail_unit":""
                                },
                                {
                                    "detail_amount":"3333.33",
                                    "detail_name":"Dafo Longjing Green Tea",
                                    "detail_rate":"17%",
                                    "detail_count":"50",
                                    "detail_type":"",
                                    "detail_unitprice":"66.6666667",
                                    "detail_tax":"566.67",
                                    "detail_unit":""
                                }
                            ],
                            "invoice_baddress":"****",
                            "invoice_bcode":"91330000000000****",
                            "invoice_scode":"91110000000000****"
                        },
                        "rate":99.91,
                        "suggestion":"review",
                        "label":"ocr",
                        "scene":"ocr"
                    }
                ],
                "taskId":"img1CVBf$Vgrno7wF75H5LaRm-1p****",
                "url":"https://aliyundoc.com/test_image_xxxx.png"
            }
        ],
        "requestId":"81121B0B-2BFE-44BA-9825-C15CA5E99551"
    }
  • Train ticket information
    {
        "msg": "OK",
        "code": 200,
        "data": [
            {
                "msg": "OK",
                "code": 200,
                "dataId": "test_data_xxxx",
                "extras": {
    
                },
                "results": [
                    {
                        "rate": 99.91,
                        "suggestion": "review",
                        "label": "ocr",
                        "ocrCardInfo": {
                            "departureTime": "Departs at 00:00 on January 01, 2019",
                            "ticketNumber": "A000001",
                            "seatType": "Second Class Seat",
                            "price": "0.0",
                            "destination": "Destination Station",
                            "passengerInfo": "1234567890****0000Name",
                            "trainNumber": "G0000",
                            "departure": "Origin Station",
                            "destinationPhonics": "Zhongdao",
                            "type": "train-ticket",
                            "seatNumber": "Car 01, Seat 01F",
                            "departurePhonics": "Shifa"
                        },
                        "scene": "ocr"
                    }
                ],
                "taskId": "img5ubtZeVSXxd7010wn9mHhd-1s****",
                "url": "https://aliyundoc.com/test_image_xxxx.png"
            }
        ],
        "requestId": "C55D1390-B1D0-4B27-83F3-7335006525E2"
    }
  • Quota invoice information
    {
        "msg": "OK",
        "code": 200,
        "data": [
            {
                "msg": "OK",
                "code": 200,
                "dataId": "test_data_xxxx",
                "extras": {
    
                },
                "results": [
                    {
                        "rate": 99.91,
                        "suggestion": "review",
                        "label": "ocr",
                        "ocrCardInfo": {
                            "amountInWords": "One Hundred Yuan",
                            "amountInFigures": "",
                            "invoiceSerial": {
                                "amountVersion": "Computerized version",
                                "year": "2015",
                                "invoiceCategoryCode": "Invoice Exchange Voucher",
                                "taxBureauCode": "National Tax",
                                "invoiceIndustryCode": "None",
                                "regionalismCode": "Nanchong City",
                                "batchNumber": "01"
                            },
                            "invoiceNumber": "0145****",
                            "type": "quota-invoice",
                            "invoiceCode": "15113159****"
                        },
                        "scene": "ocr"
                    }
                ],
                "taskId": "imgzX$vkjvXxi4o74s5NEBba-1s****",
                "url": "https://aliyundoc.com/test_image_xxxx.png"
            }
        ],
        "requestId": "B5E137FF-668F-48D9-90F7-CE55A9FCC89D"
    }
  • Taxi receipt information
    {
        "msg": "OK",
        "code": 200,
        "data": [
            {
                "msg": "OK",
                "code": 200,
                "dataId": "test_data_xxxx",
                "extras": {
    
                },
                "results": [
                    {
                        "rate": 99.91,
                        "suggestion": "review",
                        "label": "ocr",
                        "ocrCardInfo": {
                            "carNumber": "A•T****",
                            "date": "September 16, 2008",
                            "totalAmount": "",
                            "getOffTime": "16:58",
                            "amount": "",
                            "invoiceNumber": "5485G0****",
                            "type": "taxi-ticket",
                            "boardingTime": "[K068016:50",
                            "invoiceCode": "25000071****",
                            "actualCollection": "",
                            "mileage": "42.0km"
                        },
                        "scene": "ocr"
                    }
                ],
                "taskId": "img1FrkyyOQXK$7YvhRGMCWb9-1s****",
                "url": "https://aliyundoc.com/test_image_xxxx.png"
            }
        ],
        "requestId": "D1E56B7A-3876-4284-939A-8004B150D879"
    }