Trying to use easyOCR in aws using docker container image

169 views Asked by At

I am trying to set easyOCR to work on aws lambda with container and easyOCR keep trying to write inside /home instead of /tmp which cause that error to appear. thats my dockerfile:

FROM public.ecr.aws/lambda/python:3.9

ENV LANG=en_US.UTF-8

RUN yum -y update && \
    yum -y install wget libstdc++ gcc gcc-c++ make libjpeg-devel libpng-devel libtiff-devel zlib-devel && \
    yum group install -y "Development Tools"

RUN pip install --upgrade pip && \
    pip install --no-cache-dir --default-timeout=1000 \
    #opencv-python-headless==4.8.0.76 \
    numpy==1.25.2 \
    Pillow==10.0.0\
    easyocr==1.7.1

WORKDIR /var/task

RUN mkdir /tmp/easyocr

RUN chmod 777 /tmp/easyocr

COPY awslambda.py ${LAMBDA_TASK_ROOT}

CMD [ "awslambda.lambda_handler" ]

this is the function itself in the lambda:

model_storage_directory1 = '/tmp/easyocr'

reader = easyocr.Reader(['en'], model_storage_directory=model_storage_directory1)

def get_text_from_box(base64_str):
    image = base64_to_image(base64_str)
    enhancer = ImageEnhance.Contrast(image)
    sharpened_image = enhancer.enhance(1.2)  # Increase contrast
    sharpened_image = sharpened_image.filter(ImageFilter.SHARPEN)
    factor = 6  
    sharpened_image = sharpened_image.resize((sharpened_image.width * factor, sharpened_image.height * factor), Image.LANCZOS)
    sharpened_image_gray = sharpened_image.convert('L')
    result = reader.readtext(np.array(sharpened_image_gray))
    lines = [entry[1] for entry in result]
    cleaned_lines = [remove_special_characters_at_start(line) for line in lines]
    cleaned_text = '\n'.join(cleaned_lines)

    print(cleaned_text)
    return cleaned_text + "\n"

when I trigger the lambda I get these errors:

[ERROR] OSError: [Errno 30] Read-only file system: '/home/sbx_user1051'
Traceback (most recent call last):
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/var/task/awslambda.py", line 63, in <module>
    reader = easyocr.Reader(['en'], model_storage_directory=model_storage_directory1)
  File "/var/lang/lib/python3.9/site-packages/easyocr/easyocr.py", line 65, in __init__
    Path(self.user_network_directory).mkdir(parents=True, exist_ok=True)
  File "/var/lang/lib/python3.9/pathlib.py", line 1327, in mkdir
    self.parent.mkdir(parents=True, exist_ok=True)
  File "/var/lang/lib/python3.9/pathlib.py", line 1327, in mkdir
    self.parent.mkdir(parents=True, exist_ok=True)
  File "/var/lang/lib/python3.9/pathlib.py", line 1323, in mkdir
    self._accessor.mkdir(self, mode)

The question how I force easyOCR to write in \tmp instead of \home

0

There are 0 answers