【问题标题】:AttributeError: 'NoneType' object has no attribute 'image_data_format' kerasAttributeError:“NoneType”对象没有属性“image_data_format”keras
【发布时间】:2020-10-29 14:02:51
【问题描述】:

出于测试目的,我正在尝试使用预训练的 vgg-19 模型对实时对象进行分类,我还在代码中启用了多线程。当我运行以下代码时,我检索到错误。我无法弄清楚我尝试了不同的解决方案,但我无法修复它。

  from keras.applications.vgg19 import decode_predictions
  from keras_applications.vgg19 import VGG19, preprocess_input
  label = ''
  frame = None
  class MyThread(threading.Thread):
  def __init__(self):
     threading.Thread.__init__(self)
  def run(self):
     global label
     print("[INFO] loading network...")
     self.model = VGG19(weights="imagenet")
     while (~(frame is None)):
        (inID, label) = self.predict(frame)
  def predict(self, frame):
    image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
    image = image.transpose((2, 0, 1))
    image = image.reshape((1,) + image.shape)
    image = preprocess_input(image)
    preds = self.model.predict(image)
    return decode_predictions(preds)[0]
 cap = cv2.VideoCapture(0)
 if (cap.isOpened()):
   print("Camera OK")
 else:
   cap.open()
keras_thread = MyThread()
keras_thread.start()
while (True):
 ret, original = cap.read()
 frame = cv2.resize(original, (224, 224))
 cv2.putText(original, "Label: {}".format(label), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
 cv2.imshow("Classification", original)
 if (cv2.waitKey(1) & 0xFF == ord('q')):
     break;
cap.release()
frame = None
cv2.destroyAllWindows()
sys.exit()

以下是我的错误,我在运行代码时检索到

     File "C:\miniconda3\envs\tensorflow\lib\threading.py", line 916, in _bootstrap_inner
     self.run()
   File "C:/Users/video_testing.py", line 17, in run
      self.model = VGG16(weights=None)
   File "C:\miniconda3\envs\tensorflow\lib\site-packages\keras_applications\vgg16.py", line 97, in VGG16
   data_format=backend.image_data_format(),
   AttributeError: 'NoneType' object has no attribute 'image_data_format'
   Process finished with exit code -1

谢谢,非常感谢您的帮助。

【问题讨论】:

标签: python-3.x opencv keras tensor conv-neural-network


【解决方案1】:

根据this你需要更改这一行:

from keras_applications.vgg19 import VGG19, preprocess_input

收件人:

from keras.applications.vgg19 import VGG19, preprocess_input

【讨论】:

  • 嗨,非常感谢您的大力帮助,@MinaAbdEl-Massih
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2019-01-01
相关资源
最近更新 更多