【问题标题】:AttributeError: 'JpegImageFile' object has no attribute 'read'AttributeError:“JpegImageFile”对象没有“读取”属性
【发布时间】:2021-02-26 06:29:30
【问题描述】:

当我运行这些代码行时,我得到了这个错误

AttributeError: 'JpegImageFile' 对象没有属性 'read'"

我似乎无法绕过它。

import streamlit as st
import cv2
import os
import numpy as np
from PIL import Image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.models import load_model

def detect_mask(image):
    image = cv2.imdecode(np.fromstring(image.read(), dtype='uint8'),1)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    (h, w) = image.shape[:2]
    blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
    net.setInput(blob)
    detection = net.forward()

【问题讨论】:

  • 您没有import 声明。我们不知道image.read() 的结果可能是什么。你在cv2.COLOR_BGR2RGB中有一个多余的撇号
  • @MarkSetchell 我已经添加了语句。

标签: tensorflow opencv


【解决方案1】:

工作示例代码

import cv2
import os
import numpy as np
from PIL import Image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.models import load_model

image ='image.jpeg'
with open(image, 'rb') as infile:
     buf = infile.read()

x = np.fromstring(buf, dtype='uint8')
img = cv2.imdecode(x, 1)
imag = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
(h, w) = imag.shape[:2]
blob = cv2.dnn.blobFromImage(imag, 1.0, (300, 300), (104.0, 177.0, 123.0))

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 2019-08-13
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    相关资源
    最近更新 更多