画像文字認識のREST API「OCR.Space」を呼び出して、指定URL上の画像を文字認識するAppleScriptです。
とりあえず試してみるためには、OCR.SpaceにレジストしてAPI Keyを取得し、プログラムリスト中に記入してください。
英数字の文字認識については実用性を感じましたが、日本語文字認識については実用的ではないと感じました。
AppleScript名:(GET)OCR Spaceで指定URL上の画像を文字認識する |
— Created 2016-10-27 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aURL to "http://piyocast.com/as/wp-content/uploads/2018/02/604b4edd7a57544c85020973f4d46eca.png" set aText to recogTextFromImageURL(aURL) of me (* "Piyomaru Software Takaaki Machine Quit MacBook Pro MacBook Air Mac mini " set myAPIkey to "XxxxXXxxXXXXXXX" –API Key set reqURLStr to "https://api.ocr.space/parse/ImageUrl" –set aRec to {apikey:myAPIkey, |language|:"jpn", |url|:aURL}–日本語の文字認識はほぼ使い物にならない set aRec to {apikey:myAPIkey, |language|:"eng", |url|:aURL} set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL) of me set aRESCode to (responseCode of aRes) as integer set aRESTres to (json of aRes) as record set errorD to ErrorDetails of aRESTres if errorD is not equal to missing value then return "" set recogText to ParsedText of (first item of (ParsedResults of aRESTres)) return recogText end recogTextFromImageURL –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(aURL) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"GET" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:"application/json" forHTTPHeaderField:"Accept" aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding" set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code & Header set dRes to contents of second item of resList if dRes is not equal to missing value then set resCode to (dRes’s statusCode()) as number set resHeaders to (dRes’s allHeaderFields()) as record else set resCode to 0 set resHeaders to {} end if return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestGETAPIAndParseResults on retURLwithParams(aBaseURL, aRec) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set qList to {} repeat with i from 1 to aLen set aName to contents of item i of aKeyList set aVal to contents of item i of aValList set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams |
More from my site
(Visited 67 times, 1 visits today)