mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-22 16:08:41 +08:00
Merge pull request #3636 from kaingwade:ml_to_contrib
Move ml to opencv_contrib #3636 Main PR: opencv/opencv#25017
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
# Set up training data
|
||||
## [setup1]
|
||||
labels = np.array([1, -1, -1, -1])
|
||||
trainingData = np.matrix([[501, 10], [255, 10], [501, 255], [10, 501]], dtype=np.float32)
|
||||
## [setup1]
|
||||
|
||||
# Train the SVM
|
||||
## [init]
|
||||
svm = cv.ml.SVM_create()
|
||||
svm.setType(cv.ml.SVM_C_SVC)
|
||||
svm.setKernel(cv.ml.SVM_LINEAR)
|
||||
svm.setTermCriteria((cv.TERM_CRITERIA_MAX_ITER, 100, 1e-6))
|
||||
## [init]
|
||||
## [train]
|
||||
svm.train(trainingData, cv.ml.ROW_SAMPLE, labels)
|
||||
## [train]
|
||||
|
||||
# Data for visual representation
|
||||
width = 512
|
||||
height = 512
|
||||
image = np.zeros((height, width, 3), dtype=np.uint8)
|
||||
|
||||
# Show the decision regions given by the SVM
|
||||
## [show]
|
||||
green = (0,255,0)
|
||||
blue = (255,0,0)
|
||||
for i in range(image.shape[0]):
|
||||
for j in range(image.shape[1]):
|
||||
sampleMat = np.matrix([[j,i]], dtype=np.float32)
|
||||
response = svm.predict(sampleMat)[1]
|
||||
|
||||
if response == 1:
|
||||
image[i,j] = green
|
||||
elif response == -1:
|
||||
image[i,j] = blue
|
||||
## [show]
|
||||
|
||||
# Show the training data
|
||||
## [show_data]
|
||||
thickness = -1
|
||||
cv.circle(image, (501, 10), 5, ( 0, 0, 0), thickness)
|
||||
cv.circle(image, (255, 10), 5, (255, 255, 255), thickness)
|
||||
cv.circle(image, (501, 255), 5, (255, 255, 255), thickness)
|
||||
cv.circle(image, ( 10, 501), 5, (255, 255, 255), thickness)
|
||||
## [show_data]
|
||||
|
||||
# Show support vectors
|
||||
## [show_vectors]
|
||||
thickness = 2
|
||||
sv = svm.getUncompressedSupportVectors()
|
||||
|
||||
for i in range(sv.shape[0]):
|
||||
cv.circle(image, (int(sv[i,0]), int(sv[i,1])), 6, (128, 128, 128), thickness)
|
||||
## [show_vectors]
|
||||
|
||||
cv.imwrite('result.png', image) # save the image
|
||||
|
||||
cv.imshow('SVM Simple Example', image) # show it to the user
|
||||
cv.waitKey()
|
@@ -0,0 +1,117 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import random as rng
|
||||
|
||||
NTRAINING_SAMPLES = 100 # Number of training samples per class
|
||||
FRAC_LINEAR_SEP = 0.9 # Fraction of samples which compose the linear separable part
|
||||
|
||||
# Data for visual representation
|
||||
WIDTH = 512
|
||||
HEIGHT = 512
|
||||
I = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
|
||||
|
||||
# --------------------- 1. Set up training data randomly ---------------------------------------
|
||||
trainData = np.empty((2*NTRAINING_SAMPLES, 2), dtype=np.float32)
|
||||
labels = np.empty((2*NTRAINING_SAMPLES, 1), dtype=np.int32)
|
||||
|
||||
rng.seed(100) # Random value generation class
|
||||
|
||||
# Set up the linearly separable part of the training data
|
||||
nLinearSamples = int(FRAC_LINEAR_SEP * NTRAINING_SAMPLES)
|
||||
|
||||
## [setup1]
|
||||
# Generate random points for the class 1
|
||||
trainClass = trainData[0:nLinearSamples,:]
|
||||
# The x coordinate of the points is in [0, 0.4)
|
||||
c = trainClass[:,0:1]
|
||||
c[:] = np.random.uniform(0.0, 0.4 * WIDTH, c.shape)
|
||||
# The y coordinate of the points is in [0, 1)
|
||||
c = trainClass[:,1:2]
|
||||
c[:] = np.random.uniform(0.0, HEIGHT, c.shape)
|
||||
|
||||
# Generate random points for the class 2
|
||||
trainClass = trainData[2*NTRAINING_SAMPLES-nLinearSamples:2*NTRAINING_SAMPLES,:]
|
||||
# The x coordinate of the points is in [0.6, 1]
|
||||
c = trainClass[:,0:1]
|
||||
c[:] = np.random.uniform(0.6*WIDTH, WIDTH, c.shape)
|
||||
# The y coordinate of the points is in [0, 1)
|
||||
c = trainClass[:,1:2]
|
||||
c[:] = np.random.uniform(0.0, HEIGHT, c.shape)
|
||||
## [setup1]
|
||||
|
||||
#------------------ Set up the non-linearly separable part of the training data ---------------
|
||||
## [setup2]
|
||||
# Generate random points for the classes 1 and 2
|
||||
trainClass = trainData[nLinearSamples:2*NTRAINING_SAMPLES-nLinearSamples,:]
|
||||
# The x coordinate of the points is in [0.4, 0.6)
|
||||
c = trainClass[:,0:1]
|
||||
c[:] = np.random.uniform(0.4*WIDTH, 0.6*WIDTH, c.shape)
|
||||
# The y coordinate of the points is in [0, 1)
|
||||
c = trainClass[:,1:2]
|
||||
c[:] = np.random.uniform(0.0, HEIGHT, c.shape)
|
||||
## [setup2]
|
||||
|
||||
#------------------------- Set up the labels for the classes ---------------------------------
|
||||
labels[0:NTRAINING_SAMPLES,:] = 1 # Class 1
|
||||
labels[NTRAINING_SAMPLES:2*NTRAINING_SAMPLES,:] = 2 # Class 2
|
||||
|
||||
#------------------------ 2. Set up the support vector machines parameters --------------------
|
||||
print('Starting training process')
|
||||
## [init]
|
||||
svm = cv.ml.SVM_create()
|
||||
svm.setType(cv.ml.SVM_C_SVC)
|
||||
svm.setC(0.1)
|
||||
svm.setKernel(cv.ml.SVM_LINEAR)
|
||||
svm.setTermCriteria((cv.TERM_CRITERIA_MAX_ITER, int(1e7), 1e-6))
|
||||
## [init]
|
||||
|
||||
#------------------------ 3. Train the svm ----------------------------------------------------
|
||||
## [train]
|
||||
svm.train(trainData, cv.ml.ROW_SAMPLE, labels)
|
||||
## [train]
|
||||
print('Finished training process')
|
||||
|
||||
#------------------------ 4. Show the decision regions ----------------------------------------
|
||||
## [show]
|
||||
green = (0,100,0)
|
||||
blue = (100,0,0)
|
||||
for i in range(I.shape[0]):
|
||||
for j in range(I.shape[1]):
|
||||
sampleMat = np.matrix([[j,i]], dtype=np.float32)
|
||||
response = svm.predict(sampleMat)[1]
|
||||
|
||||
if response == 1:
|
||||
I[i,j] = green
|
||||
elif response == 2:
|
||||
I[i,j] = blue
|
||||
## [show]
|
||||
|
||||
#----------------------- 5. Show the training data --------------------------------------------
|
||||
## [show_data]
|
||||
thick = -1
|
||||
# Class 1
|
||||
for i in range(NTRAINING_SAMPLES):
|
||||
px = trainData[i,0]
|
||||
py = trainData[i,1]
|
||||
cv.circle(I, (int(px), int(py)), 3, (0, 255, 0), thick)
|
||||
|
||||
# Class 2
|
||||
for i in range(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES):
|
||||
px = trainData[i,0]
|
||||
py = trainData[i,1]
|
||||
cv.circle(I, (int(px), int(py)), 3, (255, 0, 0), thick)
|
||||
## [show_data]
|
||||
|
||||
#------------------------- 6. Show support vectors --------------------------------------------
|
||||
## [show_vectors]
|
||||
thick = 2
|
||||
sv = svm.getUncompressedSupportVectors()
|
||||
|
||||
for i in range(sv.shape[0]):
|
||||
cv.circle(I, (int(sv[i,0]), int(sv[i,1])), 6, (128, 128, 128), thick)
|
||||
## [show_vectors]
|
||||
|
||||
cv.imwrite('result.png', I) # save the Image
|
||||
cv.imshow('SVM for Non-Linear Training Data', I) # show it to the user
|
||||
cv.waitKey()
|
73
modules/ml/samples/python/tutorial_code/ml/py_svm_opencv/hogsvm.py
Executable file
73
modules/ml/samples/python/tutorial_code/ml/py_svm_opencv/hogsvm.py
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
SZ=20
|
||||
bin_n = 16 # Number of bins
|
||||
|
||||
|
||||
affine_flags = cv.WARP_INVERSE_MAP|cv.INTER_LINEAR
|
||||
|
||||
## [deskew]
|
||||
def deskew(img):
|
||||
m = cv.moments(img)
|
||||
if abs(m['mu02']) < 1e-2:
|
||||
return img.copy()
|
||||
skew = m['mu11']/m['mu02']
|
||||
M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
|
||||
img = cv.warpAffine(img,M,(SZ, SZ),flags=affine_flags)
|
||||
return img
|
||||
## [deskew]
|
||||
|
||||
## [hog]
|
||||
def hog(img):
|
||||
gx = cv.Sobel(img, cv.CV_32F, 1, 0)
|
||||
gy = cv.Sobel(img, cv.CV_32F, 0, 1)
|
||||
mag, ang = cv.cartToPolar(gx, gy)
|
||||
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
|
||||
bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
|
||||
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
|
||||
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
|
||||
hist = np.hstack(hists) # hist is a 64 bit vector
|
||||
return hist
|
||||
## [hog]
|
||||
|
||||
img = cv.imread(cv.samples.findFile('digits.png'),0)
|
||||
if img is None:
|
||||
raise Exception("we need the digits.png image from samples/data here !")
|
||||
|
||||
|
||||
cells = [np.hsplit(row,100) for row in np.vsplit(img,50)]
|
||||
|
||||
# First half is trainData, remaining is testData
|
||||
train_cells = [ i[:50] for i in cells ]
|
||||
test_cells = [ i[50:] for i in cells]
|
||||
|
||||
###### Now training ########################
|
||||
|
||||
deskewed = [list(map(deskew,row)) for row in train_cells]
|
||||
hogdata = [list(map(hog,row)) for row in deskewed]
|
||||
trainData = np.float32(hogdata).reshape(-1,64)
|
||||
responses = np.repeat(np.arange(10),250)[:,np.newaxis]
|
||||
|
||||
svm = cv.ml.SVM_create()
|
||||
svm.setKernel(cv.ml.SVM_LINEAR)
|
||||
svm.setType(cv.ml.SVM_C_SVC)
|
||||
svm.setC(2.67)
|
||||
svm.setGamma(5.383)
|
||||
|
||||
svm.train(trainData, cv.ml.ROW_SAMPLE, responses)
|
||||
svm.save('svm_data.dat')
|
||||
|
||||
###### Now testing ########################
|
||||
|
||||
deskewed = [list(map(deskew,row)) for row in test_cells]
|
||||
hogdata = [list(map(hog,row)) for row in deskewed]
|
||||
testData = np.float32(hogdata).reshape(-1,bin_n*4)
|
||||
result = svm.predict(testData)[1]
|
||||
|
||||
####### Check Accuracy ########################
|
||||
mask = result==responses
|
||||
correct = np.count_nonzero(mask)
|
||||
print(correct*100.0/result.size)
|
Reference in New Issue
Block a user