来自 url 列表的 ocr 图像并将结果存储在电子表格中
ocr images from list of urls and store the results in spreadsheet
你好,我有一个包含数字的图像 URL 列表,我想对它们进行 OCR 并将结果存储在 google 电子表格中
我找到了这些 google 用于 ocr 图像的脚本
1- https://gist.github.com/tagplus5/07dde5ca61fe8f42045d
2- https://ctrlq.org/code/20128-extract-text-from-image-ocr
但我不知道如何创建请求变量,所以我用 URL 变量替换了请求变量,如下所示:
function doGet(url) {
if (url != undefined && url != "") {
var imageBlob = UrlFetchApp.fetch(url).getBlob();
var resource = {
title: imageBlob.getName(),
mimeType: imageBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, imageBlob, options);
var doc = DocumentApp.openById(docFile.id);
var text = doc.getBody().getText().replace("\n", "");
Drive.Files.remove(docFile.id);
return ContentService.createTextOutput(text);
}
else {
return ContentService.createTextOutput("request error");
}
}
问题是当我调用这样的函数时 doGet(B1) 其中 B1 包含 url 到 google 电子表格中的图像以执行 OCR 并在单元格中获取结果文本C1 它说驱动变量未定义
希望尽快得到答复
代码没问题。 V2 API 还活着。参见 this documentation。您只需要启用高级驱动服务。在脚本编辑器中,select Resources > Advanced Google services 并打开 Drive API(仅v2 是 select 可用的)。那么您的代码就可以正常工作了。
好的,我修改了你的脚本并做了一个sheet来展示一个例子。 sheet 是 here(任何人都可以编辑),其脚本如下。应该为 运行 此脚本启用 Advanced Drive Service 的驱动器 API(v2)。
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'RUN', functionName: 'doGet2'}
];
ss.addMenu('OCR', menuItems);
}
function doGet2() {
var ROW_START = 3;
var URL_COL = 1;
var TEXT_COL = 2;
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var urls = sheet.getRange(ROW_START,URL_COL, sheet.getLastRow()-ROW_START+1,1).getValues();
var texts = [];
for(var i=0; i<urls.length; i++) {
var url = urls[i];
if(url != undefined && url != "") {
var imageBlob = UrlFetchApp.fetch(url).getBlob();
var resource = {
title: imageBlob.getName(),
mimeType: imageBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, imageBlob, options);
var doc = DocumentApp.openById(docFile.id);
var text = doc.getBody().getText().replace("\n", "");
texts.push([text]);
Drive.Files.remove(docFile.id);
}
else {
texts.push("request error");
}
}
sheet.getRange(ROW_START,TEXT_COL, urls.length,1).setValues(texts);
}
你好,我有一个包含数字的图像 URL 列表,我想对它们进行 OCR 并将结果存储在 google 电子表格中 我找到了这些 google 用于 ocr 图像的脚本
1- https://gist.github.com/tagplus5/07dde5ca61fe8f42045d
2- https://ctrlq.org/code/20128-extract-text-from-image-ocr
但我不知道如何创建请求变量,所以我用 URL 变量替换了请求变量,如下所示:
function doGet(url) {
if (url != undefined && url != "") {
var imageBlob = UrlFetchApp.fetch(url).getBlob();
var resource = {
title: imageBlob.getName(),
mimeType: imageBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, imageBlob, options);
var doc = DocumentApp.openById(docFile.id);
var text = doc.getBody().getText().replace("\n", "");
Drive.Files.remove(docFile.id);
return ContentService.createTextOutput(text);
}
else {
return ContentService.createTextOutput("request error");
}
}
问题是当我调用这样的函数时 doGet(B1) 其中 B1 包含 url 到 google 电子表格中的图像以执行 OCR 并在单元格中获取结果文本C1 它说驱动变量未定义
希望尽快得到答复
代码没问题。 V2 API 还活着。参见 this documentation。您只需要启用高级驱动服务。在脚本编辑器中,select Resources > Advanced Google services 并打开 Drive API(仅v2 是 select 可用的)。那么您的代码就可以正常工作了。
好的,我修改了你的脚本并做了一个sheet来展示一个例子。 sheet 是 here(任何人都可以编辑),其脚本如下。应该为 运行 此脚本启用 Advanced Drive Service 的驱动器 API(v2)。
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'RUN', functionName: 'doGet2'}
];
ss.addMenu('OCR', menuItems);
}
function doGet2() {
var ROW_START = 3;
var URL_COL = 1;
var TEXT_COL = 2;
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var urls = sheet.getRange(ROW_START,URL_COL, sheet.getLastRow()-ROW_START+1,1).getValues();
var texts = [];
for(var i=0; i<urls.length; i++) {
var url = urls[i];
if(url != undefined && url != "") {
var imageBlob = UrlFetchApp.fetch(url).getBlob();
var resource = {
title: imageBlob.getName(),
mimeType: imageBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, imageBlob, options);
var doc = DocumentApp.openById(docFile.id);
var text = doc.getBody().getText().replace("\n", "");
texts.push([text]);
Drive.Files.remove(docFile.id);
}
else {
texts.push("request error");
}
}
sheet.getRange(ROW_START,TEXT_COL, urls.length,1).setValues(texts);
}