Golang 基于chrome浏览器语音识别web演示系统WebHTK开发之 引擎篇

   要说引擎篇,也谈不上。底层语音识别的实现,是基于HTK开源语音识别工具:http://htk.eng.cam.ac.uk/# 前面用过这个工具,也用JNI转成java可调用的接口,所以还算比较熟悉。这次引擎,也只是用cgo来转。HMM模型都是用HTK的工具训练好的,只要有数据,baidu,google上教程还是很多的,自带的HTKBOOK就是最权威的教程。

   在实现阶段,主要就是用到三个工具:HCopy(提取特征),HVite(viterbi算法识别),HParse(制作语法词网)。看看都需要哪些文件:Golang 基于chrome浏览器语音识别web演示系统WebHTK开发之 引擎篇


   这些文件中,只有上述三个HCopy.c,HVite.c,HParse.c还有main主函数,所以只要把三个main函数重命名下,传参仍不变,并修改下这三个文件中一些相同命名的函数(比方Usage这类的),再写个asr.h的头文件,把上述重命名的三个main函数加进去就ok了,asr.h:

1
2
3
4
5
#ifndef ASR_H
int HCopy(int argc, char *argv[]);
int HVite(int argc, char *argv[]);
int HParse(int argc, char *argv[]);
#endif


   再看下HTKengine.go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package HTKengine
                                                                                                                                                                                               
//#include "asr.h"
//#include "string.h"
//#include "stdlib.h"
import "C"
import "unsafe"
                                                                                                                                                                                               
type cmd struct {
    HCOPY  string
    HVITE  string
    HPARSE string
}
                                                                                                                                                                                               
var Command *cmd = &cmd{"HCopy""HVite""HParse"}
                                                                                                                                                                                               
func HCopy(args []string) {
    arg := make([](*_Ctype_char), 0)
    l := len(args)
    for i, _ := range args {
        char := C.CString(args[i])
        defer C.free(unsafe.Pointer(char))
        strptr := (*_Ctype_char)(unsafe.Pointer(char))
        arg = append(arg, strptr)
    }
                                                                                                                                                                                               
    C.HCopy(C.int(l), (**_Ctype_char)(unsafe.Pointer(&arg[0])))
}
                                                                                                                                                                                               
func HVite(args []string) {
    arg := make([](*_Ctype_char), 0)
    l := len(args)
                                                                                                                                                                                               
    for i, _ := range args {
        char := C.CString(args[i])
        defer C.free(unsafe.Pointer(char))
        strptr := (*_Ctype_char)(unsafe.Pointer(char))
        arg = append(arg, strptr)
    }
                                                                                                                                                                                               
    C.HVite(C.int(l), (**_Ctype_char)(unsafe.Pointer(&arg[0])))
}
                                                                                                                                                                                               
func HParse(args []string) {
    arg := make([](*_Ctype_char), 0)
    l := len(args)
                                                                                                                                                                                               
    for i, _ := range args {
        char := C.CString(args[i])
        defer C.free(unsafe.Pointer(char))
        strptr := (*_Ctype_char)(unsafe.Pointer(char))
        arg = append(arg, strptr)
    }
                                                                                                                                                                                               
    C.HParse(C.int(l), (**_Ctype_char)(unsafe.Pointer(&arg[0])))
}


   原来的main都是要跟上args参数的,这里把他作为接口调用,所以还是需要传进args []string。在我前面的博客中,有提到cgo,可以参考下:

    golang在window环境下用cgo编译c语言

    Golang cgo编程 [] string 转 C语言 char**


   在前面录音篇,有大概讲到在服务器保存wav文件,HCopy就直接跟在wav保存的代码后,而HVite跟在HCopy后。现在时间不允许,否则,可以改下HCopy的代码,直接传byte[]数据。其实在pocketsphinx中就是这么实现的,有空也可以给pocketsphinx写几个cgo的接口来调用。


   关于这个系列的博客,应该不会再写什么了,web到这,功能已经完成80%,后面也就是添加些,动态添加待识别语句到数据库的功能,说白了,就是你想识别哪些语句,你可以自己定义,提交给服务器,通过HParse重新制作词网来搞定的。也没有什么跟web开发有很大关系的,顶多是涉及到数据库,不过目前我对数据库了解不多,也只用到简单的CRUD操作,所以嘛,就不多写了。

   上周接到网易游戏的面试电话,木有任何准备,被虐的狗一样啊。所以近阶段的博客,应该会写些java,golang数据结构方面的吧。










本文转自 ponpon_ 51CTO博客,原文链接:http://blog.51cto.com/liuxp0827/1374334,如需转载请自行联系原作者
上一篇:线程的命名和取得|学习笔记


下一篇:百度:如何将AI进行到底?