会社行きたくなかったんです。

自由になりたくて会社辞めました

Azure プログラミング

VB.netでMicrosoft Cognitive ServicesのOCR API を使ってみました。

投稿日:

さわってわかるクラウドAI Microsoft Cognitive Services実践ガイド」という本を購入しました。

Google ChromeにPostmanという拡張機能をインストールすることで、好きなデータをPOSTできるというような事が書いてあり、その例として、Microsoft Cognitive ServicesのOCR API を使っていました。

スポンサーリンク

結局、HTTPリクエストを送ることができれば良いので、VB.netでコードを書いてみて、試しました。

        Dim webReq As Net.HttpWebRequest
        Dim webRes As Net.HttpWebResponse
        Dim reqStream As System.IO.Stream
        Dim resStream As System.IO.Stream
        Dim resStreamReader As System.IO.StreamReader
        Dim fsFile As System.IO.FileStream
        Dim byData As Byte()
        Dim sエンドポイント As String
        Dim sキー As String
        Dim sFilePath As String
        Dim sJSON As String

        Try
            'エンドポイントはリソースの場所によって変わります。
            sエンドポイント = "https://westus.api.cognitive.microsoft.com/vision/v1.0"
            'visualFeaturesは必要な項目だけで良いです。
            'このコードでは全部取得しています。
            sエンドポイント &= "/ocr?language=ja&detectOrientation=true"
            sエンドポイント &= "&subscription-key="
            sキー = "<APIキー>"
            sエンドポイント &= sキー
            '画像ファイルを読み込みます。
            sFilePath = "<画像ファイルのパス>"
            fsFile = New System.IO.FileStream(sFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
            ReDim byData(fsFile.Length - 1)
            fsFile.Read(byData, 0, byData.Length)
            fsFile.Close()

            webReq = DirectCast(System.Net.WebRequest.Create(sエンドポイント), System.Net.HttpWebRequest)
            webReq.Method = "POST"
            webReq.ContentType = "application/octet-stream"

            webReq.ContentLength = byData.Length
            'RequestStreamを取得
            reqStream = webReq.GetRequestStream()
            reqStream.Write(byData, 0, byData.Length)
            reqStream.Close()
            'RequestStreamを取得
            webRes = webReq.GetResponse()
            resStream = webRes.GetResponseStream()
            resStreamReader = New System.IO.StreamReader(resStream, System.Text.Encoding.UTF8)
            'レスポンスはJSON形式です。
            sJSON = resStreamReader.ReadToEnd
            'RichTextBox1に結果を表示するようにしました。
            RichTextBox1.Text = sJSON

        Catch ex As Net.WebException

            'エラーが発生した場合、こうすると内容がわかります。
            If Not ex.Response Is Nothing Then
                Dim errorResponse As Net.HttpWebResponse
                errorResponse = ex.Response
                Dim reader As New IO.StreamReader(errorResponse.GetResponseStream())
                Dim sError As String
                sError = reader.ReadToEnd()
                MessageBox.Show(sError)
            End If

        End Try

今回は、ローカルの画像ファイルを参照しているので、
ContentType を「application/octet-stream」として、画像をバイナリデータとして読み込み渡しています。

URLを指定する場合は、ContentType を「application/json」とします。
データは、{"url":"<画像のURL>"} というようにJSON形式にします。

Computer Vision APIに関しては、こちらを参考にしてください。
VB.netでComputer Vision APIを使ってみました。

次の画像で試してみました。

結果は以下です。
どの程度正しいのかよくわかりませんが、なんとなく、本の表紙と同じ文字が表示されているのがわかります。
レスポンスを解析して、再配置するプログラムを書けば、どの程度正しいのかわかるのですが、今回はやめておきます。

{"language":"ja","textAngle":0.0,"orientation":"Up","regions":[{"boundingBox":"344,1128,2392,3555","lines":[{"boundingBox":"504,1128,808,528","words":[{"boundingBox":"504,1128,808,528","text":"THE"}]},{"boundingBox":"1576,1256,1136,280","words":[{"boundingBox":"1576,1256,296,280","text":"僕"},{"boundingBox":"1912,1272,240,240","text":"た"},{"boundingBox":"2184,1272,224,248","text":"ち"},{"boundingBox":"2464,1312,248,200","text":"の"}]},{"boundingBox":"1384,1560,1352,280","words":[{"boundingBox":"1384,1560,392,272","text":"20"},{"boundingBox":"1800,1560,288,280","text":"年"},{"boundingBox":"2104,1560,296,280","text":"戦"},{"boundingBox":"2432,1560,304,280","text":"略"}]},{"boundingBox":"344,2232,1168,544","words":[{"boundingBox":"344,2232,264,528","text":"」"},{"boundingBox":"648,2240,864,536","text":"OBS"}]},{"boundingBox":"456,1664,1104,552","words":[{"boundingBox":"456,1664,856,544","text":"END"},{"boundingBox":"1392,1864,168,352","text":"0"}]},{"boundingBox":"400,2960,672,304","words":[{"boundingBox":"400,2960,312,296","text":"新"},{"boundingBox":"768,2968,304,296","text":"働"}]},{"boundingBox":"384,3280,648,312","words":[{"boundingBox":"384,3280,304,312","text":"常"},{"boundingBox":"784,3296,248,280","text":"き"}]},{"boundingBox":"2155,4407,35,89","words":[{"boundingBox":"2155,4407,35,43","text":"日"},{"boundingBox":"2155,4451,35,45","text":"■"}]},{"boundingBox":"1418,1149,1281,83","words":[{"boundingBox":"1418,1149,177,80","text":"Money."},{"boundingBox":"1609,1152,219,80","text":"Meaning"},{"boundingBox":"1848,1154,94,62","text":"and"},{"boundingBox":"1958,1156,226,61","text":"Freedom"},{"boundingBox":"2203,1157,294,62","text":"Withoutthe"},{"boundingBox":"2518,1161,58,60","text":"9-"},{"boundingBox":"2584,1168,47,55","text":"to"},{"boundingBox":"2637,1166,62,58","text":"-5"}]},{"boundingBox":"2233,1904,490,40","words":[{"boundingBox":"2233,1905,36,37","text":"ジ"},{"boundingBox":"2280,1919,10,9","text":"・"},{"boundingBox":"2301,1909,36,28","text":"エ"},{"boundingBox":"2345,1909,34,30","text":"ン"},{"boundingBox":"2391,1905,27,35","text":"ド"},{"boundingBox":"2429,1919,11,9","text":"・"},{"boundingBox":"2452,1907,34,34","text":"オ"},{"boundingBox":"2495,1904,36,37","text":"ブ"},{"boundingBox":"2540,1920,11,9","text":"・"},{"boundingBox":"2565,1906,36,37","text":"ジ"},{"boundingBox":"2608,1920,26,23","text":"ョ"},{"boundingBox":"2643,1906,37,38","text":"ブ"},{"boundingBox":"2682,1909,41,34","text":"ズ"}]},{"boundingBox":"2067,2008,656,64","words":[{"boundingBox":"2067,2013,58,54","text":"テ"},{"boundingBox":"2134,2010,50,57","text":"イ"},{"boundingBox":"2193,2011,54,55","text":"ラ"},{"boundingBox":"2257,2033,55,9","text":"ー"},{"boundingBox":"2333,2031,16,14","text":"・"},{"boundingBox":"2372,2008,57,56","text":"ピ"},{"boundingBox":"2435,2014,60,49","text":"ア"},{"boundingBox":"2505,2015,53,49","text":"ソ"},{"boundingBox":"2570,2014,54,50","text":"ン"},{"boundingBox":"2656,2011,67,61","text":"著"}]},{"boundingBox":"2462,2089,263,53","words":[{"boundingBox":"2462,2089,53,48","text":"児"},{"boundingBox":"2519,2090,52,48","text":"島"},{"boundingBox":"2591,2091,55,48","text":"修"},{"boundingBox":"2672,2093,53,49","text":"訳"}]},{"boundingBox":"1718,3002,79,473","words":[{"boundingBox":"1723,3002,70,70","text":"人"},{"boundingBox":"1721,3081,76,73","text":"類"},{"boundingBox":"1720,3163,73,72","text":"史"},{"boundingBox":"1718,3244,74,72","text":"上"},{"boundingBox":"1718,3326,73,75","text":"初"},{"boundingBox":"1721,3414,64,61","text":"の"}]},{"boundingBox":"1617,2998,78,557","words":[{"boundingBox":"1622,2998,72,72","text":"革"},{"boundingBox":"1622,3078,73,73","text":"命"},{"boundingBox":"1624,3160,64,67","text":"を"},{"boundingBox":"1628,3241,54,71","text":"目"},{"boundingBox":"1617,3321,72,75","text":"撃"},{"boundingBox":"1628,3406,55,67","text":"し"},{"boundingBox":"1618,3485,63,70","text":"た"}]},{"boundingBox":"1516,2995,79,472","words":[{"boundingBox":"1521,2995,74,73","text":"著"},{"boundingBox":"1520,3076,73,72","text":"者"},{"boundingBox":"1522,3156,67,67","text":"が"},{"boundingBox":"1520,3236,69,71","text":"明"},{"boundingBox":"1518,3317,67,66","text":"か"},{"boundingBox":"1516,3397,68,70","text":"す"}]},{"boundingBox":"507,4303,556,84","words":[{"boundingBox":"507,4303,66,70","text":"世"},{"boundingBox":"578,4305,65,71","text":"界"},{"boundingBox":"649,4313,38,61","text":"8"},{"boundingBox":"692,4312,60,62","text":"か"},{"boundingBox":"760,4311,67,70","text":"国"},{"boundingBox":"836,4316,55,64","text":"で"},{"boundingBox":"897,4315,66,69","text":"翻"},{"boundingBox":"971,4317,64,70","text":"訳"},{"boundingBox":"1049,4322,14,64","text":"!"}]},{"boundingBox":"432,4396,703,92","words":[{"boundingBox":"432,4396,74,71","text":"午"},{"boundingBox":"506,4423,42,44","text":"0"},{"boundingBox":"551,4409,62,62","text":"市"},{"boundingBox":"617,4426,72,46","text":"es"},{"boundingBox":"692,4406,27,76","text":"』"},{"boundingBox":"735,4409,67,70","text":"誌"},{"boundingBox":"807,4415,68,62","text":"が"},{"boundingBox":"877,4413,65,72","text":"絶"},{"boundingBox":"951,4415,64,73","text":"賛"},{"boundingBox":"1026,4419,45,67","text":"し"},{"boundingBox":"1078,4421,57,67","text":"た"}]},{"boundingBox":"498,4499,547,85","words":[{"boundingBox":"498,4499,72,72","text":"若"},{"boundingBox":"572,4502,68,71","text":"手"},{"boundingBox":"643,4505,70,69","text":"起"},{"boundingBox":"721,4508,65,70","text":"業"},{"boundingBox":"793,4511,65,71","text":"家"},{"boundingBox":"869,4516,57,62","text":"に"},{"boundingBox":"934,4517,53,65","text":"よ"},{"boundingBox":"995,4522,50,62","text":"る"}]},{"boundingBox":"627,4601,282,82","words":[{"boundingBox":"627,4601,67,74","text":"衝"},{"boundingBox":"700,4606,67,72","text":"撃"},{"boundingBox":"774,4616,62,62","text":"の"},{"boundingBox":"844,4613,65,70","text":"書"}]}]}]}

-Azure, プログラミング

Copyright© 自由になりたくて会社辞めました , 2024 AllRights Reserved Powered by AFFINGER4.