Azure OCR API

Posted . Visible to the public.

Get the Microsoft Azure Computer Vision API Key here Show archive.org snapshot .

Image

Image

There is a sample PHP code in github Show archive.org snapshot that uses Pear HTTP_Request2 package.

In this stackoverflow Show archive.org snapshot , we can use curl instead.

public function msvisionAction()
{
    $t2 = microtime(true);
    $ocpApimSubscriptionKey = '232xxxxxxxxxxx4ada';
    $uriBase = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.1/ocr';
    $url = $uriBase.'?'.'language=unk&detectOrientation=true';
    $headers = array(
        'Content-Type: application/json',
        //'Content-Type: application/octet-stream',
        'Ocp-Apim-Subscription-Key: '.$ocpApimSubscriptionKey
    );
    $body = json_encode(array('url' => 'http://domain.com.my/media/m1.jpg'));

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); // don't cache curl request
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the transfer as a string of the return value
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // disable SSL checks may not be needed
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
    $response = curl_exec($ch);
    $chError = curl_error($ch);
    curl_close($ch);

    $dt = microtime(true) - $t2;
    Mage::helper('clog')->_echo([json_decode($response, true), $chError], "Done in $dt secs");
}

See API Doc Show archive.org snapshot .

Output:

Done in 5.5461490154266 secs
 array(2) {
  [0] => array(4) {
    ["language"] => string(2) "en"
    ["textAngle"] => float(-0.090757121103704)
    ["orientation"] => string(2) "Up"
    ["regions"] => array(4) {
      [0] => array(2) {
        ["boundingBox"] => string(13) "119,293,15,10"
        ["lines"] => array(1) {
          [0] => array(2) {
            ["boundingBox"] => string(13) "119,293,15,10"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "119,293,15,10"
                ["text"] => string(4) "•z"
              }
            }
          }
        }
      }
      [1] => array(2) {
        ["boundingBox"] => string(14) "63,729,439,334"
        ["lines"] => array(15) {
          [0] => array(2) {
            ["boundingBox"] => string(13) "66,729,258,17"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "66,730,143,16"
                ["text"] => string(13) "JAMHUURIYADDA"
              }
              [1] => array(2) {
                ["boundingBox"] => string(14) "214,729,110,13"
                ["text"] => string(10) "SOOMAALIYA"
              }
            }
          }
          [1] => array(2) {
            ["boundingBox"] => string(13) "63,758,201,11"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(12) "63,758,73,11"
                ["text"] => string(9) "BAASABOOR"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "206,758,58,11"
                ["text"] => string(8) "PASSPORT"
              }
            }
          }
          [2] => array(2) {
            ["boundingBox"] => string(14) "270,768,153,11"
            ["words"] => array(5) {
              [0] => array(2) {
                ["boundingBox"] => string(11) "270,771,3,8"
                ["text"] => string(1) "j"
              }
              [1] => array(2) {
                ["boundingBox"] => string(11) "276,769,6,9"
                ["text"] => string(1) "N"
              }
              [2] => array(2) {
                ["boundingBox"] => string(13) "312,768,18,11"
                ["text"] => string(3) "OJI"
              }
              [3] => array(2) {
                ["boundingBox"] => string(13) "332,769,28,10"
                ["text"] => string(6) ",rrype"
              }
              [4] => array(2) {
                ["boundingBox"] => string(12) "383,769,40,8"
                ["text"] => string(7) "AsbaoZa"
              }
            }
          }
          [3] => array(2) {
            ["boundingBox"] => string(13) "400,784,37,13"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "400,784,37,13"
                ["text"] => string(3) "SOM"
              }
            }
          }
          [4] => array(2) {
            ["boundingBox"] => string(14) "287,821,215,14"
            ["words"] => array(3) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "287,822,80,13"
                ["text"] => string(7) "NASTEHO"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "373,821,67,14"
                ["text"] => string(6) "HASSAN"
              }
              [2] => array(2) {
                ["boundingBox"] => string(13) "446,821,56,14"
                ["text"] => string(5) "ROBLE"
              }
            }
          }
          [5] => array(2) {
            ["boundingBox"] => string(14) "288,859,205,13"
            ["words"] => array(3) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "288,859,60,13"
                ["text"] => string(5) "HALWO"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "353,859,75,13"
                ["text"] => string(7) "ABDUL"
              }
              [2] => array(2) {
                ["boundingBox"] => string(13) "434,859,59,13"
                ["text"] => string(5) "GOBE"
              }
            }
          }
          [6] => array(2) {
            ["boundingBox"] => string(13) "356,881,56,11"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "356,881,56,11"
                ["text"] => string(12) "'Nationality"
              }
            }
          }
          [7] => array(2) {
            ["boundingBox"] => string(13) "288,897,62,13"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "288,897,62,13"
                ["text"] => string(6) "SOMALI"
              }
            }
          }
          [8] => array(2) {
            ["boundingBox"] => string(12) "466,918,34,8"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(11) "466,918,9,8"
                ["text"] => string(2) "oi"
              }
              [1] => array(2) {
                ["boundingBox"] => string(12) "477,918,23,8"
                ["text"] => string(5) "Birth"
              }
            }
          }
          [9] => array(2) {
            ["boundingBox"] => string(13) "288,934,96,17"
            ["words"] => array(3) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "288,934,18,13"
                ["text"] => string(2) "08"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "312,934,30,17"
                ["text"] => string(3) "May"
              }
              [2] => array(2) {
                ["boundingBox"] => string(13) "349,934,35,13"
                ["text"] => string(4) "1996"
              }
            }
          }
          [10] => array(2) {
            ["boundingBox"] => string(13) "278,956,63,10"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(12) "278,956,17,9"
                ["text"] => string(3) "Lab"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "299,956,42,10"
                ["text"] => string(8) "Dheddig/"
              }
            }
          }
          [11] => array(2) {
            ["boundingBox"] => string(13) "289,972,66,13"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "289,972,66,13"
                ["text"] => string(6) "FEMALE"
              }
            }
          }
          [12] => array(2) {
            ["boundingBox"] => string(14) "343,993,130,10"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(12) "343,994,37,9"
                ["text"] => string(12) "Bixiy.•y,'"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "437,993,36,10"
                ["text"] => string(7) "/Dateot"
              }
            }
          }
          [13] => array(2) {
            ["boundingBox"] => string(14) "290,1009,93,16"
            ["words"] => array(3) {
              [0] => array(2) {
                ["boundingBox"] => string(14) "290,1009,17,13"
                ["text"] => string(2) "12"
              }
              [1] => array(2) {
                ["boundingBox"] => string(14) "312,1009,29,16"
                ["text"] => string(3) "Aug"
              }
              [2] => array(2) {
                ["boundingBox"] => string(14) "346,1009,37,13"
                ["text"] => string(4) "2018"
              }
            }
          }
          [14] => array(2) {
            ["boundingBox"] => string(14) "291,1046,92,17"
            ["words"] => array(3) {
              [0] => array(2) {
                ["boundingBox"] => string(14) "291,1046,15,13"
                ["text"] => string(2) "11"
              }
              [1] => array(2) {
                ["boundingBox"] => string(14) "312,1047,30,16"
                ["text"] => string(3) "Aug"
              }
              [2] => array(2) {
                ["boundingBox"] => string(14) "347,1046,36,14"
                ["text"] => string(4) "2023"
              }
            }
          }
        }
      }
      [2] => array(2) {
        ["boundingBox"] => string(15) "529,726,230,296"
        ["lines"] => array(9) {
          [0] => array(2) {
            ["boundingBox"] => string(14) "610,726,149,13"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "610,727,62,12"
                ["text"] => string(6) "SOMALI"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "679,726,80,13"
                ["text"] => string(8) "REPUBLIC"
              }
            }
          }
          [1] => array(2) {
            ["boundingBox"] => string(13) "595,783,85,14"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "595,783,85,14"
                ["text"] => string(9) "Pxxxx2"
              }
            }
          }
          [2] => array(2) {
            ["boundingBox"] => string(14) "529,843,126,11"
            ["words"] => array(2) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "529,843,24,10"
                ["text"] => string(5) "Nit)."
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "557,843,98,11"
                ["text"] => string(14) "2xxxx8"
              }
            }
          }
          [3] => array(2) {
            ["boundingBox"] => string(13) "531,896,78,15"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(13) "531,896,78,15"
                ["text"] => string(7) "STUDENT"
              }
            }
          }
          [4] => array(2) {
            ["boundingBox"] => string(14) "532,934,101,13"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(14) "532,934,101,13"
                ["text"] => string(9) "MOGADISHU"
              }
            }
          }
          [5] => array(2) {
            ["boundingBox"] => string(12) "555,956,57,9"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(12) "555,956,57,9"
                ["text"] => string(11) "Dholeshada/"
              }
            }
          }
          [6] => array(2) {
            ["boundingBox"] => string(14) "532,971,104,14"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(14) "532,971,104,14"
                ["text"] => string(9) "BELEDWEYN"
              }
            }
          }
          [7] => array(2) {
            ["boundingBox"] => string(13) "667,992,82,11"
            ["words"] => array(3) {
              [0] => array(2) {
                ["boundingBox"] => string(12) "667,993,5,10"
                ["text"] => string(1) "/"
              }
              [1] => array(2) {
                ["boundingBox"] => string(13) "673,992,31,11"
                ["text"] => string(7) "Issuing"
              }
              [2] => array(2) {
                ["boundingBox"] => string(13) "707,992,42,11"
                ["text"] => string(9) "Authority"
              }
            }
          }
          [8] => array(2) {
            ["boundingBox"] => string(15) "533,1008,186,14"
            ["words"] => array(1) {
              [0] => array(2) {
                ["boundingBox"] => string(15) "533,1008,186,14"
                ["text"] => string(16) "SOMALTGOVERNMENT"
              }
            }
          }
        }
      }
      [3] => array(2) {
        ["boundingBox"] => string(14) "85,1147,667,18"
        ["lines"] => array(1) {
          [0] => array(2) {
            ["boundingBox"] => string(14) "85,1147,667,18"
            ["words"] => array(4) {
              [0] => array(2) {
                ["boundingBox"] => string(14) "85,1147,301,18"
                ["text"] => string(20) "P00xxx629SOM960xx84"
              }
              [1] => array(2) {
                ["boundingBox"] => string(14) "392,1147,83,18"
                ["text"] => string(6) "F23081"
              }
              [2] => array(2) {
                ["boundingBox"] => string(15) "483,1147,159,18"
                ["text"] => string(11) "13296050871"
              }
              [3] => array(2) {
                ["boundingBox"] => string(15) "650,1147,102,18"
                ["text"] => string(7) "5653838"
              }
            }
          }
        }
      }
    }
  }
  [1] => string(0) ""
}
kiatng
Last edit
kiatng
Attachments
Posted by kiatng to RPA - ML (2019-11-28 07:30)