libvstfdetect/vstf_lite_detector.cc
/// Run dection
/// micro_samples: samples from micro
/// tv_samples: samples from tv source
Run dection
micro_samples: samples from micro
tv_samples: samples from tv source
Run dection micro_samples : samples from micro tv_samples : samples from tv source
int VstfDetector::RunDetection(const std::vector<int16_t> *micro_samples,
                               vstf_detection_event_t *event) {
  vstf_detector_internals *internals = (vstf_detector_internals *)_privData;
  std::vector<int16_t> &desired_samples = internals->desired_samples;
  std::pair<std::string, float> res;
  event->event_type = VSTF_NONE_DETECTED;
  if (internals->use_alarm_sound_detector == true) {
    VSTF_AlarmDetectionEvent e;
    e = internals->pAlarmSoundDetector->Detect(micro_samples);
    if (e != VSTF_ALARM_NONE) {
      if (e == VSTF_ALARM_TONE1) {
        event->event_type = VSTF_ALARM_TONE1_DETECTED;
      } else if (e == VSTF_ALARM_TONE2) {
        event->event_type = VSTF_ALARM_TONE2_DETECTED;
      } else if (e == VSTF_ALARM_TONE3) {
        event->event_type = VSTF_ALARM_TONE3_DETECTED;
      } else {
        LOG(ERROR) << "Unknown alarm detection event " << e;
        return -1;
      }
      event->confidence = 1.0;
      return 0;
    }
  }
  if (internals->detected == true) {
    internals->desired_samples.erase(internals->desired_samples.begin(),
                                     internals->desired_samples.begin() +
                                         VSTF_BUFFER_WORK_SIZE);
    internals->detected = false;
  }
  internals->desired_samples.insert(std::end(internals->desired_samples),
                                    std::begin(*micro_samples),
                                    std::end(*micro_samples));
  u_int32_t required_sample_size = 0;
  required_sample_size = VSTF_SAMPLE_RATE * internals->frame_duration;
  required_sample_size /= 1000;
  if (desired_samples.size() < required_sample_size) {
    event->event_type = VSTF_NOT_ENOUGH_DATA_DETECTED;
    return 0;
  }
  if (internals->use_door_detector == true) {
    if (internals->pDoorDetector->Detect(desired_samples) == true) {
      event->event_type = VSTF_DOOR_CLOSE_DETECTED;
      event->confidence = 1.0;
      return 0;
    }
  }
  int input = internals->stage1_session->inputs()[0];
  TfLiteIntArray *dims = internals->stage1_session->tensor(input)->dims;
  float *in = internals->stage1_session->typed_tensor<float>(input);
  assert(desired_samples.size() >= dims->data[1]);
  for (int i = 0; i < dims->data[1]; i++) {
    in[i] = Int16SampleToFloat(desired_samples[i]);
  }
  if (internals->stage1_session->Invoke() != kTfLiteOk) {
    LOG(ERROR) << "Running model stage 1 failed: " << std::endl;
    return -1;
  }
  float first_sensitivity;
  if (internals->stage2_session != NULL)
    first_sensitivity = 0.05; 
  else
    first_sensitivity = internals->sensitivity;
  float *predictions = internals->stage1_session->typed_output_tensor<float>(0);
  res = get_best_result(predictions, first_sensitivity,
                        internals->stage1_keywords);
  if (res.second >= first_sensitivity) { 
    event->event_type = VSTF_KEYWORD_DETECTED;
    event->confidence = res.second;
    event->keyword = res.first;
  }
  if (event->event_type == VSTF_KEYWORD_DETECTED && internals->stage2_session) {
    int input = internals->stage2_session->inputs()[0];
    float *in = internals->stage2_session->typed_tensor<float>(input);
    for (int i = 0; i < dims->data[1]; i++) {
      in[i] = Int16SampleToFloat(desired_samples[i]);
    }
    if (internals->stage2_session->Invoke() != kTfLiteOk) {
      LOG(ERROR) << "Running model stage 2 failed: " << std::endl;
      return -1;
    }
    float *predictions =
        internals->stage2_session->typed_output_tensor<float>(0);
    res = get_best_result(predictions, first_sensitivity,
                          internals->stage2_keywords);
    if (res.second >= internals->sensitivity) {
      event->event_type = VSTF_KEYWORD_DETECTED;
      event->confidence = res.second;
      event->keyword = res.first;
    } else {
      event->event_type = VSTF_NONE_DETECTED;
    }
    if (event->event_type == VSTF_KEYWORD_DETECTED &&
        internals->stage3_session) {
      int input = internals->stage3_session->inputs()[0];
      TfLiteIntArray *dims = internals->stage3_session->tensor(input)->dims;
      float *in = internals->stage3_session->typed_tensor<float>(input);
      assert(desired_samples.size() >= dims->data[0]);
      for (int i = 0; i < dims->data[1] ; i++) {
        in[i] = Int16SampleToFloat(desired_samples[i]);
      }
      if (internals->stage3_session->Invoke() != kTfLiteOk) {
        LOG(ERROR) << "Running model stage 3 failed: " << std::endl;
        return -1;
      }
      float *predictions =
          internals->stage3_session->typed_output_tensor<float>(0);
      res = get_best_result(predictions, first_sensitivity,
                            internals->stage3_keywords);
      if (res.second >= internals->sensitivity) {
        event->event_type = VSTF_KEYWORD_DETECTED;
        event->confidence = res.second;
        event->keyword = res.first;
      } else {
        event->event_type = VSTF_NONE_DETECTED;
      }
    }
  }
  if (event->event_type == VSTF_NONE_DETECTED) {
    internals->desired_samples.erase(internals->desired_samples.begin(),
                                     internals->desired_samples.begin() +
                                         VSTF_BUFFER_WORK_SIZE);
  } else {
    internals->detected = true;
  }
  return 0;
}
int VstfDetector :: RunDetection ( const std :: vector < int16_t > * micro_samples , vstf_detection_event_t * event ) { vstf_detector_internals * internals = ( vstf_detector_internals * ) _privData ; std :: vector < int16_t > & desired_samples = internals -> desired_samples ; std :: pair < std :: string , float > res ; event -> event_type = VSTF_NONE_DETECTED ; if ( internals -> use_alarm_sound_detector == true ) { VSTF_AlarmDetectionEvent e ; e = internals -> pAlarmSoundDetector -> Detect ( micro_samples ) ; if ( e != VSTF_ALARM_NONE ) { if ( e == VSTF_ALARM_TONE1 ) { event -> event_type = VSTF_ALARM_TONE1_DETECTED ; } else if ( e == VSTF_ALARM_TONE2 ) { event -> event_type = VSTF_ALARM_TONE2_DETECTED ; } else if ( e == VSTF_ALARM_TONE3 ) { event -> event_type = VSTF_ALARM_TONE3_DETECTED ; } else { LOG ( ERROR ) << " " << e ; return -1 ; } event -> confidence = 1.0 ; return 0 ; } } if ( internals -> detected == true ) { internals -> desired_samples . erase ( internals -> desired_samples . begin ( ) , internals -> desired_samples . begin ( ) + VSTF_BUFFER_WORK_SIZE ) ; internals -> detected = false ; } internals -> desired_samples . insert ( std :: end ( internals -> desired_samples ) , std :: begin ( * micro_samples ) , std :: end ( * micro_samples ) ) ; u_int32_t required_sample_size = 0 ; required_sample_size = VSTF_SAMPLE_RATE * internals -> frame_duration ; required_sample_size /= 1000 ; if ( desired_samples . size ( ) < required_sample_size ) { event -> event_type = VSTF_NOT_ENOUGH_DATA_DETECTED ; return 0 ; } if ( internals -> use_door_detector == true ) { if ( internals -> pDoorDetector -> Detect ( desired_samples ) == true ) { event -> event_type = VSTF_DOOR_CLOSE_DETECTED ; event -> confidence = 1.0 ; return 0 ; } } int input = internals -> stage1_session -> inputs ( ) [ 0 ] ; TfLiteIntArray * dims = internals -> stage1_session -> tensor ( input ) -> dims ; float * in = internals -> stage1_session -> typed_tensor < float > ( input ) ; assert ( desired_samples . size ( ) >= dims -> data [ 1 ] ) ; for ( int i = 0 ; i < dims -> data [ 1 ] ; i ++ ) { in [ i ] = Int16SampleToFloat ( desired_samples [ i ] ) ; } if ( internals -> stage1_session -> Invoke ( ) != kTfLiteOk ) { LOG ( ERROR ) << " " << std :: endl ; return -1 ; } float first_sensitivity ; if ( internals -> stage2_session != NULL ) first_sensitivity = 0.05 ; else first_sensitivity = internals -> sensitivity ; float * predictions = internals -> stage1_session -> typed_output_tensor < float > ( 0 ) ; res = get_best_result ( predictions , first_sensitivity , internals -> stage1_keywords ) ; if ( res . second >= first_sensitivity ) { event -> event_type = VSTF_KEYWORD_DETECTED ; event -> confidence = res . second ; event -> keyword = res . first ; } if ( event -> event_type == VSTF_KEYWORD_DETECTED && internals -> stage2_session ) { int input = internals -> stage2_session -> inputs ( ) [ 0 ] ; float * in = internals -> stage2_session -> typed_tensor < float > ( input ) ; for ( int i = 0 ; i < dims -> data [ 1 ] ; i ++ ) { in [ i ] = Int16SampleToFloat ( desired_samples [ i ] ) ; } if ( internals -> stage2_session -> Invoke ( ) != kTfLiteOk ) { LOG ( ERROR ) << " " << std :: endl ; return -1 ; } float * predictions = internals -> stage2_session -> typed_output_tensor < float > ( 0 ) ; res = get_best_result ( predictions , first_sensitivity , internals -> stage2_keywords ) ; if ( res . second >= internals -> sensitivity ) { event -> event_type = VSTF_KEYWORD_DETECTED ; event -> confidence = res . second ; event -> keyword = res . first ; } else { event -> event_type = VSTF_NONE_DETECTED ; } if ( event -> event_type == VSTF_KEYWORD_DETECTED && internals -> stage3_session ) { int input = internals -> stage3_session -> inputs ( ) [ 0 ] ; TfLiteIntArray * dims = internals -> stage3_session -> tensor ( input ) -> dims ; float * in = internals -> stage3_session -> typed_tensor < float > ( input ) ; assert ( desired_samples . size ( ) >= dims -> data [ 0 ] ) ; for ( int i = 0 ; i < dims -> data [ 1 ] ; i ++ ) { in [ i ] = Int16SampleToFloat ( desired_samples [ i ] ) ; } if ( internals -> stage3_session -> Invoke ( ) != kTfLiteOk ) { LOG ( ERROR ) << " " << std :: endl ; return -1 ; } float * predictions = internals -> stage3_session -> typed_output_tensor < float > ( 0 ) ; res = get_best_result ( predictions , first_sensitivity , internals -> stage3_keywords ) ; if ( res . second >= internals -> sensitivity ) { event -> event_type = VSTF_KEYWORD_DETECTED ; event -> confidence = res . second ; event -> keyword = res . first ; } else { event -> event_type = VSTF_NONE_DETECTED ; } } } if ( event -> event_type == VSTF_NONE_DETECTED ) { internals -> desired_samples . erase ( internals -> desired_samples . begin ( ) , internals -> desired_samples . begin ( ) + VSTF_BUFFER_WORK_SIZE ) ; } else { internals -> detected = true ; } return 0 ; }
Run dection
micro_samples: samples from micro
tv_samples: samples from tv source
Run dection micro_samples : samples from micro tv_samples : samples from tv source
//assert(micro_samples->size() == VSTF_BUFFER_WORK_SIZE); /*
      Here the deletion of first 2048 samples is postponed
      to allow between the last detection and the next call (now),
      the user to record the detected sample
      */ // easy threshold /// threshold /*
      postpone deletion of whole sample to allow
      user to retrieve the detected sample.
      */