【问题标题】:getting error with atribute 'str' object has no atribute 'read' [closed]属性“str”对象出现错误没有属性“read”[关闭]
【发布时间】:2021-08-25 07:49:25
【问题描述】:

我想用flask API 上传图片,但是这段代码出错了。

def model_predict(img_path, model):
    img = img_path

    x = cv2.imdecode(np.fromstring(img.read(), np.uint8), cv2.IMREAD_UNCHANGED)
    x = cv2.resize(x,(224,224))
    x = np.expand_dims(x, axis=0)
    images = np.vstack([x])/ 255.0

    preds = model.predict(images)
    if preds[0] > 0.5:
      return "Good"
    else:
      return "Bad"

错误是

AttributeError: 'str' 对象没有属性 'read' 有什么解决办法吗?

【问题讨论】:

  • 用你自己的话来说,你的代码是img.read(),你期望img的值是多少?为什么?你测试过这个假设吗?当您为此致电.read() 时,您认为会发生什么?为什么?

标签: python image csv flask cv2


【解决方案1】:

img_path 是一个字符串。如果要从文件中读取,请执行

img = open(img_path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2013-10-28
    • 2017-03-02
    • 1970-01-01
    • 2016-04-07
    • 2018-08-10
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多