如何在Google Drive REST API(v3)中执行ORC

时间:2016-04-25 21:53:00

标签: php api google-drive-api

我使用PHP并希望使用Google Drive REST API(不是版本2)在多张图片上执行ORC(文本检测)。如您所知,在版本3中,不再有insert方法,我必须使用createcopy来执行ORC。

在这个新版本中,ORC默认启用,所以我只设置orcLanguage,我认为google在图片上做ORC没有任何问题,但我的问题是这样,如何获得ORC操作的输出?

以下是我使用的代码:

function GetORC($filename){ require_once 'google-api-php-client-2.0.0-RC7/vendor/autoload.php'; $client = new Google_Client(); $client->setClientId('>my.client.id<'); $client->setClientSecret('>my.client.secret<'); $client->setRedirectUri('>http://the.uri.i.use<'); $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

if (isset($_GET['code']) || (isset($_SESSION['accesslic_html/tttest/index.php_token']) && $_SESSION['access_token'])) {
    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['access_token'] = $client->getAccessToken();
    } else
        $client->setAccessToken($_SESSION['access_token']);

    $service = new Google_Service_Drive($client);
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle(uniqid().'.jpg');
    $file->setDescription('A test document');
    $file->setMimeType('image/jpeg');

    $data = file_get_contents($filename);

    $createdFile = $service->files->create($file, array(
          'data' => $data,
          'mimeType' => 'image/jpeg',
          'ocrLanguage' => 'fa',
          'uploadType' => 'multipart'
        ));

    var_dump($createdFile);

} else {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . $authUrl);
    exit(); } }

1 个答案:

答案 0 :(得分:0)

首先,只是为了澄清,您实际上是指光学字符识别( OCR )而不是 ORC ,就像您发布的那样?

如果是OCR,则发送请求和接收响应是通过HTTP协议执行的,如Free OCR API中所述。 在上传图片后使用OCR,对方法&#39; s / v1 / ocr URI发出GET请求并添加查询参数,例如:

GET /v1/ocr?key=api_key&file_id=12345&page=1&lang=eng&psm=3 HTTP/1.1
Host: api.newocr.com

如果请求成功,服务器将返回HTTP 200 OK状态代码以及任何元数据:

HTTP/1.1 200 OK
Content-Type: application/json
{
  "status": "success",
  "data":
  {
    "text": "Recognized text",
    "progress": "100"
  }
}

您还可以查看OCR REST API Developer's Guide以获取更多信息和示例代码。