Can't use opencv with a flask server on elastic beanstalk

Question:

So I’m running a very basic elastic beanstalk server. It’s a python version 3.7(and tried on 3.8) server using linux 2. I tried putting up a simple server with an application.py that looks like this:

from flask import Flask
import cv2

application = Flask(__name__)


@application.route("/")
def index():
    return "Hello World!"


# run the app.
if __name__ == "__main__":
    application.debug = True
    application.run()

and a requirements.txt file with the imports that look like this

click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
numpy==1.20.2
opencv-python==4.4.0.46
Werkzeug==1.0.1

Whenever I try to import cv2 I get an error from elastic beanstalk. Here’s the error report

----------------------------------------
/var/log/web.stdout.log
----------------------------------------
Apr 21 21:04:29 ip-172-31-88-38 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Apr 21 21:04:29 ip-172-31-88-38 web: self.callable = self.load()
Apr 21 21:04:29 ip-172-31-88-38 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
Apr 21 21:04:29 ip-172-31-88-38 web: return self.load_wsgiapp()
Apr 21 21:04:29 ip-172-31-88-38 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
Apr 21 21:04:29 ip-172-31-88-38 web: return util.import_app(self.app_uri)
Apr 21 21:04:29 ip-172-31-88-38 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
Apr 21 21:04:29 ip-172-31-88-38 web: mod = importlib.import_module(module)
Apr 21 21:04:29 ip-172-31-88-38 web: File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
Apr 21 21:04:29 ip-172-31-88-38 web: return _bootstrap._gcd_import(name[level:], package, level)
Apr 21 21:04:29 ip-172-31-88-38 web: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
Apr 21 21:04:29 ip-172-31-88-38 web: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
Apr 21 21:04:29 ip-172-31-88-38 web: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
Apr 21 21:04:29 ip-172-31-88-38 web: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
Apr 21 21:04:29 ip-172-31-88-38 web: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
Apr 21 21:04:29 ip-172-31-88-38 web: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
Apr 21 21:04:29 ip-172-31-88-38 web: File "/var/app/current/application.py", line 2, in <module>
Apr 21 21:04:29 ip-172-31-88-38 web: import cv2
Apr 21 21:04:29 ip-172-31-88-38 web: File "/var/app/venv/staging-LQM1lest/lib64/python3.7/site-packages/cv2/__init__.py", line 5, in <module>
Apr 21 21:04:29 ip-172-31-88-38 web: from .cv2 import *
Apr 21 21:04:29 ip-172-31-88-38 web: ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Does anyone have any suggestions as to what I might be doing wrong? Any help would be much appreciated

Asked By: Voldy21

||

Answers:

You have to install mesa-libGL on the EB instance. For example, using .ebextensions:

.ebextensions/10_install_opengl.config

packages: 
  yum:
    mesa-libGL: []
Answered By: Marcin

For me after installing opencv-python-headless resolved this

pip3 install opencv-python-headless==4.5.3.56

Answered By: Nitin Ashutosh