英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论