使用Google App脚本的Google Drive中的OCR图像 [英] OCR images from google drive using Google App Script

查看:16
本文介绍了使用Google App脚本的Google Drive中的OCR图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了以下脚本,以便使用图像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时,它都可以正常工作。但是,如果我将相同的图像上传到我的驱动器上,然后提供驱动器中的图像URL,它只会给我"登录主菜单"。对于其他驱动器映像,它会给出相同的文本。 提前谢谢。

推荐答案

如果内容已在驱动器中,则不需要获取指向它的链接-只需提供文件ID(您可以从指向它的链接获取)。

获得文件ID后,只需复制它,并使用最佳参数激活OCR即可。当然,完整的选项列表可以在睡觉驱动接口页面获得:https://developers.google.com/drive/api/v2/reference/files/copy#parameters 我还鼓励您阅读有关最佳实践的信息,如fields规范(这是较新的驱动器API版本的要求)。

此函数接受您从某个地方获得的输入驱动器文件ID和一个true-y值来设置"使用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;
}

这篇关于使用Google App脚本的Google Drive中的OCR图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