0

Firstly, say that I'm totally new to sphinx. I'm working on an image processing project that uses opencv. While I've been able to setup sphinx (and succesfully tested it with some simple modules), I'm struggling to make it work with my main module. At first I was getting errors because autodoc did not recognize some opencv (cv2) classes which I guess only "work" at runtime (e.g. the output of a VideoCapture.read() was interpreted as None and failed to get its shape, or cv2.imshow that broke the html building too). To avoid this I included

autodoc_mock_imports = ['cv2', 'numpy']

in my conf.py file. The above problems dissapeared, but now I'm getting a new one:

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

The code of the method mentioned is:

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)

I understand that autodoc doesn't like the slicing in im.Shape[:2], but if I have to revise manually the whole code it is going to be a nightmare. Since as I said I'm new to sphinx autodoc, am I doing something wrong? What am I missing? Thanks in advance.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Carlos Borau
  • 1,433
  • 1
  • 24
  • 34

1 Answers1

0

I solved it by placing the main code into __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

The autodoc_mock_imports is not even needed in the conf.py file.

Hope this helps someone!

Carlos Borau
  • 1,433
  • 1
  • 24
  • 34