yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Raspbot V2📖
1.About Raspbot V2
  • 1. First trial
  • 2. Expansion board interface distribution
  • 3. About battery and charging
  • 4. Install servo PTZ
  • 5.Servo Gimbal Installation
  • 6. Important Notes (Read Before Starting)
2.Assembly
  • Installation Video
3.Car basic course
  • 0. Preparation before development
  • 1.Buzzer control
  • 2.Key reading
  • 3.RGB control
  • 4.Servo control (PTZ control)
  • 5.Motor control
  • 6.Printing ultrasonic data
  • 7.Tracking module status printing
  • 8.IR control key value printing
4.Car motion control course
  • 0.Mecanum wheel motion principle
  • 1.Car vertical movement
  • 2.Car diagonal movement
  • 3.Car sideways movement
  • 4.Car rotation movement
  • 5.Car patrol
5.Car advanced courses
  • 1.Infrared patrol car
  • 2.Ultrasonic avoid
  • 3.Ultrasonic follow
  • 4.Infrared tracking+ ultrasonic avoid
  • 5.Restricting
  • 6.Infrared remote control car
6.OpenCV courses
A. Getting Started with OpenCV
  • 1. Introduction to OpenCV
  • 2.OpenCV image reading and display
  • 3. OpenCV image writing
  • 4. OpenCV Image Quality
  • 5.OpenCV pixel operations
B. OpenCV geometric transformation
  • 1. Image scaling
  • 2. Image cutting
  • 3. Image translation
  • 4. Image mirroring
  • 5. Affine Transformation
  • 6. Image Rotation
  • 7. Perspective Transformation
C. OpenCV image processing and text line drawing
  • 1. Grayscale processing
  • 2. Image Binarization
  • 3. Edge Detection
  • 4. Line drawing
  • 5. Rectangle and circle drawing
  • 6. Text and image drawing
D. Image beautification
  • 1. Color Image Histogram
  • 2. Histogram Equalization
  • 3. Image repair
  • 4. Brightness enhancement
  • 5. Smooth and whiten
  • 6. Gaussian mean filtering
  • 7. Median Filter
E. Machine Learning
  • 1. KNN
  • 2. SVM
7.AI vision recognition course
  • 1.Camera driver tutorial
  • 2.Color recognition
  • 3.HSV value test
  • 4.Object Recognition
  • 5.QR code recognition
  • 6.Face recognition
  • 7.License Plate Recognition
  • 8. Human Pose Estimation
  • 9. Target detection
  • 10.yolov5-lite
  • 11. Gesture Recognition
  • 12. Face Detection
  • 13. Face effects
  • 14. 3D Object Recognition
  • 15. Paintbrush
  • 16. Finger control
8.AI vision interaction module
  • 1. Color tracking
  • 2. Color follows
  • 3. QR code recognition to control movement
  • 4. Face tracking
  • 5. Face following
  • 6. Visual tracking
  • 7. Garbage identification
  • 8. Track map Autopilot(purchased separately)
  • 9. Gesture following
9.Voice control basic course(For Superior Kit)new
  • 1.Voice control car movement
  • 2.Voice control RGB light
  • 3.Voice control color recognition
  • 4.Voice control autonomous driving
  • 5.Voice control car tracking
10.AI large model knowledge(For Superior Kit)new
  • 1.AI large model types and principles
  • 2.RAG retrieval enhancement and model training samples
  • 3.Embodied intelligent robot system architecture
  • 4.Embodied intelligent functions core source code
  • 5.Configure AI large model(Required)
11.AI Large Model Development(For Superior Kit & Reference)new
  • 1.Register a model service provider account
  • 2. Configuring API-KEY(Optional)
  • 3. Introduction to Dify
  • 4. Basic Dify Features
  • 5. Deploy the RAG knowledge base
  • 6. AI Large Model Development - chatbot
  • 7. RAG knowledge base + chatbot
  • 8. AI agent workflow
  • 9. Custom wake-up response
  • 10. Core module testing tools
12.AI large basic course(For Superior Kit)new
  • 1.Free conversation
  • 2.Text-to-picture
  • 3.Scene description
  • 4.Video description
  • 5.Intelligent action choreography
  • 6.Dual-model agent application
13.ROS2-Robot chassis control course
  • 1. Robot information release
  • 2. Robot keyboard control
  • 3. URDF Model
14.ROS2-OpenCV course
  • 1.ROS+Opencv application
  • 2. AR Vision
  • 3. Mediapipe fun gameplay
  • 4. Gesture control car movement
  • 5. Gesture control car actions
  • 6. Palm control car movement
  • 7. Palm control servo PTZ
  • 8. Visual tracking
  • 9. AprilTag code recognition
  • 10. AprilTag tag code tracking
  • 11. AprilTag tag code follow
