[OCR] 画像ファイルのOCR (ドロップレット)
あくまでも参考にしてください
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #Automator用のをドロップレットにしました |
004 | # スクリプトエディタで開く |
005 | # ファイル>別名で保存>アプリケーション で保存してください |
006 | # OCRの結果は上書きされます |
007 | #com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use AppleScript version "2.8" |
010 | use framework "Foundation" |
011 | use framework "AppKit" |
012 | use framework "UniformTypeIdentifiers" |
013 | use framework "CoreImage" |
014 | use framework "VisionKit" |
015 | use framework "Vision" |
016 | use scripting additions |
017 | property refMe : a reference to current application |
018 | property refNSNotFound : a reference to 9.22337203685477E+18 + 5807 |
019 | |
020 | ##Wクリックで開いたら |
021 | on run |
022 | #デスクトップ |
023 | set appFileManager to refMe's NSFileManager's defaultManager() |
024 | set ocidUserDesktopPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
025 | set ocidUserDesktopPathURL to ocidUserDesktopPathURLArray's firstObject() |
026 | set aliasUserDesktopPath to (ocidUserDesktopPathURL's absoluteURL()) as alias |
027 | #ダイアログを前面に出す |
028 | set strName to (name of current application) as text |
029 | if strName is "osascript" then |
030 | tell application "Finder" to activate |
031 | else |
032 | tell current application to activate |
033 | end if |
034 | set listUTl to {"public.image"} as list |
035 | set strPromptText to "ファイルを選んでください" as text |
036 | set strMesText to "ファイルを選んでください" as text |
037 | set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasUserDesktopPath) of type listUTl with invisibles and multiple selections allowed without showing package contents) as list |
038 | #Openに渡す |
039 | open listAliasFilePath |
040 | |
041 | end run |
042 | |
043 | |
044 | to open listAliasFilePath |
045 | #ファイルの数だけ繰り返す |
046 | repeat with argAliasFilePath in listAliasFilePath |
047 | ##入力ファイル |
048 | set strFilePath to (POSIX path of argAliasFilePath) as text |
049 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
050 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
051 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
052 | ########################### |
053 | #データの読み込み |
054 | ########################### |
055 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
056 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference)) |
057 | if (item 2 of listResponse) ≠ (missing value) then |
058 | log (item 2 of listReadStings)'s localizedDescription() as text |
059 | return "データの読み込みに失敗しました" |
060 | else |
061 | set ocidReadData to (item 1 of listResponse) |
062 | end if |
063 | ##NSImage |
064 | set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData)) |
065 | set ocidImageRepArray to ocidReadImage's representations() |
066 | set ocidImageRep to ocidImageRepArray's firstObject() |
067 | set ocidPxSizeH to ocidImageRep's pixelsHigh() |
068 | #とっとと解放 |
069 | set ocidImageRepArray to "" |
070 | set ocidImageRep to "" |
071 | |
072 | ########################### |
073 | #OCR |
074 | ########################### |
075 | #OCR結果ファイル保存先 |
076 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
077 | set ocidSaveTextFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("txt")) |
078 | #最小文字サイズ |
079 | set numMinPt to 8 as integer |
080 | #####ImageRep画像の高さを取得 |
081 | ##画像に対しての8Ptテキストの比率--> MinimumTextHeightにセットする |
082 | set intTextHeight to (numMinPt / ocidPxSizeH) as number |
083 | #####OCR |
084 | #VNImageRequestHandler's |
085 | set ocidHandler to (refMe's VNImageRequestHandler's alloc()'s initWithData:(ocidReadData) options:(missing value)) |
086 | #VNRecognizeTextRequest's |
087 | set ocidRequest to refMe's VNRecognizeTextRequest's alloc()'s init() |
088 | set ocidOption to (refMe's VNRequestTextRecognitionLevelAccurate) |
089 | (ocidRequest's setRecognitionLevel:(ocidOption)) |
090 | (ocidRequest's setMinimumTextHeight:(intTextHeight)) |
091 | (ocidRequest's setAutomaticallyDetectsLanguage:(true)) |
092 | (ocidRequest's setRecognitionLanguages:{"en", "ja"}) |
093 | (ocidRequest's setUsesLanguageCorrection:(false)) |
094 | #results |
095 | (ocidHandler's performRequests:(refMe's NSArray's arrayWithObject:(ocidRequest)) |error| :(reference)) |
096 | set ocidResponseArray to ocidRequest's results() |
097 | #戻り値を格納するテキスト |
098 | set ocidFirstOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
099 | set ocidSecondOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
100 | set ocidSaveString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
101 | #戻り値の数だけ々 |
102 | repeat with itemArray in ocidResponseArray |
103 | #候補数指定 1−10 ここでは2種の候補を戻す指定 |
104 | set ocidRecognizedTextArray to (itemArray's topCandidates:(2)) |
105 | set ocidFirstOpinion to ocidRecognizedTextArray's firstObject() |
106 | set ocidSecondOpinion to ocidRecognizedTextArray's lastObject() |
107 | (ocidFirstOpinionString's appendString:(ocidFirstOpinion's |string|())) |
108 | (ocidFirstOpinionString's appendString:("\n")) |
109 | (ocidSecondOpinionString's appendString:(ocidSecondOpinion's |string|())) |
110 | (ocidSecondOpinionString's appendString:("\n")) |
111 | end repeat |
112 | ##比較して相違点を探すならここで |
113 | (ocidSaveString's appendString:("-----第1候補\n\n")) |
114 | (ocidSaveString's appendString:(ocidFirstOpinionString)) |
115 | (ocidSaveString's appendString:("\n\n-----第2候補\n\n")) |
116 | (ocidSaveString's appendString:(ocidSecondOpinionString)) |
117 | ###保存 |
118 | set listDone to (ocidSaveString's writeToURL:(ocidSaveTextFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
119 | if (item 1 of listDone) is true then |
120 | log "正常終了" |
121 | else if (item 1 of listDone) is false then |
122 | log (item 2 of listDone)'s localizedDescription() as text |
123 | return display alert " 保存に失敗しました" |
124 | end if |
125 | #解放 |
126 | set ocidSaveString to "" |
127 | set ocidReadImage to "" |
128 | set ocidNSInlineData to "" |
129 | set ocidReadData to "" |
130 | end repeat |
131 | |
132 | end open |
133 | |
134 | ################################ |
135 | # 日付 doGetDateNo() |
136 | ################################ |
137 | to doGetDateNo(strDateFormat) |
138 | ####日付情報の取得 |
139 | set ocidDate to current application's NSDate's |date|() |
140 | ###日付のフォーマットを定義 |
141 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
142 | ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
143 | ocidNSDateFormatter's setDateFormat:strDateFormat |
144 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate |
145 | set strDateAndTime to ocidDateAndTime as text |
146 | return strDateAndTime |
147 | end doGetDateNo |
148 | |
AppleScriptで生成しました |
| 固定リンク