最近我正在使用GCP Vision API来检测图像是否有字幕(中文字符)。
奇怪的是,当我在代码中使用GCP sdk进行检测时,它没有响应任何文本,即使我可以明显看到那里的字幕。
然后我尝试手动将该图像直接上传到官网DEMO(链接在这里)。 它准确地响应了字幕文本。
下面是我的示例代码
export class GCPVisionAPI implements TextDetectorAPI {
visionClient: ImageAnnotatorClient;
async ApiInit(GCPCredential: string | undefined) {
if (!GCPCredential) {
throw new Error('Invalid GCP Credential!');
}
const credentialParams = {
credentials: JSON.parse(GCPCredential),
};
this.visionClient = new vision.ImageAnnotatorClient(credentialParams);
}
async detect(imageBuffer: Buffer) {
try {
const [result] = await this.visionClient.textDetection(imageBuffer);
const fullTextAnnotation = result.fullTextAnnotation;
if (fullTextAnnotation) return true;
return false;
} catch (error) {
console.log('Error:', error);
return false;
}
}
}
总结一下我的问题,sdk 和网站 demo 的文本检测结果不一致。 有人遇到过这个问题吗?
下面是我的最终方法,也是为了回答@prajnaRai。 到目前为止一切正常。 为了澄清一些误解,
textAnnotation
和fullTextAnnotation
之间没有太大区别。
唯一的区别是 textAnnotation
以数组形式返回,fullTextAnnotation
以字符串形式返回。
async textDetect(imageBuffer: Buffer) {
try {
// 2 types of text detection, use both to get more accurate detection
const [documentResult] = await this.visionClient.documentTextDetection(imageBuffer);
const [textResult] = await this.visionClient.textDetection(imageBuffer);
const documentFullTextAnnotation = documentResult.fullTextAnnotation;
const textAnnotation = textResult.fullTextAnnotation;
if (documentFullTextAnnotation || textAnnotation) return true;
return false;
} catch (error) {
console.log('Error:', error);
return false;
}
}