首页 > 解决方案 > 我在 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"}。有没有办法做到这一点?

这是输出图像。

标签: node.jsionic-framework

解决方案


推荐阅读