現在、私は、ライブストリーム上の人をリアルタイムで検出するプログラムに取り組んでいます。したがって、私はOpenCV 3.1でC++を使用しています。 私のアプローチは、SVMを訓練し、HOG Detectorを使用することです。OpenCVでのSVMのトレーニング
私はSVMを訓練するときに、特にトレーニングマトリックスを構築することによって何かがうまくいかないと思います。 My Detectorは作成されたSVMを読み込み、hog.detectMultiscale()で使用します。 opencvの「getDefaulPeopleDetector」のdefault-svmを使用すると、簡単な画像で良い結果が得られました。
私は自分の訓練されたSVMを適用する必要があります。なぜなら私たちのテストセットアップはかなり難しく、デフォルトのものはそれをカバーしていないからです。 私の検出器を実行して得た結果は本当に悪いです。カバーされたウェブカメラ/黒画面上の「人」または画面全体のランダムな検出を検出します。ここで
は私のコードです:そのサイズがcoorectある場合getImagesディレクトリ内の負荷のすべての画像をチェック(64x128)私はSVMによって使用されている訓練行列を作成するには、この方法では
//Reads and checks the images from the given directory
static void getImages(vector<string>& fileNames, const string dirName) {
DIR *dir;
dir = opendir(dirName.c_str());
struct dirent *ent;
string imgName;
string imgPath;
Mat img;
if (dir != NULL) {
while ((ent = readdir (dir)) != NULL) {
imgName = ent->d_name;
imgPath = dirName + "/" +imgName;
img = imread(imgPath);
if(!img.data) {
cout << imgPath << " has no data" << endl;
} else {
if(img.rows != IMG_ROW_SIZE && img.cols != IMG_COL_SIZE) {
cout << imgName << " size not correct: " << img.cols << "x" << img.rows <<endl;
} else {
cout << imgPath << " loaded" << endl;
fileNames.push_back(imgPath);
}
}
}
closedir (dir);
} else {
cout << dirName << " not present" << endl;
}
}
。列車()。 すべての画像について、私は豚の特徴を計算します。 1つの特徴ベクトルは、トレーニング行列の1つの行に挿入される。 すべての行には1つの画像の特徴が含まれています。 私はこれが私の主な方法である
static void initTrainingMats(Mat& trainingMat, vector<string>& positiveImages, vector<string>& negativeImages, Mat& labelMat) {
HOGDescriptor hog;
vector<float> hogFeature;
Mat img;
cout << "computing hog features.. ";
for(int i = 0; i<trainingMat.rows; i ++) { // for-loop iterating through all images
img = (i < positiveImages.size() ? imread(positiveImages.at(i)) : imread(negativeImages.at(i-positiveImages.size())));
labelMat.at<float>(i,1) = (i < positiveImages.size() ? 1 : -1);
hog.compute(img, hogFeature, Size(8,8), Size(8,8));
for(int j = 0; j<trainingMat.cols; j++) {
trainingMat.at<float>(i,j) = hogFeature.at(j);
}
}
(SVM with images)hogfeaturesの計算が正しく実装されているかどうかわからないですか、マトリックスは、この記事で示した方法で作成されていない場合。 SVMを初期化して保存します。
int main(int, char**)
{
//vectors to store file path to each correct image
vector<string> positiveImages;
vector<string> negativeImages;
getImages(positiveImages, posDirName);
getImages(negativeImages, negDirName);
int allImages = positiveImages.size() + negativeImages.size();
//initialize training matrix and label matrix
Mat trainingMat(allImages, 3780 ,CV_32FC1); //matrix with column size same as size of hog_sample vector
Mat labelMat(allImages, 1, CV_32SC1);
initTrainingMats(trainingMat, positiveImages, negativeImages, labelMat);
Ptr<SVM> svm = SVM::create();
svm->setType(SVM::C_SVC);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-10));
svm->train(trainingMat, ROW_SAMPLE, labelMat);
svm->save("../../hog_video/build/svm_v2");
cout<<"SVM stored successfully" << endl;
}
私はちょっと私はあなたたちが私のために得たすべてのヘルプ/提案のための嬉しい、ここで立ち往生しています!