15.Development environment course
  • 1. Introduction to Raspberry Pi 5
  • 2. Image writing and backup
  • 3. Start the Raspberry Pi 5
  • 4. Remote Access
  • 5. Remote file transfer
  • 6. Common Linux commands and use of vim editor
  • 7. Install Raspbot V2 driver library
  • 8. Jupyter Lab environment construction
  • 9. Jupyter Lab plugin installation
  • 10. Basic use of Jupyter Widgets
16.Raspberry Pi basic settings
  • 1.Update and upgrade the operating system
  • 2. Introduction to raspi-config tool
  • 3. config.txt file description
  • 4. Network Configuration
  • 5. Set display resolution and rotation
  • 6. Set screen sleep
  • 7. Get real-time temperature of RPi
  • 8. Play audio and video
  • 9. Use USB Camera
  • 10.Use MIPI camera
17.Docker course
  • 1. Overview and installation
  • 2. Common commands
  • 3. Understand and publish images
  • 4. Docker data processing
  • 5. Enter docker container
18.ROS2 basic course
  • 1. Introduction to ROS2
  • 2.ROS2 install Humble
  • 3. ROS2 development environment
  • 4. ROS2 workspace
  • 5. ROS2 function package
  • 6. ROS2 node
  • 7.ROS2 topic communication
  • 8. ROS2 service communication
  • 9. ROS2 action communication
  • 10. ROS2 custom interface message
  • 11. ROS2 parameter service case
  • 12. ROS2 meta-function package
  • 13. ROS2 distributed communication
  • 14. ROS2 DDS
  • 15. ROS2 time related API
  • 16. ROS2 common command tools
  • 17. ROS2 rviz2 use
  • 18. ROS2 rqt toolbox
  • 19. ROS2 Launch startup file configuration
  • 20. ROS2 recording and playback tool
  • 21. ROS2 URDF model
  • 22. ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
Download
  • APP(iOS/Android)
  • Code
  • System_File
  • Instruction Manual
  • Hardware_Info
  • 3D_Model_File
  • ROS2_Basic_VM
Download ZIP

Welcome to Raspbot V2 repository

当製品の日本語版マニュアル資料は、こちらからご取得ください。

2. SVM

2.SVM

2.SVM2.1. Introduction to SVM2.2. General process of SVM:2.3 Comparison between KNN and SVM

2.1. Introduction to SVM

Support Vector Machine (SVM) is a generalized linear classifier that performs binary classification of data in a supervised learning manner. Its basic model is a linear classifier with the largest margin defined in the feature space. The largest margin distinguishes it from the perceptron. SVM also includes the kernel technique, which makes it a nonlinear classifier in essence. The learning strategy of SVM is to maximize the margin, which can be formalized as a problem of solving convex quadratic programming, which is also equivalent to the problem of minimizing the regularized hinge loss function. The learning algorithm of SVM is the optimization algorithm for solving convex quadratic programming.

Advantages: Low generalization error rate, low computational overhead, and easy to interpret results

Disadvantages: Sensitive to parameter adjustment and kernel function selection, the original classifier is only suitable for processing two-category problems without modification.

Applicable data types: numerical and nominal data

2.2. General process of SVM:
  1. Collect data: provide text files
  2. Prepare data: construct vectors based on binary images
  3. Analyze data: visually inspect images
  4. Training algorithm: use two different kernel functions and use different settings for radial basis kernel functions to run SMO algorithm
  5. Test algorithm: write a function to test different kernel functions and calculate error rate
  6. Use algorithm: SVM can be used for almost all classification problems. SVM itself is a two-class classifier. Applying SVM to multi-class problems requires some code modifications
2.3 Comparison between KNN and SVM

Similarities: Both are relatively classic machine learning classification algorithms, both belong to supervised learning algorithms, and both have important theoretical basis for the selection of machine learning algorithms.

Difference:

  1. KNN considers every sample. SVM is to find a function to make the samples separable.
  2. Naive KNN will not self-learn feature weights, and the essence of SVM is to find weights.
  3. KNN cannot handle things with too high sample dimensions, and SVM is better at handling high-dimensional data.
  4. KNN has high computational complexity, but the parameters that need to be adjusted are relatively small. SVM requires a training process and has high prediction efficiency.

How to choose between the two?

1. Scenarios for choosing KNN:

a. Accuracy does not need to be refined.

b. There are not many samples.

