如何修复AttributeError: ‘JpegImageFile’对象没有属性’load_img’

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

在这个项目中,我想制作一个汽车预测模型,但在使用keras.preprocessing函数中的load_image时遇到了一个错误,错误信息是’JpegImageFile’对象没有属性’load_img’

这是我的代码

from google.colab.patches import cv2_imshowimport cv2import globfrom keras.preprocessing import imageimport numpy as npambil = 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_))

回答:

你重写了image。你有这两行代码:

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

你从keras.processing导入了image,但随后在第二行显示的代码中重写了它。

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注