mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
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:
+16
-12
@@ -23,6 +23,10 @@ Usage:
|
||||
digits.py
|
||||
'''
|
||||
|
||||
|
||||
# Python 2/3 compatibility
|
||||
from __future__ import print_function
|
||||
|
||||
# built-in modules
|
||||
from multiprocessing.pool import ThreadPool
|
||||
|
||||
@@ -50,7 +54,7 @@ def split2d(img, cell_size, flatten=True):
|
||||
return cells
|
||||
|
||||
def load_digits(fn):
|
||||
print 'loading "%s" ...' % fn
|
||||
print('loading "%s" ...' % fn)
|
||||
digits_img = cv2.imread(fn, 0)
|
||||
digits = split2d(digits_img, (SZ, SZ))
|
||||
labels = np.repeat(np.arange(CLASS_N), len(digits)/CLASS_N)
|
||||
@@ -67,7 +71,7 @@ def deskew(img):
|
||||
|
||||
class StatModel(object):
|
||||
def load(self, fn):
|
||||
self.model.load(fn)
|
||||
self.model.load(fn) # Known bug: https://github.com/Itseez/opencv/issues/4969
|
||||
def save(self, fn):
|
||||
self.model.save(fn)
|
||||
|
||||
@@ -101,14 +105,14 @@ class SVM(StatModel):
|
||||
def evaluate_model(model, digits, samples, labels):
|
||||
resp = model.predict(samples)
|
||||
err = (labels != resp).mean()
|
||||
print 'error: %.2f %%' % (err*100)
|
||||
print('error: %.2f %%' % (err*100))
|
||||
|
||||
confusion = np.zeros((10, 10), np.int32)
|
||||
for i, j in zip(labels, resp):
|
||||
confusion[i, j] += 1
|
||||
print 'confusion matrix:'
|
||||
print confusion
|
||||
print
|
||||
print('confusion matrix:')
|
||||
print(confusion)
|
||||
print()
|
||||
|
||||
vis = []
|
||||
for img, flag in zip(digits, resp == labels):
|
||||
@@ -145,17 +149,17 @@ def preprocess_hog(digits):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print __doc__
|
||||
print(__doc__)
|
||||
|
||||
digits, labels = load_digits(DIGITS_FN)
|
||||
|
||||
print 'preprocessing...'
|
||||
print('preprocessing...')
|
||||
# shuffle digits
|
||||
rand = np.random.RandomState(321)
|
||||
shuffle = rand.permutation(len(digits))
|
||||
digits, labels = digits[shuffle], labels[shuffle]
|
||||
|
||||
digits2 = map(deskew, digits)
|
||||
digits2 = list(map(deskew, digits))
|
||||
samples = preprocess_hog(digits2)
|
||||
|
||||
train_n = int(0.9*len(samples))
|
||||
@@ -165,18 +169,18 @@ if __name__ == '__main__':
|
||||
labels_train, labels_test = np.split(labels, [train_n])
|
||||
|
||||
|
||||
print 'training KNearest...'
|
||||
print('training KNearest...')
|
||||
model = KNearest(k=4)
|
||||
model.train(samples_train, labels_train)
|
||||
vis = evaluate_model(model, digits_test, samples_test, labels_test)
|
||||
cv2.imshow('KNearest test', vis)
|
||||
|
||||
print 'training SVM...'
|
||||
print('training SVM...')
|
||||
model = SVM(C=2.67, gamma=5.383)
|
||||
model.train(samples_train, labels_train)
|
||||
vis = evaluate_model(model, digits_test, samples_test, labels_test)
|
||||
cv2.imshow('SVM test', vis)
|
||||
print 'saving SVM as "digits_svm.dat"...'
|
||||
print('saving SVM as "digits_svm.dat"...')
|
||||
model.save('digits_svm.dat')
|
||||
|
||||
cv2.waitKey(0)
|
||||
|
||||
Reference in New Issue
Block a user