c. Samples cannot be obtained at one time. Intelligence is obtained one by one over time.

2. Scenarios for choosing SVM:

a. The accuracy needs to be improved.

b. There are many samples.

c. The samples are fixed and will not change over time.

Note: The following code has been run in the factory image and the corresponding files have been generated. If you need to run it again, you can delete the two directories MSIST_data/test and train in this directory and execute the following code.

Preprocess MNIST data and convert it into png images

Source code path:

/home/pi/project_demo/06.Open_source_cv_fundamentals_course/E.Machine_Learning/03_SVM_Digit_Recog/03_1.MNIST_Preproc.ipynb

​x
from PIL import Image
import os
import sys
import numpy as np
import time
from sklearn import svm
import joblib
 
# 获取指定路径下的所有 .png 文件 Get all .png files in the specified path
def get_file_list(path):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".png")]
 
# 解析出 .png 图片文件的名称 Parse the name of the .png image file
def get_img_name_str(imgPath):
    return imgPath.split(os.path.sep)[-1]
 
  
# 将 28px * 28px 的图像数据转换成 1*784 的 numpy 向量 Convert 28px * 28px image data into 1*784 numpy vector
# 参数:imgFile--图像名  如:1.png Parameter: imgFile--image name, such as: 1.png
# 返回:1*784 的 numpy 向量 Returns: 1*784 numpy vector
def img2vector(imgFile):
    # print("in img2vector func--para:{}".format(imgFile))
    img = Image.open(imgFile).convert('L')
    img_arr = np.array(img, 'i')  # 28px * 28px 灰度图像 28px * 28px grayscale image
    img_normalization = np.round(img_arr / 255)  # 对灰度值进行归一化 Normalize the grayscale value
    img_arr2 = np.reshape(img_normalization, (1, -1))  # 1 * 784 矩阵 1 * 784 matrix
    return img_arr2
 
 
# 读取一个类别的所有数据并转换成矩阵 Read all the data of a category and convert it into a matrix
# 参数:parameter:
#    basePath: 图像数据所在的基本路径 basePath: The base path where the image data is located
#       MNIST-data/train/
#       MNIST-data/test/
#    cla:类别名称 cla: Category name
#       0,1,2,...,9
# 返回:某一类别的所有数据----[样本数量*(图像宽x图像高)] 矩阵 Returns: All data of a certain category----[sample number*(image width x image height)] matrix
def read_and_convert(imgFileList):
    dataLabel = []  # 存放类标签 Storage label
    dataNum = len(imgFileList)
    dataMat = np.zeros((dataNum, 784))  # dataNum * 784 的矩阵 Matrix of dataNum * 784
    for i in range(dataNum):
        imgNameStr = imgFileList[i]
        imgName = get_img_name_str(imgNameStr)  # 得到 当前数字的数字编号.png Get the digital number of the current number.png
        # print("imgName: {}".format(imgName))
        classTag = imgNameStr.split(os.path.sep)[-2]
        # classTag = imgName.split(".")[0].split("_")[0]  # 得到 类标签(数字) Get class label (number)
        #print(classTag)
        #print(imgNameStr)
        dataLabel.append(classTag)
        dataMat[i, :] = img2vector(imgNameStr)
    return dataMat, dataLabel
 
 
# 读取训练数据 Reading training data
def read_all_data():
    cName = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    #path = sys.path[1]
    train_data_path = 'MNIST_data/train/0' # os.path.join(path, './MNIST_data/train/0')
    print(train_data_path)
    #train_data_path = "./MNIST_data/train/0"
    print('0')
    flist = get_file_list(train_data_path)
    #print(flist)
    dataMat, dataLabel = read_and_convert(flist)
    for c in cName:
        print(c)
        #train_data_path = os.path.join(path, './MNIST_data/train/') + c
        train_data_path = 'MNIST_data/train/' + c
        flist_ = get_file_list(train_data_path)
        dataMat_, dataLabel_ = read_and_convert(flist_)
        dataMat = np.concatenate((dataMat, dataMat_), axis=0)
        dataLabel = np.concatenate((dataLabel, dataLabel_), axis=0)
    # print(dataMat.shape)
    # print(len(dataLabel))
    return dataMat, dataLabel
 
