You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow Object Detection API报错:找不到object_detection模块如何解决?

问题描述

我正在遵循TensorFlow Object Detection API的Windows 10多目标训练教程,但运行以下命令时出现错误:

python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record

错误信息如下:

C:\Users\ASUS PC\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "generate_tfrecord.py", line 20, in <module>
    from object_detection.utils import dataset_util
ModuleNotFoundError: No module named 'object_detection'
解决方案

这个ModuleNotFoundError是TensorFlow Object Detection API入门时非常常见的问题,核心原因就是Python找不到object_detection模块的路径。下面是几个经过验证的解决办法:

1. 临时配置PYTHONPATH环境变量

Python需要知道Object Detection API的位置,你可以在当前命令行窗口中临时添加路径:

  • 找到你下载的API根目录(一般是models/research)
  • 运行以下命令(把路径替换成你自己的实际路径):
    set PYTHONPATH=C:\your\path\to\models;C:\your\path\to\models\research;C:\your\path\to\models\research\slim
    
    注意:这个设置只在当前命令行窗口生效,关闭后就失效了。如果想永久生效,需要在系统的环境变量中添加这三个路径到PYTHONPATH里。

2. 切换到API的research目录下运行脚本

确保你的命令行当前工作目录是models/research,再执行generate_tfrecord.py的命令(如果脚本不在这个目录,要写完整的文件路径)。示例:

cd C:\your\path\to\models\research
python C:\your\script\path\generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record

3. 安装API的依赖和模块

你可能还没完成API的安装步骤,按以下操作来:

  1. 切换到models/research目录,运行:
    python setup.py install
    
  2. 再切换到models/research/slim目录,运行:
    python setup.py install
    

这会把API的模块安装到你的Python环境中,让系统能找到object_detection。

4. 检查Anaconda环境配置

如果你用Anaconda管理环境,先确保激活了正确的TensorFlow环境:

conda activate your_tensorflow_env

并且在这个环境里已经安装了所有必要的依赖(TensorFlow、h5py、protobuf等)。

另外,那个h5py的警告只是版本兼容性提示,不会影响脚本运行,你可以直接忽略它,或者更新h5py到最新版本消除警告:

pip install --upgrade h5py

内容的提问来源于stack exchange,提问作者Taimur Islam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:54:37