Python samples adapted for Python3 compatibility

Common fixes:
- print function
- int / float division
- map, zip iterators in py3 but lists in py2

Known bugs with opencv 3.0.0
- digits.py, called via digits_video.py: https://github.com/Itseez/opencv/issues/4969
- gaussian_mix.py: https://github.com/Itseez/opencv/pull/4232
- video_v4l2.py: https://github.com/Itseez/opencv/pull/5474

Not working:
- letter_recog.py due to changed ml_StatModel.train() signature
This commit is contained in:
flp
2015-12-13 02:43:58 +01:00
parent 5cdf0e3e89
commit 4ed2d6328b
23 changed files with 218 additions and 131 deletions

View File

@@ -14,6 +14,9 @@ USAGE
Press left mouse button on a feature point to see its matching point.
'''
# Python 2/3 compatibility
from __future__ import print_function
import numpy as np
import cv2
from common import anorm, getsize
@@ -82,8 +85,10 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
if status is None:
status = np.ones(len(kp_pairs), np.bool_)
p1 = np.int32([kpp[0].pt for kpp in kp_pairs])
p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) + (w1, 0)
p1, p2 = [], [] # python 2 / python 3 change of zip unpacking
for kpp in kp_pairs:
p1.append(np.int32(kpp[0].pt))
p2.append(np.int32(np.array(kpp[1].pt) + [w1, 0]))
green = (0, 255, 0)
red = (0, 0, 255)
@@ -133,7 +138,7 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
if __name__ == '__main__':
print __doc__
print(__doc__)
import sys, getopt
opts, args = getopt.getopt(sys.argv[1:], '', ['feature='])
@@ -150,33 +155,33 @@ if __name__ == '__main__':
detector, matcher = init_feature(feature_name)
if img1 is None:
print 'Failed to load fn1:', fn1
print('Failed to load fn1:', fn1)
sys.exit(1)
if img2 is None:
print 'Failed to load fn2:', fn2
print('Failed to load fn2:', fn2)
sys.exit(1)
if detector is None:
print 'unknown feature:', feature_name
print('unknown feature:', feature_name)
sys.exit(1)
print 'using', feature_name
print('using', feature_name)
kp1, desc1 = detector.detectAndCompute(img1, None)
kp2, desc2 = detector.detectAndCompute(img2, None)
print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))
print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2)))
def match_and_draw(win):
print 'matching...'
print('matching...')
raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2
p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
if len(p1) >= 4:
H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0)
print '%d / %d inliers/matched' % (np.sum(status), len(status))
print('%d / %d inliers/matched' % (np.sum(status), len(status)))
else:
H, status = None, None
print '%d matches found, not enough for homography estimation' % len(p1)
print('%d matches found, not enough for homography estimation' % len(p1))
vis = explore_match(win, img1, img2, kp_pairs, status, H)