浏览代码

Configurable segment size

Gamaliel Espinoza Macedo 6 年之前
父节点
当前提交
cce0e56f37
共有 2 个文件被更改,包括 34 次插入6 次删除
  1. 13 1
      ondemand/calibration.py
  2. 21 5
      ondemand/service.py

+ 13 - 1
ondemand/calibration.py

@@ -8,6 +8,12 @@ class Calibrations(object):
     TOLERANCE_RATE = 0.6
     THRESHOLD = 10
     FALL_TOLERANCE = 0
+    SEGMENT_SIZE = 5
+
+    # SEGMENT_SIZE
+    # n = cualquier cantidad de segundos
+    # ceil = la duracion del audio en segmentos de 5
+    # integer = la duracion exacto del audio 
 
     def __init__(self, device_id):
         self.dev_ref = fbdb.reference('devices').child(device_id)
@@ -22,7 +28,7 @@ class Calibrations(object):
         try:
             remote = calref.get()
         except fbdb.ApiCallError as err:
-            log.error(err)
+            log.error('[get] {}'.format(err))
             remote = None
 
         local  = self.items.get(station, None)
@@ -32,6 +38,7 @@ class Calibrations(object):
                 'tolerance': self.TOLERANCE_RATE,
                 'threshold': self.THRESHOLD,
                 'fallTolerance': self.FALL_TOLERANCE,
+                'segmentSize': self.SEGMENT_SIZE,
             }
 
         elif not remote and local:
@@ -39,6 +46,7 @@ class Calibrations(object):
                 'tolerance': self.TOLERANCE_RATE,
                 'threshold': self.THRESHOLD,
                 'fallTolerance': self.FALL_TOLERANCE,
+                'segmentSize': self.SEGMENT_SIZE,
             }
 
         else:
@@ -54,6 +62,10 @@ class Calibrations(object):
                 'fallTolerance': remote.get(
                     'fallTolerance',
                     self.FALL_TOLERANCE,
+                ),
+                'segmentSize': remote.get(
+                    'segmentSize',
+                    self.SEGMENT_SIZE,
                 )
             }
 

+ 21 - 5
ondemand/service.py

@@ -112,6 +112,7 @@ def feed_queue():
         """ Errores desconocidos """
         log.error('[feed_queue] {}'.format(ex))
         loop.add_timeout(time.time() + 60, feed_queue)
+        raise ex
 
 def process_queue():
     """ Try to the next item in a queue and start
@@ -166,14 +167,21 @@ def process_queue_with_threads():
 def process_segment(item, audios=None, calibration=None):
     """ Procesa una hora de audio """
 
-    tolerance = calibration['tolerance']
     station = item['station']
+
+    if not calibration:
+        calibration = calibrations.get(station)
+
+    tolerance = calibration['tolerance']
     date = dateutil.parser.parse(item['date'])
+    segment_size = calibration['segmentSize']
+    audio_length = 0
 
-    log.info('[process_segment] (th: {}, tl: {}, ft: {}) {}'.format(
+    log.info('[process_segment] (th: {}, tl: {}, ft: {}, ss: {}) {}'.format(
         calibration['threshold'],
         calibration['tolerance'],
         calibration['fallTolerance'],
+        calibration['segmentSize'],
         item,
     ))
 
@@ -188,8 +196,16 @@ def process_segment(item, audios=None, calibration=None):
     # de acuerdo a la duración total del audio.
     try:
         audio = mutagen.mp3.MP3(filename)
-        segments_needed = int(round(float(audio.info.length) / float(5)))
+        audio_length = audio.info.length
+
+        if segment_size == 'integer':
+            segment_size = int(audio_length)
+        elif segment_size == 'ceil':
+            segment_size = int(math.ceil(audio_length / 5)) * 5
+
+        segments_needed = int(round(float(audio_length) / float(segment_size)))
         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))
@@ -222,7 +238,7 @@ def process_segment(item, audios=None, calibration=None):
                 .format(short_path))
             continue
 
-        for match in dejavu.recognize(recognizer, path, 5,
+        for match in dejavu.recognize(recognizer, path, segment_size,
                                       ads_filter=[md5hash]):
             try:
                 results.append({
@@ -263,7 +279,7 @@ def process_segment(item, audios=None, calibration=None):
         )
         log.info('[{}] API response: {}'.format(station, response))
     except ConnectionError as ex:
-        log.error(str(ex))
+        log.error('[process_segment] {}'.format(str(ex)))
     except UserWarning as warn:
         log.warning(str(warn))