fp = Builtins.open(filename,“ rb”)FileNotFoundError:[错误2]没有这样的文件或目录:

时间:2019-11-07 02:07:39

标签: python flask keras python-imaging-library

我正在使用烧瓶

在我的config.py中设置 UPLOAD_FOLDER = '/Users/kanel/Documents/Developments/upload'

和用于处理文件上传的控制器。

@app.route('/uploaded', methods = ['GET', 'POST'])
def upload_file():
   if request.method == 'POST':
      f = request.files['file']
      path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename)
      print(path)
      model= ResNet50(weights='imagenet')
      img = image.load_img(path, target_size=(224,224))
      x = image.img_to_array(img)
      x = np.expand_dims(x, axis=0)
      x = preprocess_input(x)
      preds = model.predict(x)
      preds_decoded = decode_predictions(preds, top=3)[0] 
      print(decode_predictions(preds, top=3)[0])
      f.save(path)
      return render_template('uploaded.html', title='Success', predictions=preds_decoded, user_image=f.filename)

我收到此错误:

File "/opt/anaconda3/lib/python3.7/site-packages/PIL/Image.py", line 2766, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/Users/kanel/Documents/Developments/upload/chest-example.png'

我的路线出了什么问题?据说文件不存在,但是路径在那里!

1 个答案:

答案 0 :(得分:1)

问题在于,您尝试从中加载文件的位置不存在该文件。 在使用PIL打开文件之前,应将文件保存到磁盘。 f.save(path)应该先做img = image.load_img(path, target_size=(224,224))