recognizer.cpp
2.1 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "recognizer.h"
#include <json/json.h>
static boolean reconhecendo ;
static string sentence;
static float confidence;
string lenguage = "pt-BR";
using namespace std;
void resultado(Recog *recog, void *dummy);
Recognizer::Recognizer(){
}
Recognizer::~Recognizer(){
}
int Recognizer::recognize(string file_in)
{
FILE *file;
string cmFinal;
char message[100];
message[0] = '\0';
string jsonResult ="";
string vozReconhecida = "";
int indexLineSrt =0;
stringstream comand;
stringstream index;
string fileName = file_in;
// inicio preparação requisição
comand << " curl -ss -X POST --data-binary @";
comand << fileName;
//requisição para o google speech
comand<< " --user-agent 'Mozilla/5.0' --header 'Content-Type: audio/l16; rate=22050;' 'https://www.google.com/speech-api/v2/recognize?client=chromium&lang="<<lenguage<<"&maxresults=1&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'";
//AIzaSyBeeYW4l2OuCwiUfzBaUXXeWAO6Uy-u0F8'";
//public key Ezequiel project 1
//AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw'";
//fim
cmFinal = comand.str();
file = popen(cmFinal.c_str(),"r");
if(file == NULL)
printf("ERROR\n");
int vez =0;
while (fgets(message, sizeof (message), file))
{
vez++;
if(vez>1){
jsonResult+= message;
}
}
//cout<< jsonResult <<endl<<endl;
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(jsonResult, root);
// cout << root["result"]<<endl;
confidence = 0.0;
sentence = "";
if (parsingSuccessful)
{
sentence = root["result"][0]["alternative"][0]["transcript"].asString();
confidence = root["result"][0]["alternative"][0]["confidence"].asFloat();
}
fclose(file);
jsonResult = "";
comand.str("");
return 1;
}
float Recognizer::getconfidence(){
return confidence;
}
string Recognizer::getsentence(){
// printf("palavra %s\n",sentence.c_str() );
return sentence;
}