Python 使用atribute'获取错误;str';对象没有心房肌';阅读';

Python 使用atribute'获取错误;str';对象没有心房肌';阅读';,python,image,csv,flask,cv2,Python,Image,Csv,Flask,Cv2,我想用FlaskAPI上传图像,但我在这段代码中遇到了一个错误 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.

我想用FlaskAPI上传图像,但我在这段代码中遇到了一个错误

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_路径是一个字符串。如果要读取文件,请执行以下操作

img = open(img_path)

用你自己的话来说,如果你的代码是
img.read()
,你希望
img
的值是多少?为什么?你测试过这个假设吗?您认为在调用
.read()
时会发生什么情况?为什么?