'''
SVC参数
svm.SVC(C=1.0,kernel='rbf',degree=3,gamma='auto',coef0=0.0,shrinking=True,probability=False,
tol=0.001,cache_size=200,class_weight=None,verbose=False,max_iter=-1,decision_function_shape='ovr',random_state=None)

C:C-SVC的惩罚参数C?默认值是1.0
C越大,相当于惩罚松弛变量,希望松弛变量接近0,即对误分类的惩罚增大,趋向于对训练集全分对的情况,这样对训练集测试时
准确率很高,但泛化能力弱。C值小,对误分类的惩罚减小,允许容错,将他们当成噪声点,泛化能力较强。

kernel :核函数,默认是rbf,可以是‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ 
       0 – 线性:u'v
    1 – 多项式:(gamma*u'*v + coef0)^degree
    2 – RBF函数:exp(-gamma|u-v|^2)
    3 –sigmoid:tanh(gamma*u'*v + coef0)

degree :多项式poly函数的维度,默认是3,选择其他核函数时会被忽略。(没用)

gamma : ‘rbf’,‘poly’ 和‘sigmoid’的核函数参数。默认是’auto’,则会选择1/n_features

coef0 :核函数的常数项。对于‘poly’和 ‘sigmoid’有用。(没用)

probability :是否采用概率估计?.默认为False

shrinking :是否采用shrinking heuristic方法,默认为true

tol :停止训练的误差值大小,默认为1e-3

cache_size :核函数cache缓存大小,默认为200

class_weight :类别的权重,字典形式传递。设置第几类的参数C为weight*C(C-SVC中的C)

verbose :允许冗余输出?

max_iter :最大迭代次数。-1为无限制。

decision_function_shape :‘ovo’, ‘ovr’ or None, default=None3(选用ovr,一对多)

random_state :数据洗牌时的种子值,int值

主要调节的参数有:C、kernel、degree、gamma、coef0

SVC parameters
svm.SVC(C=1.0,kernel='rbf',degree=3,gamma='auto',coef0=0.0,shrinking=True,probability=False,
tol=0.001,cache_size=200,class_weight=None,verbose=False,max_iter=-1,decision_function_shape='ovr',random_state=None)

C: C-SVC penalty parameter C? The default value is 1.0

The larger the C value, the more it is equivalent to penalizing the slack variable. We hope that the slack variable is close to 0, that is, the penalty for misclassification increases, tending to the situation where all training sets are classified correctly. In this way, when testing the training set, the accuracy is very high, but the generalization ability is weak. The smaller the C value, the smaller the penalty for misclassification, allowing fault tolerance, treating them as noise points, and the generalization ability is stronger.

kernel :kernel function, default is rbf, can be ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’
0 – linear: u'v
1 – polynomial: (gamma*u'*v + coef0)^degree
2 – RBF function: exp(-gamma|u-v|^2)
3 –sigmoid: tanh(gamma*u'*v + coef0)

degree :dimension of polynomial poly function, default is 3, ignored when other kernel functions are selected. (Not used)

gamma : kernel function parameter for ‘rbf’, ‘poly’ and ‘sigmoid’. Default is ‘auto’, which selects 1/n_features

coef0 :constant term of kernel function. Useful for ‘poly’ and ‘sigmoid’. (Not used)

probability :Whether to use probability estimation? .Default is False

shrinking: Whether to use the shrinking heuristic method, default is true

tol: The error value for stopping training, default is 1e-3

cache_size: The kernel function cache size, default is 200

class_weight: The weight of the category, passed in dictionary form. Set the parameter C of the category to weight*C (C in C-SVC)

verbose: Allow redundant output?

max_iter: Maximum number of iterations. -1 means unlimited.

decision_function_shape: ‘ovo’, ‘ovr’ or None, default=None3 (select ovr, one-to-many)

random_state: Seed value when shuffling data, int value

The main parameters to adjust are: C, kernel, degree, gamma, coef0
''' 
# 创建模型 Creating the Model
def create_svm(dataMat, dataLabel,path,decision='ovr'):
    clf = svm.SVC(C=1.0,kernel='rbf',decision_function_shape=decision)
    rf =clf.fit(dataMat, dataLabel)
    joblib.dump(rf, path)
    return clf

 
if __name__ == '__main__':
    # clf = svm.SVC(decision_function_shape='ovr')
    st = time.process_time()
    dataMat, dataLabel = read_all_data()
    #path = sys.path[1]
    #model_path=os.path.join(path,'model\\svm.model')
    model_path = 'model/svm.model'
    create_svm(dataMat, dataLabel, model_path, decision='ovr')
    et = time.process_time()
    print("Training spent {:.4f}s.".format((et - st)))


Training

This training process takes a long time, about four hours. Please wait patiently until the time spent is printed. During this period, the time scale is printed as a negative value due to exceeding the maximum value of the variable. No need to worry about the warning

Source code path:

