&引用;找不到满足要求cv2==1.0“的版本;将带有Anaconda和OpenCV的Docker应用程序部署到Heroku时
我正在尝试向Heroku部署一个简单的Python应用程序,该应用程序支持Conda 我可以毫无问题地将这个简单的示例部署到Heroku: 但当我添加cv2导入时,它不再工作: 原始app.py文件:&引用;找不到满足要求cv2==1.0“的版本;将带有Anaconda和OpenCV的Docker应用程序部署到Heroku时,heroku,docker,anaconda,conda,miniconda,Heroku,Docker,Anaconda,Conda,Miniconda,我正在尝试向Heroku部署一个简单的Python应用程序,该应用程序支持Conda 我可以毫无问题地将这个简单的示例部署到Heroku: 但当我添加cv2导入时,它不再工作: 原始app.py文件: from flask import Flask, jsonify from sklearn import datasets, svm app = Flask(__name__) # Load Dataset from scikit-learn. digits = datasets.load_
from flask import Flask, jsonify
from sklearn import datasets, svm
app = Flask(__name__)
# Load Dataset from scikit-learn.
digits = datasets.load_digits()
@app.route('/')
def hello():
clf = svm.SVC(gamma=0.001, C=100.)
clf.fit(digits.data[:-1], digits.target[:-1])
prediction = clf.predict(digits.data[-1:])
return jsonify({'prediction': repr(prediction)})
if __name__ == '__main__':
app.run(host='0.0.0.0')
from flask import Flask, jsonify, render_template, request, redirect, url_for, send_from_directory
from sklearn import datasets, svm
import os
import json
import requests
# browser the file that the user just uploaded
from werkzeug import secure_filename
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras import applications
import cv2, numpy as np
app = Flask(__name__)
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg', 'gif'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def hello_world():
return 'Hello World!'
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
# filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
file.save(file.filename)
im = cv2.resize(cv2.imread(file.filename), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
out = model.predict(im)
decoded = decode_predictions(out, top=3)[0]
print('Predicted:', decoded)
return str(decoded)
if __name__ == '__main__':
# Test pretrained model
model = applications.VGG19(include_top=True, weights='imagenet')
app.run()
修改的app.py文件:
from flask import Flask, jsonify
from sklearn import datasets, svm
app = Flask(__name__)
# Load Dataset from scikit-learn.
digits = datasets.load_digits()
@app.route('/')
def hello():
clf = svm.SVC(gamma=0.001, C=100.)
clf.fit(digits.data[:-1], digits.target[:-1])
prediction = clf.predict(digits.data[-1:])
return jsonify({'prediction': repr(prediction)})
if __name__ == '__main__':
app.run(host='0.0.0.0')
from flask import Flask, jsonify, render_template, request, redirect, url_for, send_from_directory
from sklearn import datasets, svm
import os
import json
import requests
# browser the file that the user just uploaded
from werkzeug import secure_filename
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras import applications
import cv2, numpy as np
app = Flask(__name__)
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg', 'gif'])
# For a given file, return whether it's an allowed type or not
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def hello_world():
return 'Hello World!'
# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
# Get the name of the uploaded file
file = request.files['file']
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
# filename = secure_filename(file.filename)
# Move the file form the temporal folder to
# the upload folder we setup
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# Redirect the user to the uploaded_file route, which
# will basicaly show on the browser the uploaded file
file.save(file.filename)
im = cv2.resize(cv2.imread(file.filename), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
out = model.predict(im)
decoded = decode_predictions(out, top=3)[0]
print('Predicted:', decoded)
return str(decoded)
if __name__ == '__main__':
# Test pretrained model
model = applications.VGG19(include_top=True, weights='imagenet')
app.run()
将此修改后的应用程序推送到Heroku时,我将收到此错误:
Could not find a version that satisfies the requirement cv2==1.0 (from -r /tmp/requirements.txt (line 10)) (from versions: )
No matching distribution found for cv2==1.0 (from -r /tmp/requirements.txt (line 10))
The command '/bin/sh -c pip install -qr /tmp/requirements.txt' returned a non-zero code: 1
! Error: docker build exited with 1
看来Docker容器上没有cv2了?我在示例项目的Dockerfile模板中添加了这一行“RUN conda install-c conda forge opencv”,但它没有帮助:
FROM heroku/miniconda
# Grab requirements.txt.
ADD ./webapp/requirements.txt /tmp/requirements.txt
# Install dependencies
RUN pip install -qr /tmp/requirements.txt
# Add our code
ADD ./webapp /opt/webapp/
WORKDIR /opt/webapp
RUN conda install scikit-learn
RUN conda install -c conda-forge opencv
CMD gunicorn --bind 0.0.0.0:$PORT wsgi=
有什么帮助吗?您看到的错误与pip无法安装pypi索引中列出的版本为
cv2
且版本为1.0
的pip软件包有关。显然,该软件包描述是“我的博客分发实用程序”,并且在中不存在任何文件,这就是您看到类似以下错误的原因:
(test) ubuntu@condaexpts:~$ pip install cv2==1.0
Collecting cv2==1.0
Could not find a version that satisfies the requirement cv2==1.0 (from versions: )
No matching distribution found for cv2==1.0
很明显,这不是您想要的软件包。因此,请从文件requirements.txt
您真正想要的是opencv的conda软件包,您可以从以下网站获得:
RUN conda install -yc conda-forge opencv
谢谢你说得对,我使用的是由pipreqs生成的requirements.txt文件,它错误地添加了cv2。