我第一次设置 keras ocr,想运行文档中的示例 。
我已经通过pip安装了ocr(0.9.3)和tensorflow(2.16.1) 这是代码
import matplotlib.pyplot as plt
import keras_ocr
# keras-ocr will automatically download pretrained
# weights for the detector and recognizer.
pipeline = keras_ocr.pipeline.Pipeline()
# Get a set of three example images
images = [
keras_ocr.tools.read(url) for url in [
'https://upload.wikimedia.org/wikipedia/commons/b/bd/Army_Reserves_Recruitment_Banner_MOD_45156284.jpg',
'https://upload.wikimedia.org/wikipedia/commons/e/e8/FseeG2QeLXo.jpg',
'https://upload.wikimedia.org/wikipedia/commons/b/b4/EUBanana-500x112.jpg'
]
]
# Each list of predictions in prediction_groups is a list of
# (word, box) tuples.
prediction_groups = pipeline.recognize(images)
# Plot the predictions
fig, axs = plt.subplots(nrows=len(images), figsize=(20, 20))
for ax, image, predictions in zip(axs, images, prediction_groups):
keras_ocr.tools.drawAnnotations(image=image, predictions=predictions, ax=ax)
这是错误
2024-03-12 12:24:06.949452: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-03-12 12:24:07.386269: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Looking for D:\users\xxx\.keras-ocr\craft_mlt_25k.h5
2024-03-12 12:24:08.845546: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:From D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras\src\backend\tensorflow\core.py:174: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
Traceback (most recent call last):
File "D:\users\xxx\PycharmProjects\pytorch\pytorch.py", line 7, in <module>
pipeline = keras_ocr.pipeline.Pipeline()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras_ocr\pipeline.py", line 22, in __init__
recognizer = recognition.Recognizer()
^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras_ocr\recognition.py", line 388, in __init__
) = build_model(alphabet=alphabet, **build_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras_ocr\recognition.py", line 277, in build_model
locnet_y = keras.layers.Dense(
^^^^^^^^^^^^^^^^^^^
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras\src\layers\core\dense.py", line 85, in __init__
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
File "D:\users\xxx\PycharmProjects\pytorch\.venv\Lib\site-packages\keras\src\layers\layer.py", line 265, in __init__
raise ValueError(
ValueError: Unrecognized keyword arguments passed to Dense: {'weights': [array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
我尝试用谷歌搜索东西,但据我所知,根据版本,我不应该有问题。我不希望 pypi 示例中出现错误的代码。
知道我在环境设置过程中可能会犯哪些错误吗?
我设法通过将 TensorFlow 降级到更新前的最后一个稳定版本来解决该问题:
pip install --force-reinstall -v "tensorflow==2.15.1"
在 keras-ocr 或 TensorFlow 中发布官方修复之前,此解决方法应该会有所帮助。值得注意的是,TensorFlow 官方仅支持 Python 版本 3.9 至 3.11。如果您在此之后仍然遇到问题并且使用的版本超出此范围,请考虑降级您的 Python 版本。