我試圖將圖片上傳到Google Drive以獲取光學字符識別(OCR)。這裏是我的代碼:將圖片上傳到Google Drive for OCR
require_once('vendor/autoload.php');
// Initialize Google Client
$client_email = '[email protected]';
$private_key = file_get_contents('key.p12');
$scopes = array(
'https://www.googleapis.com/auth/drive.file'
);
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
// Initialize Google Drive service
$service = new Google_Service_Drive($client);
// Upload File
$file = new Google_Service_Drive_DriveFile();
$file->setName('Test Image for OCR');
$file->setDescription('Test Image for OCR');
$file->setMimeType('image/jpeg');
try {
$data = file_get_contents($filename);
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
));
var_dump($createdFile);
// ===========
// So, what's next?
// ===========
} catch(Exception $e) {
echo 'Error occurred: ' . $e->getMessage();
}
上面的代碼運行沒有錯誤,而$createdFile
是Google_Service_Drive_DriveFile
對象形成有效的資源。
問題:
我想上傳成功,因爲
create()
函數不返回一個錯誤。但是,我看不到在我的Google雲端硬盤中上傳的文件。不應該將其上傳到Google雲端硬盤的根文件夾?如何執行OCR?我可以從here讀取有一個名爲
ocrLanguage
的參數。我應該在哪裏放置它,以及如何獲得結果?
在此先感謝。
UPDATE的var_dump()
結果如下:
object(Google_Service_Drive_DriveFile)#18 (55) {
["collection_key":protected]=>
string(6) "spaces"
["internal_gapi_mappings":protected]=>
array(0) {
}
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
NULL
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
NULL
["fullFileExtension"]=>
NULL
["headRevisionId"]=>
NULL
["iconLink"]=>
NULL
["id"]=>
string(28) "0B_XXXXX1yjq7dENaQWp4ckZoRk0"
["imageMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=>
string(0) ""
["kind"]=>
string(10) "drive#file"
["lastModifyingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
string(0) ""
["md5Checksum"]=>
NULL
["mimeType"]=>
string(10) "image/jpeg"
["modifiedByMeTime"]=>
NULL
["modifiedTime"]=>
NULL
["name"]=>
string(18) "Test Image for OCR"
["originalFilename"]=>
NULL
["ownedByMe"]=>
NULL
["ownersType":protected]=>
string(25) "Google_Service_Drive_User"
["ownersDataType":protected]=>
string(5) "array"
["parents"]=>
NULL
["permissionsType":protected]=>
string(31) "Google_Service_Drive_Permission"
["permissionsDataType":protected]=>
string(5) "array"
["properties"]=>
NULL
["quotaBytesUsed"]=>
NULL
["shared"]=>
NULL
["sharedWithMeTime"]=>
NULL
["sharingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["sharingUserDataType":protected]=>
string(0) ""
["size"]=>
NULL
["spaces"]=>
NULL
["starred"]=>
NULL
["thumbnailLink"]=>
NULL
["trashed"]=>
NULL
["version"]=>
NULL
["videoMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileVideoMediaMetadata"
["videoMediaMetadataDataType":protected]=>
string(0) ""
["viewedByMe"]=>
NULL
["viewedByMeTime"]=>
NULL
["viewersCanCopyContent"]=>
NULL
["webContentLink"]=>
NULL
["webViewLink"]=>
NULL
["writersCanShare"]=>
NULL
["modelData":protected]=>
array(0) {
}
["processed":protected]=>
array(0) {
}
}
的文件可以通過$service->files->get($file_id);
獲得,但它在我的谷歌驅動器不可見。返回的文件資源對象不包含任何有用的內容。
您使用的服務帳戶,您上傳文件到服務帳戶谷歌驅動器帳戶,你不能看到從谷歌驅動器的網頁版本。假設我沒有錯過將代碼上傳到個人雲端硬盤帳戶目錄中的代碼。 – DaImTo
兩者都正確。我正在使用服務帳戶並嘗試上傳到個人帳戶。那麼,我應該如何更改我的代碼呢?我應該使用哪種憑據?我打算將此代碼用作cron作業/作爲接收圖像文件上傳的API。 – Raptor