【问题标题】:Sphinx autodoc and OpenCV (cv2) errors (Python 3.7)Sphinx autodoc 和 OpenCV (cv2) 错误 (Python 3.7)
【发布时间】:2020-03-31 16:12:33
【问题描述】:

首先,说我对 sphinx 完全陌生。我正在开发一个使用 opencv 的图像处理项目。虽然我已经能够设置 sphinx(并使用一些简单的模块成功地对其进行了测试),但我正在努力使其与我的主模块一起工作。起初我收到错误,因为 autodoc 无法识别一些我猜在运行时只能“工作”的 opencv (cv2) 类(例如 VideoCapture.read() 的输出被解释为 None 并且无法获得它的形状,或者 cv2 .imshow 也破坏了 html 构建)。为了避免这种情况,我包括了

autodoc_mock_imports = ['cv2', 'numpy']

在我的 conf.py 文件中。上述问题消失了,但现在我又遇到了一个新问题:

WARNING: autodoc: failed to import module 'ch01_06_movie_and_beads'; the following exception was       raised:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\Book_OpenCV3\lib\site-packages\sphinx\ext\autodoc\importer.py", line 32, in import_module
return importlib.import_module(modname)
File "C:\ProgramData\Anaconda3\envs\Book_OpenCV3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
 File "<frozen importlib._bootstrap>", line 983, in _find_and_load
 File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 728, in exec_module
 File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
 File "C:\Users\Carlos\PycharmProjects\Book_OpenCV3\Chapter_01\ch01_06_movie_and_beads.py", line 244, in <module>
resized_frame = resize_with_aspect_ratio(frame, width=1000)
 File "C:\Users\Carlos\PycharmProjects\Book_OpenCV3\Chapter_01\ch01_06_movie_and_beads.py", line 222, in resize_with_aspect_ratio
(h, w) = img.shape[:2]
 File "C:\ProgramData\Anaconda3\envs\Book_OpenCV3\lib\site-packages\sphinx\ext\autodoc\mock.py", line 57, in __getitem__
return _make_subclass(key, self.__display_name__, self.__class__)()
 File "C:\ProgramData\Anaconda3\envs\Book_OpenCV3\lib\site-packages\sphinx\ext\autodoc\mock.py", line 74, in _make_subclass
attrs = {'__module__': module, '__display_name__': module + '.' + name}
TypeError: can only concatenate str (not "slice") to str

上述方法的代码是:

def resize_with_aspect_ratio(img=None, width=None, height=None, inter=cv2.INTER_AREA):
  if img is None:
      return None
  (h, w) = img.shape[:2]
  if width is None and height is None:
      return img
  if width is None:
      r = height / float(h)
      dim = (int(w * r), height)
  else:
      r = width / float(w)
      dim = (width, int(h * r))
  return cv2.resize(img, dim, interpolation=inter)

我知道 autodoc 不喜欢 im.Shape[:2] 中的切片,但如果我必须手动修改整个代码,那将是一场噩梦。 既然我说我是 sphinx autodoc 的新手,我做错了什么吗?我错过了什么? 提前致谢。

【问题讨论】:

    标签: python opencv python-sphinx cv2 autodoc


    【解决方案1】:

    我通过将主代码放入__main__来解决它:

    if __name__ == "__main__":
        [...] # some stuff
        capture = cv2.VideoCapture(str(input_file))
        has_frame, frame = capture.read()
        resized_frame = resize_with_aspect_ratio(frame, width=1000)
        [...] # more stuff
    

    conf.py 文件中甚至不需要autodoc_mock_imports

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      相关资源
      最近更新 更多