recognizer.cpp 2.1 KB



#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;
}