/home/pi/project_demo/06.Open_source_cv_fundamentals_course/E.Machine_Learning/03_SVM_Digit_Recog/03_2.SVM_Digits.ipynb

xxxxxxxxxx
from PIL import Image
import os
import sys
import numpy as np
import time
from sklearn import svm
import joblib
 
# 获取指定路径下的所有 .png 文件 Get all .png files in the specified path
def get_file_list(path):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".png")]
 
# 解析出 .png 图片文件的名称 Parse the name of the .png image file
def get_img_name_str(imgPath):
    return imgPath.split(os.path.sep)[-1]
 
  
# 将 28px * 28px 的图像数据转换成 1*784 的 numpy 向量 Convert 28px * 28px image data into 1*784 numpy vector
# 参数:imgFile--图像名  如:1.png Parameter: imgFile--image name, such as: 1.png
# 返回:1*784 的 numpy 向量 Returns: 1*784 numpy vector
def img2vector(imgFile):
    # print("in img2vector func--para:{}".format(imgFile))
    img = Image.open(imgFile).convert('L')
    img_arr = np.array(img, 'i')  # 28px * 28px 灰度图像 28px * 28px grayscale image
    img_normalization = np.round(img_arr / 255)  # 对灰度值进行归一化 Normalize the grayscale value
    img_arr2 = np.reshape(img_normalization, (1, -1))  # 1 * 784 矩阵 1 * 784 matrix
    return img_arr2
 
 
# 读取一个类别的所有数据并转换成矩阵 Read all the data of a category and convert it into a matrix
# 参数:parameter:
#    basePath: 图像数据所在的基本路径 basePath: The base path where the image data is located
#       MNIST-data/train/
#       MNIST-data/test/
#    cla:类别名称 cla: Category name
#       0,1,2,...,9
# 返回:某一类别的所有数据----[样本数量*(图像宽x图像高)] 矩阵 Returns: All data of a certain category----[sample number*(image width x image height)] matrix
def read_and_convert(imgFileList):
    dataLabel = []  # 存放类标签 Storage label
    dataNum = len(imgFileList)
    dataMat = np.zeros((dataNum, 784))  # dataNum * 784 的矩阵 Matrix of dataNum * 784
    for i in range(dataNum):
        imgNameStr = imgFileList[i]
        imgName = get_img_name_str(imgNameStr)  # 得到 当前数字的数字编号.png Get the digital number of the current number.png
        # print("imgName: {}".format(imgName))
        classTag = imgNameStr.split(os.path.sep)[-2]
        # classTag = imgName.split(".")[0].split("_")[0]  # 得到 类标签(数字) Get class label (number)
        #print(classTag)
        #print(imgNameStr)
        dataLabel.append(classTag)
        dataMat[i, :] = img2vector(imgNameStr)
    return dataMat, dataLabel
 
 
# 读取训练数据 Reading training data
def read_all_data():
    cName = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
    #path = sys.path[1]
    train_data_path = 'MNIST_data/train/0' # os.path.join(path, './MNIST_data/train/0')
    print(train_data_path)
    #train_data_path = "./MNIST_data/train/0"
    print('0')
    flist = get_file_list(train_data_path)
    #print(flist)
    dataMat, dataLabel = read_and_convert(flist)
    for c in cName:
        print(c)
        #train_data_path = os.path.join(path, './MNIST_data/train/') + c
        train_data_path = 'MNIST_data/train/' + c
        flist_ = get_file_list(train_data_path)
        dataMat_, dataLabel_ = read_and_convert(flist_)
        dataMat = np.concatenate((dataMat, dataMat_), axis=0)
        dataLabel = np.concatenate((dataLabel, dataLabel_), axis=0)
    # print(dataMat.shape)
    # print(len(dataLabel))
    return dataMat, dataLabel
 
