python - Heroku 上的 OpenCV 错误,但模型成功部署
问题描述
我正在尝试在 Heroku 上部署深度学习 Flask 应用程序。它已成功部署,但仍然给我Application Error
消息。我检查了日志,但没有找到任何东西。帮我!
构建日志 -
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the same version as the last build: python-3.9.7
To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.9.7
-----> Installing pip 21.3.1, setuptools 57.5.0 and wheel 0.37.0
-----> Installing SQLite3
-----> Installing requirements with pip
Collecting Flask==1.1.2
Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
.
.
Successfully installed Flask-1.1.2 Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-1.0.1 absl-py-0.15.0 astunparse-1.6.3 cachetools-4.2.4 certifi-2021.10.8 charset-normalizer-2.0.7 click-8.0.3 flatbuffers-1.12 gast-0.4.0 google-auth-2.3.3 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.34.1 gunicorn-20.0.4 h5py-3.1.0 idna-3.3 itsdangerous-1.1.0 keras-nightly-2.5.0.dev2021032900 keras-preprocessing-1.1.2 markdown-3.3.4 numpy-1.19.5 oauthlib-3.1.1 opencv-python-4.5.3.56 opt-einsum-3.3.0 protobuf-3.19.1 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.26.0 requests-oauthlib-1.3.0 rsa-4.7.2 six-1.15.0 tensorboard-2.7.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.0 tensorflow-cpu-2.5.0 tensorflow-estimator-2.5.0 termcolor-1.1.0 tqdm-4.59.0 typing-extensions-3.7.4.3 urllib3-1.26.7 wrapt-1.12.1
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 439.1M
-----> Launching...
! Warning: Your slug size (439 MB) exceeds our soft limit (300 MB) which may affect boot time.
Released v4
https://image-captionss.herokuapp.com/ deployed to Heroku
这是我的app.py
-
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1
@app.route('/')
def index():
return render_template('index.html')
@app.route('/after', methods = ['GET', 'POST'])
def after():
global model, resnet, vocab, inv_vocab
img = request.files['file1']
img.save('static/file.jpg')
print("=" * 50)
print("IMAGE SAVED")
image = cv2.imread('static/file.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224))
image = np.reshape(image, (1, 224, 224, 3))
incept = resnet.predict(image).reshape(1, 2048)
print("=" * 50)
print("Predict Features")
text_in = ['startofseq']
final = ''
count = 0
while tqdm(count < 20):
count += 1
encoded = []
for i in text_in:
encoded.append(vocab[i])
padded = pad_sequences([encoded], maxlen = 36, padding = 'post', truncating = 'post')
sampled_index = np.argmax(model.predict([incept, padded]))
sampled_word = inv_vocab[sampled_index]
if sampled_word != 'endofseq':
final = final + ' ' + sampled_word
text_in.append(sampled_word)
return render_template('after.html', data = final)
if __name__ == "__main__":
app.run(debug=True)
这是我的 GitHub 存储库 -链接
我的应用程序日志:
2021-11-08T06:11:59.657691+00:00 app[web.1]: File "app.py", line 1, in <module>
2021-11-08T06:11:59.657772+00:00 app[web.1]: import cv2
2021-11-08T06:11:59.657781+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/cv2/__init__.py", line 5, in <module>
2021-11-08T06:11:59.657862+00:00 app[web.1]: from .cv2 import *
2021-11-08T06:11:59.657876+00:00 app[web.1]: ImportError: libGL.so.1: cannot open shared object file: No such file or directory
2021-11-08T06:11:59.842143+00:00 heroku[web.1]: Process exited with status 1
2021-11-08T06:11:59.906506+00:00 heroku[web.1]: State changed from starting to crashed
2021-11-08T06:12:08.000000+00:00 app[api]: Build succeeded
2021-11-08T06:12:11.971382+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=image-captionss.herokuapp.com request_id=bc6b146e-11bb-407e-a861-ba9244a954e3 fwd="152.57.22.102" dyno= connect= service= status=503 bytes= protocol=https
2021-11-08T06:12:12.623298+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=image-captionss.herokuapp.com request_id=74968483-821f-459c-b317-b7e02e8a5c82 fwd="152.57.22.102" dyno= connect= service= status=503 bytes= protocol=https
解决方案
通过更改以下行已解决此错误requirements.txt
:
opencv-python==4.2.0
至
opencv-python-headless==4.2.0.32
推荐阅读
- swiftui - 在 SwiftUI 的 TabView 中显示选定的选项卡
- linux - 在命令行中使用单引号作为密码
- c++ - 在不打开外部程序/窗口 C++ 的情况下获取 ping
- java - 主进程结束后如何运行子进程
- c - c 随机 int 类型
- java - 如何使正则表达式仅匹配 excat 邮政编码?
- android - Paint 对象的 alpha 从 float 到 int 的转换
- image - Flutter drawImage canvas一直报错对象已被处理
- sql-server - 如何为 2 个表设置外键约束?
- java - Swift StaticFunction vs Java StaticFunction ,函数声明中需要标识符吗?