EasyOCR icon indicating copy to clipboard operation
EasyOCR copied to clipboard

make the result json serializable

Open hubutui opened this issue 3 years ago • 2 comments

According to https://github.com/JaidedAI/EasyOCR/blob/7a685cb8c4ba14f2bc246f89c213f1a56bbc2107/easyocr/recognition.py#L137-L149 the the confidence is numpy.array, and it's not json serializable. It would be great to make it json serializable. And according to https://github.com/JaidedAI/EasyOCR/blob/7a685cb8c4ba14f2bc246f89c213f1a56bbc2107/easyocr/detection.py#L92-L110 the text box coord is ndarray too. However, I got some coords return as int, not ndarray. That is wierd. Sorry I could not provide the source image since it contains patient information.

hubutui avatar May 20 '22 00:05 hubutui

very good idea. It makes it easier to interface with non-Python programs. I would like to have read.readtext(filepath, output_format = 'json') like issues #326.

kfjt avatar Sep 23 '22 06:09 kfjt

I was getting a serialization error when getting the data from EasyOCR as a Python dict, doing some processing, then later saving it to a JSON file. In case it's helpful for anyone else, I was able to work around it by creating a custom JSON encoder:

class CustomEncoder(json.JSONEncoder):
  def default(self, obj):
    if isinstance(obj, np.integer):
      return int(obj)
    elif isinstance(obj, np.ndarray):
      return obj.tolist()
    return super().default(obj)

Then referencing my custom encoder in json.dump():

json.dump(result, f, cls=CustomEncoder)

samuelbradshaw avatar Jan 14 '25 06:01 samuelbradshaw