Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easyocr.recognize is significantly slower when given several boxes to estimate, rather than running it several time with one box each time #358

Closed
fxmarty opened this issue Jan 28, 2021 · 4 comments

Comments

@fxmarty
Copy link

fxmarty commented Jan 28, 2021

Hello,

Thank you for this tool, it is great. I want to build on top of it, and execution time is a matter of importance for me (even on CPU).

I don't know if it's a bug or a feature, but I've noticed that easyocr.recognize is significantly slower when called once and given n boxes to estimate the text in, rather than called n times with one box each time.

How to reproduce

1/ Download
example

Run

import easyocr
reader = Reader(['ja'], gpu = False)
image = "path/to/example.png"

result = reader.detect(image)
print(result)
# returns ([[117, 471, -3, 37], [120, 596, 32, 64]], [])

Then run

import time

L = result[0]

start_time = time.time()
estimate = reader.recognize(filename,
                            horizontal_list=L,
                            free_list=[]
                            )
print(estimate)
print("--- %s seconds ---" % (time.time() - start_time))

and

start_time = time.time()
for box in L:
    estimate = reader.recognize(filename,
                                horizontal_list=[box],
                                free_list=[]
                                )
    print(estimate)
print("--- %s seconds ---" % (time.time() - start_time))

For me the first one takes ~3.45s, and the second one takes ~2.95s, i.e. about 15% faster.

Is it an expected behavior?

Many thanks!

@rkcosmos
Copy link
Contributor

rkcosmos commented Feb 1, 2021

It should not be significantly slower. Can you try with other cases to see if this behavior persist? (especially with a lot of boxes)

@fxmarty
Copy link
Author

fxmarty commented Feb 1, 2021

Thank you very much for your answer!

I tried with the following image:
MERGED

Note that I run (for now) on CPU so I am very slow.

Here is my script

from easyocr import Reader
import time
import numpy as np

reader = Reader(['ja'], gpu = False)
image = "/path/to/image.png"

result = reader.detect(image)
print(result)

L = result[0]

tps = []
for i in range(10):
    start_time = time.time()
    estimate = reader.recognize(image,
                                horizontal_list=L,
                                free_list=[]
                                )
    print("--- %s seconds ---" % (time.time() - start_time))
    tps.append(time.time() - start_time)
print("Mean time (10 runs) all at once:", np.mean(tps), "seconds")

tps = []
for i in range(10):
    start_time = time.time()
    for box in L:
        estimate = reader.recognize(image,
                                    horizontal_list=[box],
                                    free_list=[]
                                    )
    print("--- %s seconds ---" % (time.time() - start_time))
    tps.append(time.time() - start_time)
print("Mean time (10 runs) one by one:", np.mean(tps), "seconds")

Output:

Using CPU. Note: This module is much faster with a GPU.

([[178, 531, -3, 35], [181, 513, 27, 67], [231, 361, 59, 97], [233, 479, 89, 131], [229, 480, 116, 164], [231, 417, 155, 193], [183, 249, 185, 223], [259, 531, 185, 223], [183, 279, 215, 253], [258, 448, 243, 283], [247, 465, 276, 312], [247, 463, 308, 344], [233, 477, 338, 376], [233, 419, 371, 409], [231, 482, 395, 443], [218, 348, 429, 474], [219, 493, 465, 503], [227, 487, 495, 533], [275, 337, 529, 547], [191, 525, 539, 581], [191, 409, 572, 612], [324, 390, 606, 638], [211, 501, 627, 671], [325, 387, 661, 697]], [])

--- 33.16319298744202 seconds ---
--- 30.86881136894226 seconds ---
--- 32.36347842216492 seconds ---
--- 43.46124005317688 seconds ---
--- 31.177638292312622 seconds ---
--- 41.59009075164795 seconds ---
--- 29.97151231765747 seconds ---
--- 34.05026316642761 seconds ---
--- 34.657999992370605 seconds ---
--- 33.71805453300476 seconds ---
Mean time (10 runs) all at once: 34.50231809616089 seconds

--- 16.246243715286255 seconds ---
--- 19.049020528793335 seconds ---
--- 27.189735889434814 seconds ---
--- 20.718939065933228 seconds ---
--- 20.56512212753296 seconds ---
--- 17.336503267288208 seconds ---
--- 16.20930004119873 seconds ---
--- 19.736635446548462 seconds ---
--- 23.933078050613403 seconds ---
--- 24.421704053878784 seconds ---
Mean time (10 runs) one by one: 20.540727972984314 seconds

I didn't specifically check CPU usage during the two runs, but at least on my side I had nothing new / different running between the two. I wonder if you can reproduce it or if it comes from me.

Thanks again!

@rkcosmos
Copy link
Contributor

rkcosmos commented Feb 2, 2021

Thanks for pointing this out. I can reproduce this undesirable behavior. I'll look into the cause of this problem.

@rkcosmos
Copy link
Contributor

rkcosmos commented Feb 3, 2021

Last update should fix this. It should now a little bit faster (<10%) to process n boxes comparing to manual looping. Please install the last code (pip install --upgrade git+git://github.com/jaidedai/easyocr.git) and confirm if it fix your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants