获取属性 'str' 的错误对象没有属性 'read'

getting error with atribute 'str' object has no atribute 'read'

我想用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' object has no attribute 'read' any solutions?

img_path 是一个字符串。如果你想从文件中读取,做

img = open(img_path)