Pārlūkot izejas kodu

Tolernace to falls configurable by firebase

Gamaliel Espinoza Macedo 6 gadi atpakaļ
vecāks
revīzija
bf29f8fc81
2 mainītis faili ar 23 papildinājumiem un 15 dzēšanām
  1. 15 8
      ondemand/calibration.py
  2. 8 7
      ondemand/service.py

+ 15 - 8
ondemand/calibration.py

@@ -5,8 +5,9 @@ import logging as log
 
 
 class Calibrations(object):
-    DEFAULT_TOLERANCE_RATE = 0.6
-    DEFAULT_THRESHOLD = 10
+    TOLERANCE_RATE = 0.6
+    THRESHOLD = 10
+    FALL_TOLERANCE = 2
 
     def __init__(self, device_id):
         self.dev_ref = fbdb.reference('devices').child(device_id)
@@ -28,26 +29,32 @@ class Calibrations(object):
 
         if not remote and not local:
             calibration = {
-                'tolerance': self.DEFAULT_TOLERANCE_RATE,
-                'threshold': self.DEFAULT_THRESHOLD,
+                'tolerance': self.TOLERANCE_RATE,
+                'threshold': self.THRESHOLD,
+                'fallTolerance': self.FALL_TOLERANCE,
             }
 
         elif remote and not local:
             calibration = {
                 'tolerance': remote.get(
                     'tolerance',
-                    self.DEFAULT_TOLERANCE_RATE,
+                    self.TOLERANCE_RATE,
                 ),
                 'threshold': remote.get(
                     'threshold',
-                    self.DEFAULT_THRESHOLD,
+                    self.THRESHOLD,
                 ),
+                'fallTolerance': remote.get(
+                    'fallTolerance',
+                    self.FALL_TOLERANCE,
+                )
             }
 
         elif not remote and local:
             calibration = {
-                'tolerance': self.DEFAULT_TOLERANCE_RATE,
-                'threshold': self.DEFAULT_THRESHOLD
+                'tolerance': self.TOLERANCE_RATE,
+                'threshold': self.THRESHOLD,
+                'fallTolerance': self.FALL_TOLERANCE,
             }
 
         self.items[station] = calibration

+ 8 - 7
ondemand/service.py

@@ -166,6 +166,7 @@ def process_queue_with_threads():
 def process_segment(item, audios=None, calibration=None):
     """ Procesa una hora de audio """
 
+    tolerance = calibration['tolerance']
     station = item['station']
     date = dateutil.parser.parse(item['date'])
 
@@ -187,9 +188,7 @@ def process_segment(item, audios=None, calibration=None):
     try:
         audio = mutagen.mp3.MP3(filename)
         segments_needed = int(round(float(audio.info.length) / float(5)))
-        segments_needed = int(round(
-            segments_needed * SEGMENTS_TOLERANCE_RATE
-        ))
+        segments_needed = int(round(segments_needed * tolerance))
     except Exception as ex:
         log.error('[process_segment] file {} is not an mp3'.format(filename))
         log.error(str(ex))
@@ -255,7 +254,8 @@ def process_segment(item, audios=None, calibration=None):
             item['id'],
             None, # TODO: send results again
             found=find_repetitions(results,
-                segments_needed=segments_needed
+                segments_needed=segments_needed,
+                calibration=calibration,
             ),
             missing_files=(12 - audios_counter) \
                           if audios_counter < 12 else 0
@@ -266,16 +266,17 @@ def process_segment(item, audios=None, calibration=None):
     except UserWarning as warn:
         log.warning(str(warn))
 
-def find_repetitions(results, segments_needed=2):
+def find_repetitions(results, segments_needed=2, calibration=None):
     found_counter = 0
     found_index = None
     expect_space = False
     expect_recover = False
     last_value_in_threshold_index = -1
+    fall_tolerance = calibration['fallTolerance']
     found = []
 
     if threshold_mode == THRESHOLD_FIXED:
-        threshold = THRESHOLD
+        threshold = calibration['threshold']
     elif threshold_mode == THRESHOLD_AVERAGE:
         values = [x['confidence'] for x in results]
         threshold = math.ceil(float(sum(values)) / float(len(values)))
@@ -293,7 +294,7 @@ def find_repetitions(results, segments_needed=2):
                 if expect_recover:
                     expect_recover = False
 
-            elif FALL_TOLERANCE_SEGMENTS:
+            elif fall_tolerance:
                 if not expect_recover:
                     if last_value_in_threshold_index != -1:
                         """ Solo cuando ya haya entrado por lo menos