【发布时间】:2022-01-04 14:01:36
【问题描述】:
我使用以下 Youtube 编写了一个代码,并且 youtuber 没有遇到此代码的问题,但我遇到了.. 代码似乎没有问题,但我认为这是关于带有 anaconda 或 tensorflow 或其他程序的程序...... 我已经在 google 上花了 3 个小时来解决这个问题,但甚至找不到任何线索....
代码是
import tensorflow.keras
import numpy as np
import cv2
model = tensorflow.keras.models.load_model('keras_model.h5')
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, img = cap.read()
if not ret:
break
img = cv2.flip(img, 1)
h, w, c = img.shape
img = img[:, 100:100+h]
img_input = cv2.resize(img, (224,224))
img_input = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_input = (img.astype(np.float32) / 127.0) - 1.0
img_input = np.expand_dims(img, axis=0)
prediction = model.predict(img_input)
print(prediction)
cv2.imshow('result', img)
if cv2.waitKey(1) == ord('q'):
break
错误是
Traceback (most recent call last):
File "d:\Python work\Rock\main.py", line 24, in <module>
prediction = model.predict(img_input)
File "d:\anaconda3\envs\test\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "d:\anaconda3\envs\test\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1621, in predict_function
*
return step_function(self, iterator)
File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1611, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1604, in run_step **
outputs = model.predict_step(data)
File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\training.py", line 1572, in predict_step
return self(x, training=False)
File "d:\anaconda3\envs\test\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "d:\anaconda3\envs\test\lib\site-packages\keras\engine\input_spec.py", line 263, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential_24" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 480, 480, 3)
[ WARN:1] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
--版本-- keras 2.7.0 蟒蛇 3.9.7 张量流 2.7.0
如果您需要更多信息,请告诉我。 如果你能解决这个问题,我将不胜感激......
【问题讨论】:
标签: python tensorflow anaconda