上篇提到了怎么用video-caffe训练UCF101的demo,然后我就要把他应用到手势识别的数据集上。
首先要对数据集进行处理,因为没有深入阅读video-caffe训练UCF101demo 的源码,因此这里采用了笨办法去适应程序的数据集格式,要求图片格式为image_xxxx.jpg , 刚开始比较苦恼,后来百度了下用windows批处理就可以轻松搞定。
批量加前缀的程序:
@echo off
for /f "delims=" %%f in ('dir/b/s/a-d *.*') do (if not "%%~nxf"=="%0" ren "%%f" "image_%%~nxf")
批量改后缀的程序:
ren *.png *.jpg
后来发现直接修改后缀后在windows环境下可以读,但是在Ubuntu环境下就无法读取,于是摒弃这种方式,而转用格式转换工具,试了两种-格式工厂和ACDSee,格式工厂压缩严重,ACDSee稍微好点。
这样数据集格式就满足条件了。
接下来需要的操作是生成均值文件,本来example里带着生成mean文件的脚本,奈何生成出来的文件才有10bytes,而且训练的时候读取也有问题,遂改用opencv来计算图像均值,幸好手势数据是灰度图像单通道的,程序就更为简单了。
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <io.h>
#include <stdlib.h>
#include "opencv2\opencv.hpp"
using namespace std;
using namespace cv;
double aaa = 0;
double bbb = 0;
bool get_filelist_from_dir(string, vector<string>&);
int main()
{
string file_read_path = "C:\\Users\\feynman\\Desktop\\手势识别\\10类数据\\train\\train_10\\";
string search_path_01 = file_read_path + "*.jpg";
vector<string> file_list;
vector<string> file_list_new;
string n_str;
string image_path_save;
int temp;
if (!get_filelist_from_dir(search_path_01, file_list))
cout << "open file error!" << endl;
for (int i = 0; i < file_list.size(); i++)
{
string image_path = file_read_path + file_list[i];
const char *p = image_path.c_str();
IplImage* src = cvLoadImage(p, 0);//导入图片
int width = src->width;//图片宽度
int height = src->height;//图片高度
for (int row = 0; row<height; row++)
{
uchar* ptr = (uchar*)src->imageData + row*src->width;//获得灰度值数据指针
for (int cols = 0; cols<width; cols++)
{
uchar intensity = ptr[cols];//数据类型
bbb =(int)intensity;//强制转换
aaa = aaa + bbb;
}
}
}
aaa = aaa / (250*115*1024);
cout << aaa;
return 0;
}
bool get_filelist_from_dir(string path, vector<string>& files)
{
long hFile = 0;
struct _finddata_t fileinfo;
files.clear();
if ((hFile = _findfirst(path.c_str(), &fileinfo)) != -1)
{
do
{
if (!(fileinfo.attrib & _A_SUBDIR))
files.push_back(fileinfo.name);
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
return true;
}
else
return false;
}
另一个版本貌似也可以:
#include "cv.h"
#include "highgui.h"
#include "iostream"
double ImgAver(IplImage *img);
int main(){
using namespace std;
double aaa = ImgAver(cvLoadImage("1.png", 0));
cout << aaa;
}
/********************************************************************
*函数描述: ImgAver 计算并返回一幅图像的均值
*函数参数: IplImage *img 单通道8位图像
*函数返回值:double
*******************************************************************/
double ImgAver(IplImage *img)
{
int i, j;//循环变量
int height = img->height;
int width = img->width;
int step = img->widthStep / sizeof(uchar);
uchar *data = (uchar*)img->imageData;
double aver = 0.0;
for (i = 0; i<height; i++)
{
for (j = 0; j<width; j++)
{
aver += data[i*step + j];
}
}
aver = 1.0*aver / (height*width);
return aver;
}