Browse Source

Second threshold mode

Gamaliel Espinoza Macedo 6 years ago
parent
commit
bfde0f4b89
1 changed files with 16 additions and 2 deletions
  1. 16 2
      ondemand/service.py

+ 16 - 2
ondemand/service.py

@@ -22,6 +22,7 @@ import mutagen.mp3
 import requests
 import dateutil
 import sqlite3
+import math
 import time
 import sys
 import os
@@ -35,6 +36,10 @@ THRESHOLD = 10
 SEGMENTS_TOLERANCE_RATE = 0.6
 FALL_TOLERANCE_SEGMENTS = 1
 
+# THRESHOLD
+THRESHOLD_FIXED = 1
+THRESHOLD_AVERAGE = 2
+
 # Modos de procesamiento de queue
 #  - QUEQUE_SINGLE: procesa solo un segmento a la vez
 #  - QUEUE_THREAD:  inicia un hilo para cada segmento
@@ -61,7 +66,10 @@ dejavu = Dejavu({"database_type":"mem"})
 device_id = config['device_id']
 device_path = os.path.join(base_path, device_id)
 recognizer = FilePerSecondRecognizer
+
+# settings
 queue_mode = QUEUE_THREAD
+threshold_mode = THRESHOLD_AVERAGE
 
 db_path = config.get('localDatabase', os.path.join(device_path, 'files.db'))
 db = sqlite3.connect(db_path)
@@ -248,12 +256,18 @@ def find_repetitions(results, segments_needed=2):
     last_value_in_threshold_index = -1
     found = []
 
+    if threshold_mode == THRESHOLD_FIXED:
+        threshold = THRESHOLD
+    elif threshold_mode == THRESHOLD_AVERAGE:
+        values = [x['confidence'] for x in results]
+        threshold = math.ceil(float(len(values)) / float(sum(values)))
+
     if segments_needed < 1:
         segments_needed = 1
 
     for index, result in enumerate(results):
         if not expect_space:
-            if result['confidence'] >= THRESHOLD:
+            if result['confidence'] >= threshold:
                 found_counter += 1
                 last_value_in_threshold_index = index
                 if found_index is None:
@@ -284,7 +298,7 @@ def find_repetitions(results, segments_needed=2):
                 found_index = None
                 expect_recover = False
         else:
-            if result['confidence'] <= THRESHOLD:
+            if result['confidence'] <= threshold:
                 expect_space = False
 
         if found_counter >= segments_needed: