Python 应用HOG+;基于支持向量机的网络摄像机目标检测训练
我已经通过从正反数据集中提取HOG特征来训练我的SVM分类器Python 应用HOG+;基于支持向量机的网络摄像机目标检测训练,python,opencv,machine-learning,scikit-learn,computer-vision,Python,Opencv,Machine Learning,Scikit Learn,Computer Vision,我已经通过从正反数据集中提取HOG特征来训练我的SVM分类器 from sklearn.svm import SVC import cv2 import numpy as np hog = cv2.HOGDescriptor() def hoggify(x,z): data=[] for i in range(1,int(z)): image = cv2.imread("/Users/munirmalik/cvprojek/cod/"+x+"/"+"fi
from sklearn.svm import SVC
import cv2
import numpy as np
hog = cv2.HOGDescriptor()
def hoggify(x,z):
data=[]
for i in range(1,int(z)):
image = cv2.imread("/Users/munirmalik/cvprojek/cod/"+x+"/"+"file"+str(i)+".jpg", 0)
dim = 128
img = cv2.resize(image, (dim,dim), interpolation = cv2.INTER_AREA)
img = hog.compute(img)
img = np.squeeze(img)
data.append(img)
return data
def svmClassify(features,labels):
clf=SVC(C=10000,kernel="linear",gamma=0.000001)
clf.fit(features,labels)
return clf
def list_to_matrix(lst):
return np.stack(lst)
我希望应用该培训,以便程序能够检测到我的自定义对象(椅子)
我已经为每一组添加了标签;下一步需要做什么 您已经有三件最重要的物品可供您使用
hoggify
创建一个HOG描述符列表-每个图像一个。请注意,用于计算描述符的预期输入是灰度图像,描述符作为一个2D数组返回,该数组有1列,这意味着HOG描述符中的每个元素都有自己的行。但是,您正在使用np.squence
删除singleton列,并将其替换为1D numpy数组,因此我们在这里就可以了。然后使用list\u to_matrix
将列表转换为numpy
数组。完成此操作后,您可以使用svmClassify
最终训练数据。这假设您已经在1Dnumpy
数组中设置了标签。训练SVM后,您将使用给定输入特征的方法,它将分类图像是否属于椅子
因此,您需要执行以下步骤:
使用hoggify
创建您的HOG描述符列表,每个图像一个。看起来输入x
是您将椅子图像称为的前缀,而z
表示要加载的图像总数。请记住,range
不包括结束值,因此您可能希望在int(z)
之后添加一个+1
(即int(z)+1
),以确保包含结束值。我不确定这是不是真的,但我想把它扔出去
x = '...' # Whatever prefix you called your chairs
z = 100 # Load in 100 images for example
lst = hoggify(x, z)
将HOG描述符列表转换为实际矩阵:
data = list_to_matrix(lst)
训练你的SVM分类器。假设您已经将标签存储在labels
中,其中值0
表示非椅子,1
表示椅子,它是1Dnumpy
数组:
labels = ... # Define labels here as a numpy array
clf = svmClassify(data, labels)
使用SVM分类器执行预测。假设您有一个要用分类器测试的测试图像,您将需要执行与训练数据相同的处理步骤。我假设这就是hoggify所做的,您可以指定不同的x
来表示要使用的不同集合。指定一个新变量xtest
以指定此不同的目录或前缀,以及所需的图像数,然后使用hoggify
与list\u to\u matrix
组合使用以获取您的特征:
xtest = '...' # Define new test prefix here
ztest = 50 # 50 test images
lst_test = hoggify(xtest, ztest)
test_data = list_to_matrix(lst_test)
pred = clf.predict(test_data)
pred
将包含一个预测标签数组,每个标签对应一个测试图像。如果您愿意,您可以看到SVM对训练数据的处理效果如何,因此,既然您已经掌握了这些数据,只需从步骤2开始再次使用数据
:
pred_training
将包含一组预测标签,每个标签对应一张训练图像
如果您最终希望将其与网络摄像头一起使用,则过程将是使用对象并指定连接到计算机的设备的ID。通常只有一个网络摄像头连接到您的计算机,因此请使用ID 0。一旦你这样做了,过程将是使用一个循环,抓取一帧,将其转换为灰度,因为猪描述符需要灰度图像,计算描述符,然后对图像进行分类
假设您已经对模型进行了训练,并且之前已经创建了一个HOG描述符对象,那么这样的方法是可行的:
cap = cv2.VideoCapture(0)
dim = 128 # For HOG
while True:
# Capture the frame
ret, frame = cap.read()
# Show the image on the screen
cv2.imshow('Webcam', frame)
# Convert the image to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert the image into a HOG descriptor
gray = cv2.resize(gray, (dim, dim), interpolation = cv2.INTER_AREA)
features = hog.compute(gray)
features = features.T # Transpose so that the feature is in a single row
# Predict the label
pred = clf.predict(features)
# Show the label on the screen
print("The label of the image is: " + str(pred))
# Pause for 25 ms and keep going until you push q on the keyboard
if cv2.waitKey(25) == ord('q'):
break
cap.release() # Release the camera resource
cv2.destroyAllWindows() # Close the image window
上述过程读取图像,在屏幕上显示,将图像转换为灰度,以便我们可以计算其HOG描述符,确保数据在一行中与您训练的SVM兼容,然后我们预测其标签。我们将其打印到屏幕上,然后等待25毫秒,然后再读取下一帧,这样就不会使CPU过载。此外,您可以随时按键盘上的q键退出该程序。否则,该程序将永远循环。完成后,我们将相机资源释放回计算机,以便它可用于其他过程。您是否使用scikit learn的支持向量分类模块?此代码不能单独运行。主要是,您没有显示包含的软件包。@rayryeng抱歉,我已将它们包含在编辑中。我必须使用opencv中的SVM函数吗?不,你不必使用opencv中的SVM函数。我这样问是因为我可以写一个答案:P@rayryeng哈哈,那我该怎么做呢?我已经在写答案了。给我几分钟。谢谢!!但是如果我想通过我的网络摄像头测试它呢?i、 e.通过我的实时网络摄像头检测椅子?没问题。这是一个有趣的练习。祝你好运
cap = cv2.VideoCapture(0)
dim = 128 # For HOG
while True:
# Capture the frame
ret, frame = cap.read()
# Show the image on the screen
cv2.imshow('Webcam', frame)
# Convert the image to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert the image into a HOG descriptor
gray = cv2.resize(gray, (dim, dim), interpolation = cv2.INTER_AREA)
features = hog.compute(gray)
features = features.T # Transpose so that the feature is in a single row
# Predict the label
pred = clf.predict(features)
# Show the label on the screen
print("The label of the image is: " + str(pred))
# Pause for 25 ms and keep going until you push q on the keyboard
if cv2.waitKey(25) == ord('q'):
break
cap.release() # Release the camera resource
cv2.destroyAllWindows() # Close the image window