EasyOCR – 使用Python批处理图像

huangapple go评论60阅读模式
英文:

EasyOCR - Batch processing images with Python

问题

I am attempting to write a bit of python that uses EasyOCR to write the numbers it sees in the images into a text file. My goal is to batch process all images in a directory, rather than a single images at a time, as I have several thousand images to process.

The python code:

import cv2
import os
import io

reader = easyocr.Reader(['en'])

for image_name in os.listdir("ocr-source"):
        image = cv2.imread(f'ocr-source/{image_name}')
        result = reader.readtext(image, allowlist='0123456789', detail=0)
        
print(image_name, " ", result, file=open('output.txt', 'w'))

My test ocr-source directory contains about 10 images.

The resulting output.txt file only contains the results from a single image.

How to I get it to properly iterate through the entire directory?

英文:

I am attempting to write a bit of python that uses EasyOCR to write the numbers it sees in the images into a text file. My goal is to batch process all images in a directory, rather than a single images at a time, as I have several thousand images to process.

The python code:

import cv2
import os
import io

reader = easyocr.Reader(['en'])

for image_name in os.listdir("ocr-source"):
        image = cv2.imread(f'ocr-source/{image_name}')
        result = reader.readtext(image, allowlist='0123456789', detail=0)
        
print(image_name, " ", result, file=open('output.txt', 'w'))

My test ocr-source directory contains about 10 images.

The resulting output.txt file only contains the results from a single image.

How to I get it to properly iterate through the entire directory?

答案1

得分: 1

EasyOCR似乎最近支持了批量推断:https://github.com/JaidedAI/EasyOCR/pull/458

英文:

Easyocr seem to have recently supported batch inference:
https://github.com/JaidedAI/EasyOCR/pull/458

答案2

得分: 0

Simple fix: Instead of writing over the file each loop, I needed to append.

import cv2
import os
import io

reader = easyocr.Reader(['en'])

for image_name in os.listdir("ocr-source"):
    image = cv2.imread(f'ocr-source/{image_name}')
    result = reader.readtext(image, allowlist='0123456789', detail=0)

print(image_name, " ", result, file=open('output.txt', 'a'))

Note the 'a' in the print call.

英文:

Simple fix: Instead of writing over the file each loop, I needed to append.

import cv2
import os
import io

reader = easyocr.Reader(['en'])

for image_name in os.listdir("ocr-source"):
        image = cv2.imread(f'ocr-source/{image_name}')
        result = reader.readtext(image, allowlist='0123456789', detail=0)
        
print(image_name, " ", result, file=open('output.txt', 'a'))

Note the 'a' in the print call

huangapple
  • 本文由 发表于 2023年2月16日 04:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定