【问题标题】:AttributeError: module 'keras.preprocessing.image' has no attribute 'img_to_array'AttributeError: 模块 \'keras.preprocessing.image\' 没有属性 \'img_to_array\'
【发布时间】:2022-11-04 20:18:46
【问题描述】:

我正在尝试从面部情绪出纳员的 github 运行代码,但出现属性错误,

错误是:

line 27, in <module>
    img_pixel = image.img_to_array(roi_gray)
AttributeError: module 'keras.preprocessing.image' has no attribute 'img_to_array'

完整的如下:

import os
import cv2
import numpy as np
from keras.models import model_from_json
from keras.preprocessing import image

model = model_from_json(open("fer.json", "r").read())
model.load_weights('fer.h5')


face_haar_cascade = cv2.CascadeClassifier(
    'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)

while True:
    ret, test_img = cap.read()
    if not ret:
        continue
    gray_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)

    faces_detect = face_haar_cascade.detectMultiScale(gray_img, 1.32, 5)

    for (x, y, w, h) in faces_detect:
        cv2.rectangle(test_img, (x, y), (x+w, y+h), (255, 0, 0), thickness=7)
        roi_gray = gray_img[y:y+w, x:x+h]
        roi_gray = cv2.resize(roi_gray, (48, 48))
        img_pixel = image.img_to_array(roi_gray)
        img_pixel = np.expand_dims(img_pixel, axis=0)
        img_pixel /= 255
        predictions = model.predict(img_pixel)
        max_index = np.argmax(predictions[0])
        emotions = ('angry', 'disgust', 'fear', 'happy',
                    'sad', 'surprise', 'neutral')
        predicted_emotion = emotions[max_index]
        cv2.putText(test_img, predicted_emotion, (int(x), int(y)),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

    resize_img = cv2.resize(test_img, (1000, 700))
    cv2.imshow('Facial emotion analysis ', resize_img)

    if cv2.waitKey(10) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows

当我运行代码时,相机打开一秒钟然后出现此错误,请帮我解决此错误

我试图使用与我的错误类似的其他 stackoverflow 问题来更改 keras 库的导入,但我没有得到任何解决方案

【问题讨论】:

    标签: python keras attributeerror image-preprocessing facial-identification


    【解决方案1】:

    tf.keras.preprocessing.image 是deprecated

    您可以使用

    from tensorflow.keras.utils import img_to_array
    
    ...
    
    img_pixel = img_to_array(roi_gray)
    

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2022-07-18
      • 2018-04-14
      • 2019-02-18
      • 1970-01-01
      • 2020-01-01
      • 2019-07-20
      • 2021-11-05
      相关资源
      最近更新 更多