Skip to content

Commit

Permalink
fix(OcrExtractor): 修复ocr截取图像时选区上下翻转的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
nzh63 committed Oct 22, 2023
1 parent d58427c commit 6611e8b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/extractor/OcrExtractor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ export class OcrExtractor extends IExtractor {

private async triggerRecognize() {
this.lastImage = await this.screenCapturer.capture();
this.lastCropImage = this.rect ? this.lastImage.clone().extract(this.rect) : this.lastImage.clone();
if (this.rect) {
// NOTE: 我们把图像flip过,所以这里rect同样要反转
const metatdat = await this.lastImage.metadata();
const rect: sharp.Region = {
left: this.rect.left,
width: this.rect.width,
top: (metatdat.height ?? 0) - this.rect.top - this.rect.height,
height: this.rect.height
};
this.lastCropImage = this.lastImage.clone().extract(rect);
} else {
this.lastCropImage = this.lastImage.clone();
}
this.lastCropImage = this.preprocess(this.lastCropImage);
this.ocrManager.recognize(this.lastCropImage, (e, res) => {
if (this.lastCropImage !== res.img) return;
Expand All @@ -117,7 +129,7 @@ export class OcrExtractor extends IExtractor {
if (this.preprocessOption.color === 'grey') {
img = img.clone().greyscale();
} else if (['red', 'green', 'blue'].includes(this.preprocessOption.color)) {
img = img.clone().extractChannel(this.preprocessOption.color);
img = img.clone().extractChannel(this.preprocessOption.color as any);
}
if (this.preprocessOption.threshold) {
img = img.threshold(this.preprocessOption.threshold, { greyscale: false });
Expand Down

0 comments on commit 6611e8b

Please sign in to comment.