使用Google App Script从Google Drive中OCR图像

3
我已经实现了以下脚本,以便通过图像URL对单个和多个图像进行OCR。
function doOCRALL() {
  var selected = SpreadsheetApp.getActiveSheet().getActiveRange().getValues().length;
  for (var i = 0; i < selected; i++) {
    var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
    var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
    var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol).getValue();

    var image = UrlFetchApp.fetch(valueURL).getBlob();

    var file = {
      title: 'OCR File',
      mimeType: 'image/png'
    };

    // OCR is supported for PDF and image formats
    file = Drive.Files.insert(file, image, {ocr: true});
    var doc = DocumentApp.openByUrl(file.embedLink);
    var body = doc.getBody().getText();
    //Get link Doc that Generated
    SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 2).setValue(file.embedLink);
    //Get Content of Doc that Generated
    SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 1).setValue(body);

  }
}


function doOCR() {
  //
  var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
  var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();

  var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol).getValue();

  var image = UrlFetchApp.fetch(valueURL).getBlob();

  var file = {
    title: 'OCR File',
    mimeType: 'image/png'
  };

  // OCR is supported for PDF and image formats
  file = Drive.Files.insert(file, image, {ocr: true});
  var doc = DocumentApp.openByUrl(file.embedLink);
  var body = doc.getBody().getText();


  // Print the Google Document URL in the console
  Logger.log("body: %s", body);
  Logger.log("File URL: %s", file.embedLink);
  //Get link Doc that Generated
  SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 2).setValue(file.embedLink);
  //Get Content of Doc that Generated
  SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 1).setValue(body);
}



function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('OCR Tools')
      .addItem('Extract Cell', 'doOCR')
      .addItem('Extract All Cell', 'doOCRALL')
      .addSeparator()
      .addSubMenu(ui.createMenu('About US')
          .addItem('Infomation', 'menuItem2'))
      .addToUi();
}

function menuItem2() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     .alert('AIO Team');
}

当我提供任何图像的 URL 时,它都可以正常显示。但是,如果我上传相同的图像到我的 Google Drive 并从 Drive 提供图像 URL,它只会给我“登录主菜单”。对于其他 Drive 图像也是如此。谢谢!

我可以问一下关于URL的事吗?你是怎么得到它的?你能提供一个样本URL吗?当然,记得删除你的私人信息。 - Tanaike
谢谢,但我的问题已经解决了。我使用了文件ID而不是URL,并更改了访问权限。 - Sanket Tarodekar
谢谢您的回复。我很高兴您的问题得到了解决。 - Tanaike
1个回答

3
如果内容已经在Drive中,您不需要获取其链接 - 只需提供文件ID(可以从链接中获取)即可。
一旦您获得了文件ID,您可以简单地复制它,并使用最佳参数来激活OCR。当然,完整的选项列表可以在Drive REST API页面上找到:https://developers.google.com/drive/api/v2/reference/files/copy#parameters。我鼓励您阅读有关最佳实践的文章,例如fields规范(这是较新的Drive API版本的要求)。
此函数接受您从某个地方获取的输入Drive文件ID以及一个真值以设置“使用OCR”选项。显而易见的假设是您具有权限,ID有效,已启用高级服务和云控制台中的Drive API等。
function getIdOfCopyOfDriveFile(fileId, useOcr) {
  const options = {
    fields: "choose the metadata fields to return in the response e.g. 'id,title,parents'"
  };
  const existingMetaData = Drive.Files.get(fileId, options);

  options.ocr = !!useOcr;
  existingMetaData.title += " (copied with" + (options.ocr ? " " : "out ") + "ocr)";
  // We could do other modifications of fields we requested before
  // copying, like changing the parents array to move the new file.
  const newFileMetaData = Drive.Files.copy(existingMetaData, fileId, options);
  return newFileMetaData.id;
}

答案很有帮助。我使用了fileID而不是url,并且需要更改文件的访问权限和权限。 file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT); - Sanket Tarodekar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接