MonsterAuto.py OpenCV Issue

Hi;

I have installed OpenCV 3.4.0 and would like to use MonsterAuto.py for line following however when I try and start the script I get the following error:

I have installed OpenCV 3.4.0. When running MonsterAuto.py I get the following error:

pi@raspberrypi:~/monster-self-drive $ ./MonsterAuto.py
Libraries loaded
Running script in directory "."
TEST MODE: Skipping board setup
Setup camera input
Traceback (most recent call last):
File "./MonsterAuto.py", line 91, in
Settings.capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, Settings.imageWidth);
AttributeError: 'module' object has no attribute 'cv'

I believe this is due to changes between OpenCV 2 and OpenCV 3?

I have just raised this as an issue on GitHub.

Thanks

piborg's picture

There are a few minor differences for OpenCV 3 vs OpenCv 2, it should be just a few minor changes to the code.

Try changing these lines:

Settings.capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, Settings.imageWidth);
Settings.capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, Settings.imageHeight);
Settings.capture.set(cv2.cv.CV_CAP_PROP_FPS, Settings.frameRate);

to these ones:

Settings.capture.set(cv2.CAP_PROP_FRAME_WIDTH, Settings.imageWidth);
Settings.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, Settings.imageHeight);
Settings.capture.set(cv2.CAP_PROP_FPS, Settings.frameRate);

Sorted thanks

I am trying to install this on Buster OS for Pi, runnning on a Pi3B+.
I found that there is no package libcv-dev in Buster although libopencv-dev and python-opencv are available and installed.( It says that libopencv-dev replaces it). I tried the changes listed above, but I still get the same error.
Any suggestions?

piborg's picture

The libcv-dev package is not required anymore, it can be removed from the install.

The command should be:

sudo apt-get -y install libopencv-dev python-opencv

I will get the instructions updated once I am back in the office :)

Ok on libcv-dev not required. I still have the same problem indicated in the thread even though I have for the three mods you suggest.

piborg's picture

What is the actual error message you are seeing?

Please include all of the lines shown like the original poster.

I get exactly the same error as the original poster in this thread. ie in line 91
I followed your advice there and altered the lower case cv to uppercase CV but the eror persists.
I am using the latest Buster OS with sudo apt update and sudo apt upgrade applied.
I have installed the two extra packages specified in your answer libopencv-dev python-opencv
Running on a Pi 3B+ The Web control program works fine.

piborg's picture

The change was to remove cv., not make it upper case. The lines should be three characters shorter.

For comparison:

Settings.capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, Settings.imageWidth);
Settings.capture.set(cv2.CV_CAP_PROP_FRAME_WIDTH, Settings.imageWidth);

Oops. My mistake....however I have removed the cv and I still get the same error.

Traceback (most recent call last):
  File "./MonsterAuto.py", line 91, in 
    Settings.capture.set(cv2.CV_CAP_PROP_FRAME_WIDTH, Settings.cameraWidth);
AttributeError: 'module' object has no attribute 'CV_CAP_PROP_FRAME_WIDTH'

For additional info the exact version of the packages installed by Buster are
libopencv-dev-3.2.0+dfsg-6
python-opencv-3.2.0+dfsg-6

piborg's picture

If you run Python:

python

then use these lines to determine the exact version of OpenCV that Python is actually finding

import cv2
print(cv2.__version__)

It is probably the same as the packages, but it is possible that it is different.

piborg's picture

It looks like you might need to remove the CV_ part of the names as well:

Settings.capture.set(cv2.CAP_PROP_FRAME_WIDTH, Settings.imageWidth);
Settings.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, Settings.imageHeight);
Settings.capture.set(cv2.CAP_PROP_FPS, Settings.frameRate);

checked and the cv2 version is 3.2.0
removing the CV_ for lines 19-21 solves that error. Now gives an error in ImageProcessor.py
for lineType = cv2.CV_AA no attribute CV_AA
I googled this and found https://bugsinmycodes.blogspot.com/2016/11/opencv3-no-attribute-name-cvaa.html which suggests changing it to LINE_AA which I did in the three lines with this attribute. That cured that error. Now it stopped with an exception.
I should say that I am trying it out disconnected to the thunderBorg at the moment, although connected to the camera, but I shall connect it all up and retry and let you now if that cures it.

In the meantime you may wish to upgrade the documentation for other users trying out the code on Buster.

pi@PiBorg:~/monsterborg/monster-self-drive $ ./MonsterAuto.py 
Libraries loaded
Running script in directory "."
TEST MODE: Skipping board setup
Setup camera input
Setup stream processor threads
Processor thread 1 started with idle time of 0.27s
Processor thread 2 started with idle time of 0.27s
Processor thread 3 started with idle time of 0.27s
Processor thread 4 started with idle time of 0.27s
Setup control loop
Control loop thread started with idle time of 0.07s
Wait ...
Press CTRL+C to quit

(, ValueError('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()',), )

Unexpected error, shutting down!
Streaming terminated.
Processor thread 4 terminated
Processor thread 3 terminated
Processor thread 2 terminated
Processor thread 1 terminated
Control loop thread terminated
Program terminated.
piborg's picture

The latest error is due to a newer version of the numpy library.

You can fix it by changing the line

if monsterView != None:

to

if monsterView is not None:

Thanks. That final change has got it all working.
So to get it working under latest Buster OS:
A) change lines 91-93 in MonsterAuto.py to:
Settings.capture.set(cv2.CAP_PROP_FRAME_WIDTH, Settings.imageWidth);
Settings.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, Settings.imageHeight);
Settings.capture.set(cv2.CAP_PROP_FPS, Settings.frameRate);

B) change three references to LineType = cv2.CV_AA to LineType = cv2.LINE_AA

C in MonsterAuto.py change line if monsterView !=None: to
if monsterView is not None:

With these three changes the program works!

Subscribe to Comments for "MonsterAuto.py OpenCV Issue"