Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
fa1426f1
Commit
fa1426f1
authored
Feb 29, 2016
by
berak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix python ml tutorials
parent
177aef05
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
16 deletions
+18
-16
py_knn_opencv.markdown
...torials/py_ml/py_knn/py_knn_opencv/py_knn_opencv.markdown
+6
-6
py_knn_understanding.markdown
...py_knn/py_knn_understanding/py_knn_understanding.markdown
+4
-4
py_svm_opencv.markdown
...torials/py_ml/py_svm/py_svm_opencv/py_svm_opencv.markdown
+8
-6
No files found.
doc/py_tutorials/py_ml/py_knn/py_knn_opencv/py_knn_opencv.markdown
View file @
fa1426f1
...
@@ -42,9 +42,9 @@ train_labels = np.repeat(k,250)[:,np.newaxis]
...
@@ -42,9 +42,9 @@ train_labels = np.repeat(k,250)[:,np.newaxis]
test_labels = train_labels.copy()
test_labels = train_labels.copy()
# Initiate kNN, train the data, then test it with test data for k=1
# Initiate kNN, train the data, then test it with test data for k=1
knn = cv2.
KNearest
()
knn = cv2.
ml.KNearest_create
()
knn.train(train,train_labels)
knn.train(train,
cv2.ml.ROW_SAMPLE,
train_labels)
ret,result,neighbours,dist = knn.find
_n
earest(test,k=5)
ret,result,neighbours,dist = knn.find
N
earest(test,k=5)
# Now we check the accuracy of classification
# Now we check the accuracy of classification
# For that, compare the result with test_labels and check which are wrong
# For that, compare the result with test_labels and check which are wrong
...
@@ -103,9 +103,9 @@ responses, trainData = np.hsplit(train,[1])
...
@@ -103,9 +103,9 @@ responses, trainData = np.hsplit(train,[1])
labels, testData = np.hsplit(test,
[
1
]
)
labels, testData = np.hsplit(test,
[
1
]
)
# Initiate the kNN, classify, measure accuracy.
# Initiate the kNN, classify, measure accuracy.
knn = cv2.
KNearest
()
knn = cv2.
ml.KNearest_create
()
knn.train(trainData, responses)
knn.train(trainData,
cv2.ml.ROW_SAMPLE,
responses)
ret, result, neighbours, dist = knn.find
_n
earest(testData, k=5)
ret, result, neighbours, dist = knn.find
N
earest(testData, k=5)
correct = np.count_nonzero(result == labels)
correct = np.count_nonzero(result == labels)
accuracy = correct
*
100.0/10000
accuracy = correct
*
100.0/10000
...
...
doc/py_tutorials/py_ml/py_knn/py_knn_understanding/py_knn_understanding.markdown
View file @
fa1426f1
...
@@ -114,9 +114,9 @@ So let's see how it works. New comer is marked in green color.
...
@@ -114,9 +114,9 @@ So let's see how it works. New comer is marked in green color.
newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
plt.scatter(newcomer
[
:,0
]
,newcomer
[
:,1
]
,80,'g','o')
plt.scatter(newcomer
[
:,0
]
,newcomer
[
:,1
]
,80,'g','o')
knn = cv2.
KNearest
()
knn = cv2.
ml.KNearest_create
()
knn.train(trainData,responses)
knn.train(trainData,
cv2.ml.ROW_SAMPLE,
responses)
ret, results, neighbours ,dist = knn.find
_n
earest(newcomer, 3)
ret, results, neighbours ,dist = knn.find
N
earest(newcomer, 3)
print "result: ", results,"
\n
"
print "result: ", results,"
\n
"
print "neighbours: ", neighbours,"
\n
"
print "neighbours: ", neighbours,"
\n
"
...
@@ -140,7 +140,7 @@ obtained as arrays.
...
@@ -140,7 +140,7 @@ obtained as arrays.
@code{.py}
@code{.py}
# 10 new comers
# 10 new comers
newcomers = np.random.randint(0,100,(10,2)).astype(np.float32)
newcomers = np.random.randint(0,100,(10,2)).astype(np.float32)
ret, results,neighbours,dist = knn.find
_n
earest(newcomer, 3)
ret, results,neighbours,dist = knn.find
N
earest(newcomer, 3)
# The results also will contain 10 labels.
# The results also will contain 10 labels.
@endcode
@endcode
Additional Resources
Additional Resources
...
...
doc/py_tutorials/py_ml/py_svm/py_svm_opencv/py_svm_opencv.markdown
View file @
fa1426f1
...
@@ -64,9 +64,6 @@ import numpy as np
...
@@ -64,9 +64,6 @@ import numpy as np
SZ=20
SZ=20
bin_n = 16 # Number of bins
bin_n = 16 # Number of bins
svm_params = dict( kernel_type = cv2.SVM_LINEAR,
svm_type = cv2.SVM_C_SVC,
C=2.67, gamma=5.383 )
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
...
@@ -105,8 +102,13 @@ hogdata = [map(hog,row) for row in deskewed]
...
@@ -105,8 +102,13 @@ hogdata = [map(hog,row) for row in deskewed]
trainData = np.float32(hogdata).reshape(-1,64)
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.float32(np.repeat(np.arange(10),250)
[
:,np.newaxis
]
)
responses = np.float32(np.repeat(np.arange(10),250)
[
:,np.newaxis
]
)
svm = cv2.SVM()
svm = cv2.ml.SVM_create()
svm.train(trainData,responses, params=svm_params)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setType(cv2.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
svm.train(trainData, cv2.ml.ROW_SAMPLE, responses)
svm.save('svm_data.dat')
svm.save('svm_data.dat')
###### Now testing ########################
###### Now testing ########################
...
@@ -114,7 +116,7 @@ svm.save('svm_data.dat')
...
@@ -114,7 +116,7 @@ svm.save('svm_data.dat')
deskewed =
[
map(deskew,row) for row in test_cells
]
deskewed =
[
map(deskew,row) for row in test_cells
]
hogdata =
[
map(hog,row) for row in deskewed
]
hogdata =
[
map(hog,row) for row in deskewed
]
testData = np.float32(hogdata).reshape(-1,bin_n
*
4)
testData = np.float32(hogdata).reshape(-1,bin_n
*
4)
result = svm.predict
_all
(testData)
result = svm.predict(testData)
####### Check Accuracy ########################
####### Check Accuracy ########################
mask = result==responses
mask = result==responses
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment