Irek
Irek

Reputation: 197

No module named 'cv2.aruco'

I have a bit of a problem when trying to use Aruco on Raspberry Pi 3.
I am using Python 3.5 on headless version of Raspbian and I followed this tutorial to install OpenCV with opencv_contrib - which as far as I understand contains all the packages:

https://www.pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/

OpenCV installed without any problems, I can import it in Python and it works fine. I need to use Aruco for a project and when I try to import it I get this error:

Traceback (most recent call last):
  File "<stdin>", line1, in <module>
ImportError: No module named 'cv2.aruco': 'cv2' is not a package

I had the same problem before so I flashed SD card and started with fresh installation, but it happens again. From the previous question I found here I tried to install opencv_contrib by using

pip install opencv-contrib-python

But I got the error:

Could not find a version that satisfies the requirement opencv-contrib-python (from versions: )  
No matching distribution found for opencv-contrib-python

Did anybody encounter the same issue, or do you have any suggestions or how to fix that? Any help would be appreciated.

Upvotes: 1

Views: 4677

Answers (2)

tbob
tbob

Reputation: 111

I followed the answer by Irek and have only some minor tweaks, I skipped step 3 and it worked fine, actually trying to do step 3 and I ran into issues. Also, after step 2 I run make && make install instead of step 5. I am running this in a dockerfile building on an Ubuntu 20.04 image.

Upvotes: 0

Irek
Irek

Reputation: 197

EDIT: I found the perfect solution for the problem with installing additional modules for OpenCV. Previous solution required the manual copying of files from one github directory into another, and copied files had to be added manually into the txt file. This solution allows you to install all the modules without any copying etc. I am leaving my initial thoughts in the answer as they were, the only thing that is changing are the steps to follow. Please correct me if I did anything wrong in regards to editing, as I never had to edit my own answer before due to finding a better solution (as you may see, I'm quite new in here).

I managed to solve my problem and thought I will share my solution as a new answer, so it's easier for people to see, should anyone encounter the same problem in the future.
The solution works fine when there is only need to install few modules from opencv_contrib as it requires some copying.

The overall installation process requires following this tutorial https://www.pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/

New process, after finding better solution:

  1. Clone both OpenCV and OpenCV_contrib from GitHub
  2. Use cmake command from the tutorial listed above to create installation files for OpenCV. My command looked like this:

    cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..

  3. Once this command is completed and you get no errors, run cmake -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules .. again
  4. This should also work without any errors, and to double check if all the modules are going to install just scroll up the output result and you should see the line "Modules to install: " and there should be list of all modules included in opencv_contrib/modules.
  5. Once that is done and you confirmed it will install all you want/need, you can carry on with the tutorial by doing make -j4.

I know it's simply duplicating a line that was included in cmake before and I have no clue why it works after using this command again. If someone could explain why that happens, I would appreciate it and it would give me some understanding as to why it works. Again, I hope this answer helps anyone who goes through the same problem. Also, I'm sorry for editing this post twice, I thought it will be useful to show people more correct way of sorting this problem out.

Upvotes: 2

Related Questions