码迷,mamicode.com
首页 > 其他好文 > 详细

[Audio processing] 数据集生成 & 性别年龄分类训练

时间:2016-03-08 21:11:03      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

1、重命名,Python中文路径各种错误,所以需要先将所有文件的路径名全都改成中文。用的是MAC系统,所以WIN下的命令行批处理没法解决,所以用C来完成

技术分享
//  Created by Carl on 16.
//  Copyright (c) 2016年 Carl. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
using namespace std;

void getFileList()
{
    string sourceDir = "/Users/karl/Work/database/rawdata/children_CN/";
    string targetDir = "/Users/karl/Work/database/rawdata/children/";
    DIR *dir;
    struct dirent *ptr;
    int i = 0;
    if ((dir=opendir(sourceDir.c_str())) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }
    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent
            continue;
        else if(ptr->d_type == 8)
        {
            printf("%s %s\n",(sourceDir + ptr->d_name).c_str(),(targetDir + to_string(i) + ".wav").c_str());
            if(rename((sourceDir + ptr->d_name).c_str(), (targetDir + to_string(i++) + ".wav").c_str())<0)
                cout<<"error"<<endl;
            else
                cout<<"ok"<<endl;
        }
        
    }
    return;
}

int main() {
    getFileList();
    return 1;
}
View Code

2、然后再使用FFMPEG那篇文章写的Python代码,将所有音频文件转成统一格式

技术分享
#coding=utf-8
#!/usr/bin/env python
‘‘‘CREATED:2016-03-08
Use example of ffmpeg
‘‘‘
import argparse
import sys
import os
import string
import subprocess as sp

#Full path of ffmpeg
FFMPEG_BIN = "/Users/karl/Documents/python/audio/tool/ffmpeg"
#Full path of sourceDir
sourceDir = "/Users/karl/Work/database/rawdata/male/"
#Full path of targetDir
targetDir = "/Users/karl/Work/database/age/male/"
#Channel setting 1 for mono
ac = 1
#Sample frequency
sf = 16000
#Extension setting
ext = wav

def convert(sourceDir, targetDir, ac, sf, ext):
    i = 0
    if not os.path.exists(targetDir):
        os.mkdir(targetDir)
    files = os.listdir(sourceDir)
    for f in files:
        if f.endswith(.wav):
            command = [ FFMPEG_BIN,
                       -i, os.path.join(sourceDir, f),
                       -ac, str(ac),
                       -ar, str(sf), os.path.join(targetDir, str(i) + "." + ext)]
            i += 1
            print command
            pipe = sp.Popen(command, stdout = sp.PIPE, bufsize = 10**8)

if __name__ == __main__:
    convert(sourceDir, targetDir, ac, sf, ext)
View Code

 

[Audio processing] 数据集生成 & 性别年龄分类训练

标签:

原文地址:http://www.cnblogs.com/littletail/p/5255290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!