将HOG + SVM培训应用于网络摄像头以进行对象检测 [英] Apply HOG+SVM Training to Webcam for Object Detection

查看:81
本文介绍了将HOG + SVM培训应用于网络摄像头以进行对象检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过从正负数据集中提取HOG特征来训练我的SVM分类器

I have trained my SVM classifier by extracting HOG features from a positive and negative dataset

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) 

我想应用该培训,以便程序能够检测到我的自定义对象(椅子).

I want to apply that training so that the program will be able to detect my custom object (chairs).

我已经为每个集合添加了标签;接下来需要做什么?

I have added labels to each set already; what needs to be done next?

推荐答案

您已经掌握了三个最重要的组件. hoggify创建一个HOG描述符列表-每个图像一个.注意,用于计算描述符的预期输入是灰度图像,并且该描述符作为具有1列的2D数组返回,这意味着HOG描述符中的每个元素都有自己的行.但是,您正在使用np.squeeze删除单例列,而是将其替换为一维numpy数组,因此我们在这里很好.然后,您将使用list_to_matrix将列表转换为numpy数组.完成此操作后,您可以使用svmClassify最终训练您的数据.假设您已经将labels放在一维numpy数组中.训练SVM后,您可以使用

You already have three of the most important pieces available at your disposal. hoggify creates a list of HOG descriptors - one for each image. Note that the expected input for computing the descriptor is a grayscale image and the descriptor is returned as a 2D array with 1 column which means that each element in the HOG descriptor has its own row. However, you are using np.squeeze to remove the singleton column and replacing it with a 1D numpy array instead, so we're fine here. You would then use list_to_matrix to convert the list into a numpy array. Once you do this, you can use svmClassify to finally train your data. This assumes that you already have your labels in a 1D numpy array. After you train your SVM, you would use the SVC.predict method where given input HOG features, it would classify whether the image belonged to a chair or not.

因此,您需要执行的步骤是:

Therefore, the steps you need to do are:

  1. 使用hoggify创建HOG描述符列表,每个图像一个.看起来输入x是您称呼椅子图像的前缀,而z表示要加载的图像总数.请记住,range不包含结尾值,因此您可能想在int(z)(即int(z) + 1)之后添加一个+ 1,以确保包括结尾.我不确定是否是这种情况,但是我想把它扔在那里.

  1. Use hoggify to create your list of HOG descriptors, one per image. It looks like the input x is a prefix to whatever you called your chair images as, while z denotes the total number of images you want to load in. Remember that range is exclusive of the ending value, so you may want to add a + 1 after int(z) (i.e. int(z) + 1) to ensure that you include the end. I'm not sure if this is the case, but I wanted to throw it out there.

x = '...' # Whatever prefix you called your chairs
z = 100 # Load in 100 images for example
lst = hoggify(x, z)

  • 将HOG描述符列表转换为实际矩阵:

  • Convert the list of HOG descriptors into an actual matrix:

    data = list_to_matrix(lst)
    

  • 训练您的SVM分类器.假设您已经将标签存储在labels中,其中值0表示不是椅子,而1表示椅子,并且它是一维numpy数组:

  • Train your SVM classifier. Assuming you already have your labels stored in labels where a value 0 denotes not a chair and 1 denotes a chair and it is a 1D numpy array:

    labels = ... # Define labels here as a numpy array
    clf = svmClassify(data, labels)
    

  • 使用SVM分类器执行预测.假设您有要使用分类器进行测试的测试图像,则需要执行与处理训练数据相同的处理步骤.我假设这就是hoggify的作用,您可以在其中指定其他x来表示要使用的不同集合.指定一个新变量xtest以指定此不同的目录或前缀,以及所需的图像数量,然后将hoggifylist_to_matrix结合使用以获取功能:

  • Use your SVM classifer to perform predictions. Assuming you have a test image you want to test with your classifier, you will need to do the same processing steps like you did with your training data. I'm assuming that's what hoggify does where you can specify a different x to denote different sets to use. Specify a new variable xtest to specify this different directory or prefix, as well as the number of images you need, then use hoggify combined with list_to_matrix to get your features:

    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中再次使用data:

    pred will contain an array of predicted labels, one for each test image that you have. If you want, you can see how well your SVM did with the training data, so since you have this already at your disposal, just use data again from step #2:

    pred_training = clf.predict(data)
    

    pred_training将包含一组预测标签,每个训练图像一个.

    pred_training will contain an array of predicted labels, one for each training image.


    如果您最终希望将其用于网络摄像头,则过程将是使用


    If you ultimately want to use this with a webcam, the process would be to use a VideoCapture object and specify the ID of the device that is connected to your computer. Usually there's only one webcam connected to your computer, so use the ID of 0. Once you do this, the process would be to use a loop, grab a frame, convert it to grayscale as HOG descriptors require a grayscale image, compute the descriptor, then classify the image.

    假设您已经训练了模型并从之前创建了HOG描述符对象,这样的事情就可以工作:

    Something like this would work, assuming that you've already trained your model and you've created a HOG descriptor object from before:

    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 ms,然后再读下一帧,这样才不会使您的CPU过载.另外,您可以随时按键盘上的 q 键退出程序.否则,该程序将永远循环.完成后,我们会将相机资源释放回计算机,以便可以将其用于其他进程.

    The above process reads in an image, displays it on the screen, converts the image into grayscale so we can compute its HOG descriptor, ensures that the data is in a single row compatible for the SVM you trained and we then predict its label. We print this to the screen, and we wait for 25 ms before we read in the next frame so we don't overload your CPU. Also, you can quit the program at any time by pushing the q key on your keyboard. Otherwise, this program will loop forever. Once we finish, we release the camera resource back to the computer so that it can be made available for other processes.

    这篇关于将HOG + SVM培训应用于网络摄像头以进行对象检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