Kaynağa Gözat

Nueva comparacion

hquijada 4 yıl önce
ebeveyn
işleme
f4c4e2f92e
1 değiştirilmiş dosya ile 50 ekleme ve 7 silme
  1. 50 7
      ondemand/service.py

+ 50 - 7
ondemand/service.py

@@ -58,7 +58,7 @@ THRESHOLD_AVERAGE = 2
 QUEUE_SINGLE = 1
 QUEUE_THREAD = 2
 
-# Se pueden usar diferentes API's
+# Se pueden usar diferentes API'se
 # la de threading y la de multiprocessing.
 MultiAPI = Process
 
@@ -211,11 +211,15 @@ def process_segment(item, audios=None, calibration=None):
     try:
         filenames = []
         id_by_ad = {}
+        item_ids = []
         x = 0
         for i in item["elementos"]:
             x = x + 1
-            log.info('[process_segment] {} downloading ad {}'.format(x, i["anuncio"]))
+            log.info('[process_segment] downloading ad {} {}'.format(x, i["anuncio"]))
             id_by_ad[i['anuncio']] = i['id']
+            if i['id'] not in item_ids:
+                item_ids.append(i['id'])
+
             filename, md5hash = cloud_download(ad_key=i["anuncio"])
             if filename:
                 filenames.append((filename, md5hash))
@@ -248,6 +252,7 @@ def process_segment(item, audios=None, calibration=None):
 
     dejavu = Dejavu({"database_type": "mem"})
     try:
+        x = 0
         for i in filenames:
             filename = i[0]
             dejavu.fingerprint_file(filename)
@@ -273,7 +278,7 @@ def process_segment(item, audios=None, calibration=None):
     v = []
 
     for path, name, ts in audios_iterable:
-        short_path = os.path.join(station, name)
+        # short_path = os.path.join(station, name)
         audios_counter += os.path.isfile(path)
         values = []
 
@@ -283,7 +288,9 @@ def process_segment(item, audios=None, calibration=None):
         try:
             for match in dejavu.recognize(recognizer, path, segment_size):
                 name = None
+                ad = None
                 try:
+                    ad = match['name']
                     if match['name'] in id_by_ad.keys():
                         name = id_by_ad[match['name']]
                     else:
@@ -293,6 +300,7 @@ def process_segment(item, audios=None, calibration=None):
                     pass
 
                 results.append({
+                    'ad': ad,
                     'confidence': match['confidence'],
                     'timestamp': ts,
                     'offset': match['offset'],
@@ -313,9 +321,19 @@ def process_segment(item, audios=None, calibration=None):
             log.error('[process_segment] {}'.format(ex))
 
     try:
-        for i in item["elementos"]:
-            r = [result for result in results if result["name"] == i["id"]]
-            i['encontrados'] = find_repetitions(r, segments_needed=segments_needed, calibration=calibration,)
+        encontrados = {}
+        for i in item_ids:
+            r = [result for result in results if result["name"] == i]
+            encontrados[i] = find_repetitions(r, segments_needed=segments_needed, calibration=calibration,)
+
+        for id in encontrados:
+            for e in encontrados[id]:
+                for i in item['elementos']:
+                    if i['id'] == id and i['anuncio'] == e['ad']:
+                        if 'encontrados' not in i:
+                            i['encontrados'] = []
+                        i['encontrados'].append(e)
+                        break
 
         item["archivos_perdidos"] = (12 - audios_counter) if audios_counter < 12 else 0
         response = send_results(item)
@@ -336,6 +354,12 @@ def find_repetitions(results, segments_needed=2, calibration=None):
     fall_tolerance = calibration['fallTolerance']
     found = []
 
+    high = 100 # Obtener este valor desde un parámetro
+    middle_high = 50 # Obtener este valor desde un parámetro
+    segment_middle_needed = 2 # Obtener este valor desde un parámetro
+    found_high = None
+    found_middle_high = []
+
     if threshold_mode == THRESHOLD_FIXED:
         threshold = calibration['threshold']
     elif threshold_mode == THRESHOLD_AVERAGE:
@@ -346,6 +370,14 @@ def find_repetitions(results, segments_needed=2, calibration=None):
         segments_needed = 1
 
     for index, result in enumerate(results):
+        if result['confidence'] >= high:
+            if found_high is None:
+                found_high = index
+            elif result['confidence'] > results[found_high]['confidence']:
+                found_high = index
+        elif result['confidence'] >= middle_high:
+            found_middle_high.append(index)
+
         if not expect_space:
             if result['confidence'] >= threshold:
                 found_counter += 1
@@ -380,6 +412,16 @@ def find_repetitions(results, segments_needed=2, calibration=None):
                 found_down_counter = 0
                 found_index = None
                 expect_recover = False
+                # Aquí veremos si hay un valor alto
+                if found_high is not None:
+                    found_row = results[found_high]
+                    found.append(found_row)
+                elif len(found_middle_high) >= segment_middle_needed:
+                    found_row = results[found_middle_high[0]]
+                    found.append(found_row)
+                found_high = None
+                found_middle_high = []
+
         else:
             if result['confidence'] <= threshold:
                 expect_space = False
@@ -389,6 +431,8 @@ def find_repetitions(results, segments_needed=2, calibration=None):
             found.append(found_row)
             found_counter = 0
             expect_space = True
+            found_high = None
+            found_middle_high = []
 
     return found
 
@@ -500,7 +544,6 @@ def get_pendings():
 
 def send_results(item):
     url = 'https://api.fourier.audio/v1/calendario/resultado'
-    # url = "http://requestbin.net/r/1bcyvg91"
     headers = {
         'Authorization': 'Bearer {}'.format(config['apiSecret'], )
     }