'''
SVC参数
svm.SVC(C=1.0,kernel='rbf',degree=3,gamma='auto',coef0=0.0,shrinking=True,probability=False,
tol=0.001,cache_size=200,class_weight=None,verbose=False,max_iter=-1,decision_function_shape='ovr',random_state=None)

C:C-SVC的惩罚参数C?默认值是1.0
C越大,相当于惩罚松弛变量,希望松弛变量接近0,即对误分类的惩罚增大,趋向于对训练集全分对的情况,这样对训练集测试时
准确率很高,但泛化能力弱。C值小,对误分类的惩罚减小,允许容错,将他们当成噪声点,泛化能力较强。

kernel :核函数,默认是rbf,可以是‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ 
       0 – 线性:u'v
    1 – 多项式:(gamma*u'*v + coef0)^degree
    2 – RBF函数:exp(-gamma|u-v|^2)
    3 –sigmoid:tanh(gamma*u'*v + coef0)

degree :多项式poly函数的维度,默认是3,选择其他核函数时会被忽略。(没用)

gamma : ‘rbf’,‘poly’ 和‘sigmoid’的核函数参数。默认是’auto’,则会选择1/n_features

coef0 :核函数的常数项。对于‘poly’和 ‘sigmoid’有用。(没用)

probability :是否采用概率估计?.默认为False

shrinking :是否采用shrinking heuristic方法,默认为true

tol :停止训练的误差值大小,默认为1e-3

cache_size :核函数cache缓存大小,默认为200

class_weight :类别的权重,字典形式传递。设置第几类的参数C为weight*C(C-SVC中的C)

verbose :允许冗余输出?

max_iter :最大迭代次数。-1为无限制。

decision_function_shape :‘ovo’, ‘ovr’ or None, default=None3(选用ovr,一对多)

random_state :数据洗牌时的种子值,int值

主要调节的参数有:C、kernel、degree、gamma、coef0

SVC parameters
svm.SVC(C=1.0,kernel='rbf',degree=3,gamma='auto',coef0=0.0,shrinking=True,probability=False,
tol=0.001,cache_size=200,class_weight=None,verbose=False,max_iter=-1,decision_function_shape='ovr',random_state=None)

C: C-SVC penalty parameter C? The default value is 1.0

The larger the C value, the more it is equivalent to penalizing the slack variable. We hope that the slack variable is close to 0, that is, the penalty for misclassification increases, tending to the situation where all training sets are classified correctly. In this way, when testing the training set, the accuracy is very high, but the generalization ability is weak. The smaller the C value, the smaller the penalty for misclassification, allowing fault tolerance, treating them as noise points, and the generalization ability is stronger.

kernel :kernel function, default is rbf, can be ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’
0 – linear: u'v
1 – polynomial: (gamma*u'*v + coef0)^degree
2 – RBF function: exp(-gamma|u-v|^2)
3 –sigmoid: tanh(gamma*u'*v + coef0)

degree :dimension of polynomial poly function, default is 3, ignored when other kernel functions are selected. (Not used)

gamma : kernel function parameter for ‘rbf’, ‘poly’ and ‘sigmoid’. Default is ‘auto’, which selects 1/n_features

coef0 :constant term of kernel function. Useful for ‘poly’ and ‘sigmoid’. (Not used)

probability :Whether to use probability estimation? .Default is False

shrinking: Whether to use the shrinking heuristic method, default is true

tol: The error value for stopping training, default is 1e-3

cache_size: The kernel function cache size, default is 200

class_weight: The weight of the category, passed in dictionary form. Set the parameter C of the category to weight*C (C in C-SVC)

verbose: Allow redundant output?

max_iter: Maximum number of iterations. -1 means unlimited.

decision_function_shape: ‘ovo’, ‘ovr’ or None, default=None3 (select ovr, one-to-many)

random_state: Seed value when shuffling data, int value

The main parameters to adjust are: C, kernel, degree, gamma, coef0
''' 
# 创建模型 Creating the Model
def create_svm(dataMat, dataLabel,path,decision='ovr'):
    clf = svm.SVC(C=1.0,kernel='rbf',decision_function_shape=decision)
    rf =clf.fit(dataMat, dataLabel)
    joblib.dump(rf, path)
    return clf

 
if __name__ == '__main__':
    # clf = svm.SVC(decision_function_shape='ovr')
    st = time.process_time()
    dataMat, dataLabel = read_all_data()
    #path = sys.path[1]
    #model_path=os.path.join(path,'model\\svm.model')
    model_path = 'model/svm.model'
    create_svm(dataMat, dataLabel, model_path, decision='ovr')
    et = time.process_time()
    print("Training spent {:.4f}s.".format((et - st)))


Test MNIST dataset

Source code path:

/home/pi/project_demo/06.Open_source_cv_fundamentals_course/E.Machine_Learning/03_SVM_Digit_Recog/03_3.SVM_MNIST.ipynb

xxxxxxxxxx
import sys
import time
from PIL import Image
import os
import joblib
import numpy as np
import matplotlib.pyplot as plt
#忽略警告 Ignore warnings
import warnings
warnings.filterwarnings('ignore')

# 获取指定路径下的所有 .png 文件 Get all .png files in the specified path
def get_file_list(path):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".png")]

# 解析出 .png 图片文件的名称 Parse the name of the .png image file
def get_img_name_str(imgPath):
    return imgPath.split(os.path.sep)[-1]
 
