我正在使用PHP,并希望使用Google Drive REST API(不是版本2)对几张图片执行ORC(文本检测)。如您所知,在版本 3 中不再有insert
方法,我必须与create
或copy
合作才能执行 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(); } }
首先,澄清一下,您实际上指的是光学字符识别 (OCR) 而不是 ORC,就像您发布的那样?
如果是 OCR,则发送请求和接收响应是通过 HTTP 协议执行的,如免费 OCR API 中所述。上传图片后使用 OCR 时,向方法的/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 开发人员指南以获取更多信息和示例代码。