Sphinx autodoc and OpenCV (cv2) errors (Python 3.7)



首先,说我对狮身人面像完全陌生。我正在做一个使用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:ProgramDataAnaconda3envsBook_OpenCV3libsite-packagessphinxextautodocimporter.py", line 32, in import_module
return importlib.import_module(modname)
File "C:ProgramDataAnaconda3envsBook_OpenCV3libimportlib__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:UsersCarlosPycharmProjectsBook_OpenCV3Chapter_01ch01_06_movie_and_beads.py", line 244, in <module>
resized_frame = resize_with_aspect_ratio(frame, width=1000)
File "C:UsersCarlosPycharmProjectsBook_OpenCV3Chapter_01ch01_06_movie_and_beads.py", line 222, in resize_with_aspect_ratio
(h, w) = img.shape[:2]
File "C:ProgramDataAnaconda3envsBook_OpenCV3libsite-packagessphinxextautodocmock.py", line 57, in __getitem__
return _make_subclass(key, self.__display_name__, self.__class__)()
File "C:ProgramDataAnaconda3envsBook_OpenCV3libsite-packagessphinxextautodocmock.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不喜欢我的切片。Shape[:2],但如果我必须手动修改整个代码,那将是一场噩梦。既然我说我是斯芬克斯汽车公司的新手,我是不是做错了什么?我错过了什么?提前谢谢。

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

希望这能帮助到别人!

相关内容

最新更新