2016-04-25 34 views
1

我正在使用PHP並希望使用Google Drive REST API(不是版本2)在多張圖片上執行ORC(文本檢測)。如您所知,在版本3中不再有insert方法,我必須使用createcopy來執行ORC。如何在Google Drive REST API(v3)中執行ORC

在這個新版本ORC默認啓用,所以我只是設置orcLanguage,我認爲谷歌沒有任何問題做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(); } } 

回答

0

首先,我只想澄清,你實際上指的是光學字符識別(OCR),而不是ORC,就像你有什麼貼?

如果是OCR,則通過HTTP協議發送請求和接收響應,如Free OCR API中所述。 在上傳照片後,使用OCR,做一個GET請求方法的/ V1/OCR URI並添加查詢參數,例如:

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瞭解更多信息和示例代碼。

相關問題