-3

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?

stateMachine
  • 5,227
  • 4
  • 13
  • 29
  • Have you searched Google? This has been asked numerous times on this forum in different ways. – fmw42 Feb 15 '23 at 20:44
  • 1
    This is a basic programing question, rather than a Computer Vision related one. – stateMachine Feb 15 '23 at 20:48
  • You need to change the output name within the loop. Look up python format. `cv2.imwrite("output_name_{0}.png".format(loop_argument), img)`. Whatever argument you want in your loop with be put into {0}. – fmw42 Feb 15 '23 at 20:56
  • @fmw42 - Yes, the for loop based on an example on how to batch process with easyocr. – Fullerton Feb 15 '23 at 23:40

2 Answers2

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

0

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

mostafa.elhoushi
  • 3,758
  • 1
  • 17
  • 10