原标题:机器学习:用SVM和局部二值模式进行图像识别
来源:今日头条 链接:https://www.toutiao.com/a6563500474505363971/
在本文中,我将告诉你如何做一个简单的性别预测。这里我用了一些库
Flask
sklearn
matplotlib
Local binary pattern
其他
机器只知道数字,所以我们需要将图像像素转换成数字。使用局部二值模式是一件好事,因为它提供了一个简单的概念来将图像转换成数字,尽管这对进一步的研究没有好处。
LBP将图像分割到一些区域,并计算每个区域的梯度密度,然后处理成直方图
LBP
# import the necessary packages
from skimage import feature
import numpy as np
class LocalBinaryPatterns:
def __init__(self, numPoints, radius):
# store the number of points and radius
self.numPoints = numPoints
self.radius = radius
def describe(self, image, eps=1e-7):
# compute the Local Binary Pattern representation
# of the image, and then use the LBP representation
# to build the histogram of patterns
lbp = feature.local_binary_pattern(image, self.numPoints,
self.radius, method="uniform")
(hist, _) = np.histogram(lbp.ravel(),
bins=np.arange(0, self.numPoints + 3),
range=(0, self.numPoints + 2))
# normalize the histogram
hist = hist.astype("float")
hist /= (hist.sum() + eps)
# return the histogram of Local Binary Patterns
return hist
这是Pyth实现的LBP代码,结果将是描述的直方图,或者我们可以说它是一组数组。
你从上面的代码中得到的结果可以称之为数据集。
Dataset
[0.021636221875666023,0.01754288260189137,0.009927043885038529,0.007963911784350686,0.007880374248151202,0.008311984851848529,0.007031075963456462,0.009189128981943098,0.01198763644462577,0.016122744486500164,0.023543662285554212,0.038496881265261615,0.05056805524608687,0.04409389619062696,0.029669748273516275,0.023641122744453607,0.014465916685210422,0.01357484963241594,0.008311984851848529,0.010581421251934477,0.008854978837145167,0.01077634216973327,0.012377478280223356,0.019659166852278264,0.02316774337265654,0.5506237469361903]
这是我从一个图像中得到的数据集的一个例子,我使用了LBP,这个数据集现在可以用于训练。别忘了给它贴上数字标签。我用1表示男性,0代表女性。
首先,您应该定义标签和数据变量。
E.g i have three datasets
datas = [[dataset],[dataset],[dataset]]
label = [1,0,0]
1= male, 0=female
sklearn将帮助您做一个SVM预测器,只需要几行代码。
model = LinearSVC(C=100.0, random_state=42)
model.fit(datas, label)
一切都设置好了。您的训练代码已经准备好使用,现在您只需要制作测试代码。很简单
testing = [dataset]
prediction = model.predict(testing)[0]
在执行代码之后,您将看到基于数据训练中的标签的结果。
result = print(prediction)
一THE END一
免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。
合作及投稿邮箱:E-mail:editor@tusaishared.com