【问题标题】:How to Fix AttributeError: 'JpegImageFile' object has no attribute 'load_img'如何修复 AttributeError:“JpegImageFile”对象没有属性“load_img”
【发布时间】:2021-04-20 02:50:05
【问题描述】:

我是初学者,正在学习编写图像分类器。我的目标是创建一个预测函数。

在这个项目中我想制作一个汽车预测模型,当我从 keras.preprocessing 函数中加载图像时出现问题 JpegImageFile' object has no attribute 'load_img'

这是我的代码

from google.colab.patches import cv2_imshow
import cv2
import glob
from keras.preprocessing import image
import numpy as np
ambil = glob.glob("*.jpg")
for foto in ambil:
  lol = cv2.imread(foto)
  with open(foto, 'rb') as f:
    np_image_string = np.array([f.read()])
    image = Image.open(foto)
    width, height = image.size
    gambar_masuk = np.array(image.getdata()).reshape(height, width, 3).astype(np.uint8)

    num_detections, detection_boxes, detection_classes, detection_scores, detection_masks, image_info = session.run(
      ['NumDetections:0', 'DetectionBoxes:0', 'DetectionClasses:0', 'DetectionScores:0', 'DetectionMasks:0', 'ImageInfo:0'],
      feed_dict={'Placeholder:0': np_image_string})

    num_detections = np.squeeze(num_detections.astype(np.int32), axis=(0,))
    detection_boxes = np.squeeze(detection_boxes * image_info[0, 2], axis=(0,))[0:num_detections]
    detection_scores = np.squeeze(detection_scores, axis=(0,))[0:num_detections]
    detection_classes = np.squeeze(detection_classes.astype(np.int32), axis=(0,))[0:num_detections]
    detection_boxes = detection_boxes[detection_classes==3]
    detection_scores = detection_scores[detection_classes==3]
    detection_boxes = detection_boxes[detection_scores>0.8]
    detection_boxes = detection_boxes.astype(int)
    print(detection_boxes)
    urut=1
    for kotak in detection_boxes:
      hasil = lol[kotak[0]:kotak[2],kotak[1]:kotak[3],:]
      hasil_potong = 'hasil'+str(urut)+'.jpg'
      cv2.imwrite(hasil_potong, hasil)
      lihat = cv2.imread(hasil_potong)
      cv2_imshow(lihat)
      img = image.load_img(lihat, target_size = (size_, size_))

【问题讨论】:

    标签: python machine-learning keras computer-vision keras-layer


    【解决方案1】:

    您正在覆盖image。你有这两行:

    from keras.preprocessing import image
        :
        :
        image = Image.open(foto)
    

    您从keras.processing 导入image,但随后在显示的第二行中覆盖它。

    要么以不同的方式导入image,要么为打开的图像使用不同的变量名...

    【讨论】:

    • 伙计,你说得对,一个简单的错误让我几天无法入睡,非常感谢你
    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多