利用video-caffe训练自己的数据

上篇提到了怎么用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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值