📜 ⬆️ ⬇️

Recognize checks in Google Docs using the ABBYY OCR SDK

In the northern hemisphere, it is winter now, which means it's time to ski! My friends and I succumbed to this trend and set about preparing our joint winter holiday. I will not disclose the details of the entire planning process; I will only say that I had the responsible role of collecting the checks of our joint expenses.

As a sandbox for our plans were made by Google Docs, and more specifically Tables, I wanted to place the photos of all the checks on a separate sheet. The first attempt to insert a picture into the table through the usual menu item "Insert → Image" led to the fact that the check was on top of the cells of the table and my friends, IT friends, would not like the design:



“There must be a way to insert a picture into a cell ...”, I thought. Google found this method from the first query: the IMAGE function (URL), like the value of a cell. Well, it's done:
')


You can go further to watch ski lessons. Although…

... Google immediately responded to my request for the “Online FineReader API” and issued several articles about the ABBYY Cloud OCR SDK. A quick scan of the available methods of the platform confirmed my expectations and the desired ocrsdk.com/documentation/apireference/processReceipt was found. For the implementation of the conception, there was a lack of understanding how to insert a user-defined function into the table cell, which will call the check processing methods. But even here the search did not fail me and the Google Script Editor was found.

Now it's up to you. The first step is to create the application ocrsdk. This is done in the process of registering a new user, so there are no problems with this. An email with the application password, which is required to authenticate requests, was sent immediately after registration was completed.

Further in our Table, select the menu item “Tools → Script Editor” and create the processReceipt (imageUrl) function:

function processReceipt(imageUrl) { //    var image = UrlFetchApp.fetch(imageUrl); //   ,        var pass = "GoogleDriveTest" + ":" + "********************" //  POST     var url = "http://cloud.ocrsdk.com/processReceipt"; var headers = { "Content-Type":"image/png", "Authorization" : "Basic " + Utilities.base64Encode(pass) }; var options = { "method":"POST", "headers": headers, "payload" : image.getContent() }; var response = UrlFetchApp.fetch(url, options); //  XML      ID      (   http://ocrsdk.com/documentation/apireference/processReceipt/) var document = XmlService.parse(response.getContentText()) var id = document.getRootElement().getChildren()[0].getAttribute('id').getValue() var resultUrl //       url        do { Utilities.sleep(3000) url = "http://cloud.ocrsdk.com/getTaskStatus" + "?taskId=" + id; headers = { "Authorization" : "Basic " + Utilities.base64Encode(pass) }; options = { "method":"GET", "headers": headers, }; response = UrlFetchApp.fetch(url, options); document = XmlService.parse(response.getContentText()); if (document.getRootElement().getChildren()[0].getAttribute('status').getValue() == 'Completed') { resultUrl = document.getRootElement().getChildren()[0].getAttribute('resultUrl').getValue() break } } while(true) //     options = { "method":"GET", }; response = UrlFetchApp.fetch(resultUrl, options); document = XmlService.parse(response.getContentText()); // document  XML  . -     findTotalPriceInReceiptXML   :) result = findTotalPriceInReceiptXML(document) return result } 

We save everything and go back to the table. In order for this miracle to work, I had to put the URL of the check picture in a separate cell of the table. Our function is now available as = processReceipt (cell with URL).



All good winter weather and good luck on the ski slopes!

Source: https://habr.com/ru/post/319042/


All Articles