Sphinx autodoc和OpenCV(cv2)错误(Python 3.7)

问题描述 投票:0回答:1

[首先,说我对狮身人面像完全陌生。我正在使用opencv的图像处理项目。虽然我已经能够设置狮身人面像(并通过一些简单的模块成功对其进行了测试),但我仍在努力使其与我的主模块一起使用。刚开始我遇到错误,因为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个回答
0
投票

我通过将主要代码放入__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

希望这对某人有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.