代码之家  ›  专栏  ›  技术社区  ›  tree em

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

  •  -1
  • tree em  · 技术社区  · 5 年前

    我在用烧瓶,

    在我的 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 回复  |  直到 5 年前
        1
  •  1
  •   Arunmozhi    5 年前

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