# 将 28px * 28px 的图像数据转换成 1*784 的 numpy 向量 Convert 28px * 28px image data into 1*784 numpy vector
# 参数:imgFile--图像名  如:0_1.png Parameter: imgFile--image name, such as: 0_1.png
# 返回:1*784 的 numpy 向量 Returns: 1*784 numpy vector
def img2vector(imgFile):
    # print("in img2vector func--para:{}".format(imgFile))
    img = Image.open(imgFile).convert('L')
    img_arr = np.array(img, 'i')  # 28px * 28px 灰度图像 28px * 28px grayscale image
    img_normalization = np.round(img_arr / 255)  # 对灰度值进行归一化 Normalize the grayscale value
    img_arr2 = np.reshape(img_normalization, (1, -1))  # 1 * 400 矩阵 1 * 400 matrix
    return img_arr2

# 读取一个类别的所有数据并转换成矩阵 Read all the data of a category and convert it into a matrix
# 参数: parameter:
#    basePath: 图像数据所在的基本路径 basePath: The base path where the image data is located
#       MNIST-data/train/
#       MNIST-data/test/
#    cla:类别名称 cla: Category name
#       0,1,2,...,9
# 返回:某一类别的所有数据----[样本数量*(图像宽x图像高)] 矩阵 Returns: All data of a certain category----[sample number*(image width x image height)] matrix
def read_and_convert(imgFileList):
    dataLabel = []  # 存放类标签 Storage label
    dataNum = len(imgFileList)
    dataMat = np.zeros((dataNum, 784))  # dataNum * 784 的矩阵 Matrix of dataNum * 784
    for i in range(dataNum):
        imgNameStr = imgFileList[i]
        imgName = get_img_name_str(imgNameStr)  # 得到 当前数字的数字编号.png Get the digital number of the current number.png
        # print("imgName: {}".format(imgName))
        classTag = imgNameStr.split(os.path.sep)[-2]
        # classTag = imgName.split(".")[0].split("_")[0]  # 得到 类标签(数字) Get class label (number)
        #print(classTag)
        #print(imgNameStr)
        dataLabel.append(classTag)
        dataMat[i, :] = img2vector(imgNameStr)
    return dataMat, dataLabel

def svmtest(model_path):

    tbasePath = "MNIST_data/test/"
    tcName = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    tst = time.perf_counter()
    allErrCount = 0
    allErrorRate = 0.0
    allScore = 0.0
    ErrCount=np.zeros(10,int)
    TrueCount=np.zeros(10,int)
    #加载模型 Loading the model
    clf = joblib.load(model_path)
    for tcn in tcName:
        testPath = tbasePath + tcn
        # print("class " + tcn + " path is: {}.".format(testPath))
        tflist = get_file_list(testPath)
        # tflist
        tdataMat, tdataLabel = read_and_convert(tflist)
        print("test dataMat shape: {0}, test dataLabel len: {1} ".format(tdataMat.shape, len(tdataLabel)))
        # print("test dataLabel: {}".format(len(tdataLabel)))
        pre_st = time.perf_counter()
        preResult = clf.predict(tdataMat)
        pre_et = time.perf_counter()
        print("Recognition  " + tcn + " spent {:.4f}s.".format((pre_et - pre_st)))
        # print("predict result: {}".format(len(preResult)))
        errCount = len([x for x in preResult if x != tcn])
        ErrCount[int(tcn)]=errCount
        TrueCount[int(tcn)]= len(tdataLabel)-errCount
        print("errorCount: {}.".format(errCount))
        allErrCount += errCount
        score_st = time.perf_counter()
        score = clf.score(tdataMat, tdataLabel)
        score_et = time.perf_counter()
        print("computing score spent {:.6f}s.".format(score_et - score_st))
        allScore += score
        print("score: {:.6f}.".format(score))
        print("error rate is {:.6f}.".format((1 - score)))
 
    #tet = perf_counter()
    tet = time.process_time()
    print("Testing All class total spent {:.6f}s.".format(tet - tst))
    print("All error Count is: {}.".format(allErrCount))
    avgAccuracy = allScore / 10.0
    print("Average accuracy is: {:.6f}.".format(avgAccuracy))
    print("Average error rate is: {:.6f}.".format(1 - avgAccuracy))
    print("number"," TrueCount"," ErrCount")
    for tcn in tcName:
        tcn=int(tcn)
        print(tcn,"     ",TrueCount[tcn],"      ",ErrCount[tcn])
    plt.figure(figsize=(12, 6))
    x=list(range(10))
    plt.plot(x,TrueCount, color='green', label="TrueCount")  # 将正确的数量设置为绿色 Set the correct amount to green
    plt.plot(x,ErrCount, color='red', label="ErrCount")      # 将错误的数量为红色  The wrong number is colored red
    plt.legend(loc='best')  # 显示图例的位置,这里为右下方 Displays the location of the legend, here is the lower right
    plt.title('Projects')
    plt.xlabel('number')    # x轴标签 x-axis label
    plt.ylabel('count')     # y轴标签 y-axis label
    plt.xticks(np.arange(10), ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
    plt.show()
 
 
if __name__ == '__main__':
    model_path='model/svm.model'
    svmtest(model_path)

Recognize your own handwritten numbers

Source code path:

/home/pi/project_demo/06.Open_source_cv_fundamentals_course/E.Machine_Learning/03_SVM_Digit_Recog/03_4.SVM_Personal.ipynb

xxxxxxxxxx
from PIL import Image
import sys
import time
import os
import joblib
import numpy as np
import matplotlib.pyplot as plt

# 获取指定路径下的所有 .png 文件 Get all .png files in the specified path
def get_file_list(path):
    return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".png")]
 
