zagor / watermeter

Water meter reader - Convert analog dials into a digital value
GNU General Public License v3.0
20 stars 11 forks source link

"AttributeError: 'module' object has no attribute 'cv'" (Ubuntu 17.10) #1

Closed sanderjo closed 6 years ago

sanderjo commented 6 years ago

Ubuntu 17.10, after sudo apt-get install python-opencv, running watermeter, I get:

sander@Stream-13:~/git/watermeter$ python watermeter.py sample1.jpg 
Traceback (most recent call last):
  File "watermeter.py", line 196, in <module>
    fromleft = findCircles(red)
  File "watermeter.py", line 45, in findCircles
    circles = cv2.HoughCircles(img, cv2.cv.CV_HOUGH_GRADIENT, 1, 50,
AttributeError: 'module' object has no attribute 'cv'

Am I doing something wrong?

On Ubuntu 14.04, it works:

sander@haring:~/git/watermeter$ python watermeter.py sample1.jpg
314.93 liter
zagor commented 6 years ago

Hmm, interesting. As far as I can tell, Ubuntu 17.10 uses the exact same python-opencv package as my Debian install (2.4.9.1+dfsg1-2).

Try adding the following line after "import cv2" and see if that makes any difference: import cv2.cv as cv

sanderjo commented 6 years ago
sander@haring:~/git/watermeter$ uname -a ; python -c "import cv2;  print cv2.__version__ ; print cv2.cv.CV_HOUGH_GRADIENT"

Linux haring 3.13.0-133-generic #182-Ubuntu SMP Tue Sep 19 15:49:23 UTC 2017 i686 i686 i686 GNU/Linux

2.4.8
3

versus

sander@Stream-13:~$ uname -a ; python -c "import cv2;  print cv2.__version__ ; print cv2.cv.CV_HOUGH_GRADIENT"
Linux Stream-13 4.13.0-041300-generic #201709031731 SMP Sun Sep 3 21:33:09 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

3.1.0
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'cv'
zagor commented 6 years ago

Aha, that explains it. I've only tried this with opencv 2.4.9. It seems in v3+ it's just called cv2.HOUGH_GRADIENT.

sanderjo commented 6 years ago

I changed that like this

        circles = cv2.HoughCircles(img, cv2.cv.CV_HOUGH_GRADIENT, 1, 50,
                               param1=20,param2=15,minRadius=20,maxRadius=50)
    except:
        circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 50,
                               param1=20,param2=15,minRadius=20,maxRadius=50)

but now:

sander@Stream-13:~/git/watermeter$ python watermeter.py sample2.jpg 
No tip found!
sander@Stream-13:~/git/watermeter$ python watermeter.py sample3.jpg 
No tip found!
sander@Stream-13:~/git/watermeter$ python watermeter.py sample1.jpg 
No tip found!
sander@Stream-13:~/git/watermeter$ python watermeter.py sample4.jpg 
No tip found!

Update:

Some ugly debugging:

                print "SJ: 520. l and l2", l, l2
                r = intersection(l, l2)
                print "SJ: 550. r is", r
                if r and r[0] > 0 and r[1] > 0:
                    print "SJ: 600"

results in

sander@Stream-13:~/git/watermeter$ python watermeter.py sample1.jpg 
SJ: 100
SJ: 200
SJ: 300
SJ: 400
SJ: 520. l and l2 (-67, 56, -2812) (-67, 56, -2812)
SJ: 550. r is False
No tip found!

Only one loop, with r False. That is bad, isn't it?

Same debug code on Ubuntu 14.04 / opencv 2.x:

SJ: 100
SJ: 200
SJ: 300
SJ: 400
SJ: 520. l and l2 (-64, 53, -2681) (-64, 53, -2681)
SJ: 550. r is False
SJ: 400
SJ: 520. l and l2 (-64, 53, -2681) (-78, 5, -3767)
SJ: 550. r is (48, 8)
SJ: 600
SJ: 400
SJ: 520. l and l2 (-64, 53, -2681) (-73, 5, -3759)
SJ: 550. r is (52, 12)
SJ: 600
SJ: 400
<snip much more>

Update 2

I un-commented this line:

print "%d lines" % len(lines[0])

with results:

Ubuntu 17.10:

sander@Stream-13:~/git/watermeter2$ python watermeter.py sample1.jpg 
1 lines
SJ: lines[0] is [[ 52  12 108  79]]
No tip found!

Ubuntu 14.04:

sander@haring:~/git/watermeter$ python wat-debug3.py sample1.jpg 
5 lines
SJ: lines[0] is [[ 51  11 104  75]
 [ 49  11  54  89]
 [ 53  22  58  95]
 [ 61  18 107  73]
 [ 49  83  56  16]]
7 lines
SJ: lines[0] is [[ 55  88 113 140]
 [106  64 117 128]
 [ 56  93 113 141]
 [ 50  80 111 143]
 [107  66 115 130]
 [ 57  95 113 142]
 [ 55  97 113 130]]
7 lines
SJ: lines[0] is [[117 116 150  48]
 [ 79  68 151  52]
 [ 75  70 148  59]
 [104 125 151  54]
 [102 126 151  53]
 [ 72  73 139  51]
 [ 68  77 132  54]]
4 lines
SJ: lines[0] is [[ 71 114 143 114]
 [ 75 116 133 116]
 [ 89  57 149 107]
 [ 69 113 144 113]]
314.93 liter
zagor commented 6 years ago

Hmm, it appears that OpenCV v3 has changed more than just the API.

But I also know the code is rather sensitive and does not tolerate much difference in source imagery. It's still a very early version and should be seen more as a base for further work than a finished product right now.

sanderjo commented 6 years ago

Just checking: you closed this on purpose?

zagor commented 6 years ago

Ooops, no. :) Fat finger error.

sanderjo commented 6 years ago

I tried if pip provided an opencv version 2.x, but alas not:

sander@Stream-13:~/git/watermeter$ sudo -H pip install 'opencv-python<3'
Collecting opencv-python<3
  Could not find a version that satisfies the requirement opencv-python<3 (from versions: 3.1.0.0, 3.1.0.1, 3.1.0.2, 3.1.0.3, 3.1.0.4, 3.1.0.5, 3.2.0.6, 3.2.0.7, 3.2.0.8, 3.3.0.9, 3.3.0.10)
No matching distribution found for opencv-python<3
zagor commented 6 years ago

I got the opencv 3 in dist-upgrade now. It seems I treat the return from cv2.HoughLinesP() wrong. Update coming.

zagor commented 6 years ago

Try the latest change.

sanderjo commented 6 years ago

Yes, it now works on my Ubuntu 17.10. Thank you!

sander@Stream-13:~/git/watermeter3$ python watermeter.py sample1.jpg 
314.93 liter
zagor commented 6 years ago

Thanks for helping!