node.js - 我在 ionic 中使用 tesseract.js 从图像中提取文本。现在有没有办法在键值对中表示提取的文本?
问题描述
import { Component } from '@angular/core';
import { NavController, ActionSheetController, LoadingController } from '@ionic/angular';
import { Camera, PictureSourceType } from '@ionic-native/camera/ngx';
import * as Tesseract from 'tesseract.js'
// import { NgProgress } from 'ngx-progressbar';
// import { NgProgress } from '@ngx-progressbar/core';
import { createWorker } from 'tesseract.js';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
progress = false;
worker = createWorker({
logger: m => console.log(m)
})
selectedImage: string;
imageText: string;
constructor(
public navCtrl: NavController,
private camera: Camera,
private actionSheetCtrl: ActionSheetController,
// public ngProgress: NgProgress
) {}
async selectSource() {
let actionSheet = await this.actionSheetCtrl.create({
buttons: [
{
text: 'Use Library',
handler: () => {
this.getPicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
}, {
text: 'Capture Image',
handler: () => {
this.getPicture(this.camera.PictureSourceType.CAMERA);
}
}, {
text: 'Cancel',
role: 'cancel'
}
]
});
actionSheet.present();
}
getPicture(sourceType: PictureSourceType) {
this.camera.getPicture({
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
sourceType: sourceType,
allowEdit: true,
saveToPhotoAlbum: false,
correctOrientation: true
}).then((imageData) => {
this.selectedImage = `data:image/jpeg;base64,${imageData}`;
});
}
recognizeImage() {
(async () => {
await this.worker.load();
this.progress = true;
await this.worker.loadLanguage('eng');
await this.worker.initialize('eng');
const { data: { text } } = await this.worker.recognize(this.selectedImage);
this.imageText = JSON.stringify(text);
// this.imageText = this.imageText.split('\n').join("<br />")
this.progress = false;
await this.worker.terminate();
})();
// Tesseract.recognize(this.selectedImage)
// .progress(message => {
// if (message.status === 'recognizing text')
// this.ngProgress.set(message.progress);
// console.log('progressing....')
// })
// .catch(err => console.error(err))
// .then(result => {
// this.imageText = result.text;
// })
// .finally(resultOrError => {
// this.ngProgress.done();
// console.log('finished.');
// });
}
}
我在 ionic 中使用 tesseract.js 从图像中提取文本。现在有没有办法在键值对中表示提取的文本?
我想以键值对的形式表示从图像中提取的文本,例如:{"name":"john", "title":"anniversary"}。有没有办法做到这一点?
解决方案
推荐阅读
- python - 散景悬停工具如何为相同坐标的多线段显示多个工具提示条目
- react-native - 如何在反应原生选项卡视图上隐藏一些选项卡?
- angularjs - 无法使用 ngUpgrade 在 Angular 8 中运行 AngularJS
- hibernate - Hibernate 中的 HQL 查询针对不同的用户选择相同的结果集
- microsoft-graph-api - Office 365 使用报告的 Microsoft Graph API 不适用于 Edge 浏览器
- c# - 如何使用 NPOI 导出列表数据
- r - ggplot直方图和分布相同的水平
- google-colaboratory - Google Colab:运行所有单元格,直到当前单元格
- angular - 未捕获的类型错误:无法在 registerNgModuleType 读取未定义的属性“id”-角度 PWA
- javascript - 如何优化 JS 中的拉推元素?