# 解析出 .png 图片文件的名称 Parse the name of the .png image file
def get_img_name_str(imgPath):
    return imgPath.split(os.path.sep)[-1]
 
 
# 将 28px * 28px 的图像数据转换成 1*784 的 numpy 向量 Convert 28px * 28px image data into 1*784 numpy vector
# 参数:imgFile--图像名  如:0_1.png Parameter: imgFile--image name, such as: 0_1.png
# 返回:1*784 的 numpy 向量 Returns: 1*784 numpy vector
def img2vector(imgFile):
    # print("in img2vector func--para:{}".format(imgFile))
    img = Image.open(imgFile).convert('L')
    img_arr = np.array(img, 'i')  # 28px * 28px 灰度图像 28px * 28px grayscale image
    img_normalization = np.round(img_arr / 255)  # 对灰度值进行归一化 Normalize the grayscale value
    img_arr2 = np.reshape(img_normalization, (1, -1))  # 1 * 400 矩阵 1 * 400 matrix
    return img_arr2
 
 
# 读取一个类别的所有数据并转换成矩阵 Read all the data of a category and convert it into a matrix
# 参数: parameter:
#    basePath: 图像数据所在的基本路径 basePath: The base path where the image data is located
#       MNIST-data/train/
#       MNIST-data/test/
#    cla:类别名称 cla: Category name
#       0,1,2,...,9
# 返回:某一类别的所有数据----[样本数量*(图像宽x图像高)] 矩阵 Returns: All data of a certain category----[sample number*(image width x image height)] matrix
def read_and_convert(imgFileList):
    dataLabel = []  # 存放类标签 Storage label
    dataNum = len(imgFileList)
    dataMat = np.zeros((dataNum, 784))  # dataNum * 784 的矩阵 Matrix of dataNum * 784
    for i in range(dataNum):
        imgNameStr = imgFileList[i]
        imgName = get_img_name_str(imgNameStr)  # 得到 当前数字的数字编号.png Get the digital number of the current number.png
        # print("imgName: {}".format(imgName))
        classTag = imgNameStr.split(os.path.sep)[-2]
        # classTag = imgName.split(".")[0].split("_")[0]  # 得到 类标签(数字) Get class label (number)
        #print(classTag)
        #print(imgNameStr)
        dataLabel.append(classTag)
        dataMat[i, :] = img2vector(imgNameStr)
    return dataMat, dataLabel
 
def svmtest(model_path):
    
    #图片路径 Image Path
    tbasePath = "image/"

    #加载模型 Loading the model
    clf = joblib.load(model_path)
    #获取文件列表 Get a list of files
    tflist = get_file_list(tbasePath)
    # tflist
    tdataMat, tdataLabel = read_and_convert(tflist)
    print("test dataMat shape: {0}, test dataLabel len: {1} ".format(tdataMat.shape, len(tdataLabel)))
    pre_st = time.perf_counter()
    #预测结果 forecast result
    preResult = clf.predict(tdataMat) 
    pre_et = time.perf_counter()
    print("Recognition  1 spent {:.4f}s.".format((pre_et - pre_st)))
    print("predict result: {}".format(len(preResult)))
    score = clf.score(tdataMat, tdataLabel)

 
if __name__ == '__main__':
    model_path='model/svm.model'
    svmtest(model_path)

Original image:

Recognition results:

KNN
Camera driver tutorial

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文