def recognizeAudio(): djv = Dejavu(config) djv.fingerprint_directory("songs", [".mp3"], 3) print "starting recognition" song = djv.recognize(MicrophoneRecognizer, seconds=5) print "finished recognition" print(song) return song['song_name']
def findSong(): global confidence, song djv = Dejavu(config) song_internal = djv.recognize(FileRecognizer, "voice.wav") print(song_internal) confidence = song_internal['confidence'] song = song_internal
def fingerprint(args): print args (surahDirectory, surahFile) = args surahFile = surahDirectory.rstrip('/') + "/" + surahFile databaseName = "dejavu" + os.path.basename(surahFile)[:3] # MySQL database config # Dejavu stores fingerprints in MySQL using the config provided below. config = { "database": { "host": DATABASE_HOST, "user": DATABASE_USER, "passwd": DATABASE_PASSWORD, "db": databaseName, } } print surahFile.split("/")[-1] # Create a database for the current surah db1 = mysql.connect(host=config["database"]["host"], user=config["database"]["user"], passwd=config["database"]["passwd"]) cursor = db1.cursor() sql = 'CREATE DATABASE IF NOT EXISTS ' + config["database"]["db"] cursor.execute(sql) # Pass the MySQL config to dejavu to fingerprint the surah djv = Dejavu(config) djv.fingerprint_file(surahFile)
class Lilo(): """ The class for JamJar which will be used to identify video matches and add fingerprints to the database """ def __init__(self, config, filename, video_id): """ usage: fingerprinter = Lilo('/path/to/video/file','unique_video_id') """ self.djv = Dejavu(config) self.filename = filename self.video_id = video_id # cache these after first fingerprint self.hashes = None def recognize_track(self): # Try to match the song to the existing database hashes, songs = self.djv.recognize(FileRecognizer, self.filename) self.hashes = hashes return songs def fingerprint_song(self): # Now let's add this song to the DB data = self.djv.fingerprint_file(self.filename, self.video_id, cached_hashes=self.hashes) return data def check_if_fingerprinted(self): video_hash = unique_hash(self.filename) # Set self.djv.songhashes_set fingerprinted_video_hashes = self.djv.get_fingerprinted_songs() return video_hash in self.djv.songhashes_set
def main(): djv = Dejavu(config) if request.method == 'GET': file_log = open("hashes_samples.log", "r") array_hashes = file_log.readlines() elif request.method == 'POST': json_local = request.get_json() if len(json_local['hashes']) == 0: return jsonify({'error': 'invalid input'}) string_hashes = json_local['hashes'] string_hashes = string_hashes.replace("[", "") string_hashes = string_hashes.replace("]", "") array_hashes = string_hashes.rstrip().split(', ') hashes = dict() samples_indexes = [] for string_hash in array_hashes: sample_index, hash_local, offset = string_hash.rstrip().split('|') if sample_index in hashes: hashes[sample_index].append((hash_local, int(offset))) else: hashes[sample_index] = [] if sample_index not in samples_indexes: samples_indexes.append(sample_index) matches = [] for sample_index in samples_indexes: matches.extend(djv.rest_find_matches(hashes[sample_index])) return jsonify(djv.align_matches(matches))
def mon_super_test(request): print("=" * 50) print(request.data) files = request.data.get("uploaded_file") # Read song from parameters -> download it as mp3 file files.file.seek(0) b = io.BytesIO(files.file.read()) song = AudioSegment.from_file(b, format="3gp") song.export("temp/test.mp3", format="mp3") # create a Dejavu instance djv = Dejavu(config) # Recognize audio from a file song_found = djv.recognize(FileRecognizer, "temp/test.mp3") if song_found: song_name = song_found.get('song_name').split("--") artist = song_name[0] title = song_name[1] else: artist = "" title = "" return JsonResponse({'artist': artist, 'title': title})
def crbt(): result = "" if request.method == 'POST': print request if 'file' not in request.files: print('No file part') file = request.files['file'] if file.filename == '': print('No selected file') file = request.files['file'] filename = secure_filename(file.filename) # os.path.join is used so that paths work in every operating system file.save(os.path.join("temp", filename)) # You should use os.path.join here too. with open("dejavu.cnf.SAMPLE") as f: config = json.load(f) djv = Dejavu(config) song = djv.recognize(FileRecognizer, "temp/" + filename) path = "temp/" + filename os.remove(path) title = song["song_name"].replace('_', ' ') confidence = song["confidence"] if (confidence < 50): result = {'title': "Unknown Song"} else: result = {'title': title} return jsonify(result)
def msearch(self): types = ('audio/wav', 'audio/mp3') for aud in self.request.files: print aud aud = self.request.files[aud] if len(aud) < 1: return u'上传失败' _t = aud[0]['content_type'] if _t not in types: return u'上传文件有误,请回去重新上传' print fname = md5(aud[0]['filename'] + str(time.time())).hexdigest() fname += '.wav' fname = os.path.join('temp', fname) # print type() with open(fname, 'wb') as f: f.write(aud[0]['body']) try: djv = Dejavu(config) print 'begint recognize...' song = djv.recognize(FileRecognizer, fname) except Exception as e: song = str(e) finally: # os.remove(fname) pass print 'recognize: ', song return song
def run(): print("Running digest") for filename in os.listdir(path): print(path + filename) djv = Dejavu(config) response = djv.recognize(FileRecognizer, path + filename) print(response) print("*" * 30) if response: save_record(filename, response) os.remove(path + filename) print("Matched and record saved") return "2" else: #No match was found if "_attempt1" in filename: response = { 'offset': 0, 'confidence': 0, 'offset_seconds': 0, 'match_time': 0, 'song_name': 'None', 'dejavu_log': '' } save_record(filename, response) os.remove(path + filename) print("Did not match and record saved") return "0" else: new_filename = filename.split('.')[0] + "_attempt1.wav" os.rename(path + filename, path + new_filename) print("Did not match, will try again") return "1"
def msearch(self): types=('audio/wav','audio/mp3') for aud in self.request.files: print aud aud=self.request.files[aud] if len(aud)<1: return u'上传失败' _t=aud[0]['content_type'] if _t not in types: return u'上传文件有误,请回去重新上传' print fname=md5(aud[0]['filename']+str(time.time())).hexdigest() fname+='.wav' fname = os.path.join('temp', fname) # print type() with open(fname, 'wb') as f: f.write(aud[0]['body']) try: djv = Dejavu(config) print 'begint recognize...' song = djv.recognize(FileRecognizer, fname) except Exception as e: song = str(e) finally: # os.remove(fname) pass print 'recognize: ', song return song
def mp_worker(urldata): url = None station_name = None song = None name = None try: url, station_name = urldata name = station_name + '.mp3' except ValueError: pass try: u = urllib.request.urlopen(url) data = u.read(150000) with open(name, 'wb') as file: file.write(data) time.sleep(1) except Exception as e: print(e) #try: djv = Dejavu(config) song = djv.recognize(FileRecognizer, name) # print("From Stream we recognized: {}\n".format(song)) if song is None: print("NONE") elif song['confidence'] >= 40: file_data = None try: with open(station_name + ".txt", 'r') as file: file_data = file.read() except Exception as e: with open(station_name + ".txt", 'w') as file: pass if file_data == song["song_name"]: print('ad already recorded') with open('log_date.txt', 'a') as writeFile: writeFile.write( "\n Duplicate recognition with confidence %d %s " % (song["confidence"], song["song_name"])) else: with open(station_name + ".txt", 'w') as file: file.write(song["song_name"]) db_cls = get_database(config.get("database_type", None)) db = db_cls(**config.get("database", {})) db.setup() # count = db.get_song_count_by_name(song["song_name"]) # db.update_song_count(song["song_name"],count['count']+1) d_local = datetime.datetime.now(pytz.timezone("Asia/Kathmandu")) db.insert_radio_song(station_name, song["song_name"], 'Begari Guys', int(song['confidence']), d_local) print("From file we recognized: {}\n".format(song["song_name"])) with open('log_date.txt', 'a') as writeFile: writeFile.write("\n Identified with high confidence %d %s" % (song['confidence'], song['song_name'])) else: with open('log_date.txt', 'a') as writeFile: writeFile.write("\n Identified with very less confidence %d %s" % (song['confidence'], song["song_name"])) print("From file we recognized: {} {} \n".format( song["song_name"], song['confidence']))
def fingerprintAndComputeAccuracy(fingerprintReduction, peakSort, defaultOverlapRatio, defaultFanValue, defaultAmpMin, peakNeighbourhoodSize): # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("mp3", [".mp3"])
def recognise_microphone(): djv = Dejavu(config) secs = 10 song = djv.recognize(MicrophoneRecognizer, seconds=secs) if song is None: print "Nothing recognized -- did you play the song out loud so your mic could hear it? :)" else: print "From mic with %d seconds we recognized: %s\n" % (secs, song)
def main(): # Set up our commang line arguments parser = argparse.ArgumentParser(description="Take in a folder of video and/or audio files and find the alignment" "of them all.") parser.add_argument("folder",type=str,help="The folder containing your audio and video files, relative to the current directory.") args = parser.parse_args() # Our DB settings config = { "database": { "host": "127.0.0.1", "user": "******", "passwd": "root", "db": "dejavu" } } # Get the files in our folder dir = os.path.expanduser(args.folder) files = os.listdir(dir) # Set up our dejavu instance djv = Dejavu(config) # Generate our corpus name - we'll add this functionality later. corpus = dir.replace(" ","") corpus = corpus.lower() # For now, let's just empty the DB before the experiment djv.db.empty() # Iterate through the files for filename in files: full_path = os.path.join(dir,filename) # For now we'll assume all the files are valid audio or video if (os.path.isfile(full_path)): print "Attempting to match {0}...".format(filename) # Try to match the song to the existing database songs = djv.recognize(FileRecognizer, full_path) if songs: for song in songs: print song else: print "No matches found." print "Adding {0} to database...".format(filename) # Now let's add this song to the DB djv.fingerprint_file(full_path) print djv.db.get_num_fingerprints()
def dejavu(self, select_song): # config = {"database": {"host": "127.0.0.1", "user": "******", "passwd": "", "db": "dejavu"}} config = {"database": {"host": "10.66.31.157", "user": "******", "passwd": "662813", "db": "dejavu"}} djv = Dejavu(config) # djv.fingerprint_directory("mp3/Piano/", [".mp3"]) # djv.fingerprint_directory("mp3/Violin/", [".mp3"]) song = djv.recognize(FileRecognizer, select_song) # print(song) return song
def run_client(): """ runs all the patches """ run() directory_path = raw_input("enter the directory path") djv = Dejavu(config) djv.fingerprint_directory(directory_path, [".mp3"], 3)
def runDejavu(): # create a Dejavu instance djv = Dejavu(config) # service.py executed as script # do something activateMic.micStart() results = djv.recognize(FileRecognizer, "mp3/recording.wav") print(f"From file we recognized: {results}\n")
class DejavuPlainDBTestCases(unittest.TestCase): def setUp(self): self.djv = Dejavu(config_plain) def tearDown(self): self.djv.db.empty() del self.djv def test_fingerprint_1_file(self): self.djv.fingerprint_file("tests/test1.mp3") # should be the only fingerprinted file self.assertEqual(1, self.djv.db.get_num_songs()) self.assertEqual(5279, self.djv.db.get_num_fingerprints()) def test_fingerprint_directory(self): list_dir = [f for f in os.listdir("tests") if f[-4:] == ".mp3"] self.djv.fingerprint_directory("tests", [".mp3"]) self.assertEqual(len(list_dir), self.djv.db.get_num_songs()) def test_fingerprint_1_file_10secs(self): self.djv.limit = 10 self.djv.fingerprint_file("tests/test1.mp3") # should be the only fingerprinted file self.assertEqual(1, self.djv.db.get_num_songs()) # fingerprinting the first 10 secs of this test file, # shouldn't get more than 3000 hashes. self.assertEqual(2554, self.djv.db.get_num_fingerprints()) def test_recognize_1_file(self): self.djv.fingerprint_file("tests/test1.mp3") self.djv.fingerprint_file("tests/test2.mp3") song = self.djv.recognize(FileRecognizer, "tests/test2.mp3") self.assertEqual(song["song_name"], "tests/test2.mp3")
def start(event): if __name__ == '__main__': djv = Dejavu(config) secs = 8 song = djv.recognize(MicrophoneRecognizer, seconds=secs) if song is None: print "Nothing recognized -- did you play the song out loud so your mic could hear it?" else: print "From mic with %d seconds we recognized: %s\n" % (secs, song)
def __init__(self, config, filename, video_id): """ usage: fingerprinter = Lilo('/path/to/video/file','unique_video_id') """ self.djv = Dejavu(config) self.filename = filename self.video_id = video_id # cache these after first fingerprint self.hashes = None
def main(): config = { "database": { "host": "127.0.0.1", "user": "******", "passwd": "root", "db": "Philadelphia" } } djv = Dejavu(config) # start fingerprint djv.fingerprint_directory("../../Seattle/data/songs", [".mp3"], 4) print djv.db.get_num_fingerprints()
def recognize_audio(): # load config # check if the post request has the audio part if "audio" not in request.files: data = {'message': 'Audio to fingerprint is missing.'} return jsonify(data), 400 else: print("bat dau xu ly file upload") audio = request.files["audio"] #filename = request.form["info"] if audio.filename == "": data = {'message': 'Audio to fingerprint is missing.'} return jsonify(data), 400 else: # Save file in a temporary location try: name, ext = os.path.splitext(audio.filename) print("Save file...") if ext not in ('.mp3'): return {'message': 'File extension not allowed.'} audio.save(os.path.join(path, name + ext)) except Exception as ex: template = "An 1 exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) data = {'message': message} return data # Create a Dejavu instance print("Create instance") djv = Dejavu(config) # Recognize audio from a file try: print("recognize audio is starting") song = djv.recognize(FileRecognizer, os.path.join(path, name + ext)) print(song) data = {'message': 'successful', 'result': song} os.remove(os.path.join(path, name + ext)) if song: return jsonify(data) else: return {'message': 'None'} except Exception as ex: template = "An 2 exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) data = {'message': message} return data return Response(status=200)
def audio_test(test_files_root=None, extensions=None): djv = Dejavu(config) file_list = get_dir_files(test_files_root, extensions) true_num = 0 offsets = [] statistics_off = {} avg_offsets = 0.0 for index, filename in enumerate(file_list): music_name = filename.split('/')[-1].split('.')[0] song = djv.recognize(FileRecognizer, test_files_root + "/" + filename.split('/')[-1]) flag = 'False' pos = 'False' if not song: continue if find_lcsubstr(song['song_name'], music_name)[1] >= 2: true_num += 1 flag = 'True' music_start_pos = float(music_name.split('_')[-2]) result_pos = song['song_name'].split('_')[-2] tempos = float(result_pos) + float(song['offset_seconds']) offset = tempos - music_start_pos offsets.append(offset) if statistics_off.has_key(str(int(offset))): statistics_off[str(int(offset))] += 1 else: statistics_off[str(int(offset))] = 1 print "%03d: %3d/%-3d %s offset:%02.3f %-30s search_result:%-20s detail_info:%s" % ( (index + 1), true_num, len(file_list), flag, offset, music_name, song['song_name'], song) max_key = '0' max_value = -1 for key, value in statistics_off.items(): if int(statistics_off[key]) > int(max_value): max_value = value max_key = key sum_pos = 0 for off in offsets: if abs(int(off) - int(max_key)) <= 1: sum_pos += 1 print "%d/%-3d pos_true_num:%d" % (true_num, len(file_list), sum_pos)
def upload_file(): if request.method == 'POST': file = request.files['file'] brand = request.form['brand'] offertitle = request.form['offertitle'] offercontent = request.form['offercontent'] image = request.files['offerimage'] offerlink = request.form['offerlink'] if file and allowed_file(file.filename) and image and allowed_file( image.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # Can also rename the uploaded file if you want #os.rename(UPLOAD_FOLDER + filename, UPLOAD_FOLDER+'niloofar.jpg') FILEPATH = UPLOAD_FOLDER + filename imagename = secure_filename(image.filename) image.save( os.path.join(app.config['UPLOAD_FOLDER'] + "images/", imagename)) imagepath = UPLOAD_FOLDER + "images/" + imagename #Initializing Dejavu object with config djv = Dejavu(config) #Insert clip and its fingerprint in DB sid = djv.fingerprint_file(FILEPATH) #Insert related AdContent to DB # Create db connection to add data db = mysql.connect() cursor = db.cursor() cursor.execute( "INSERT INTO adcontent (sid, brand, offertitle, offercontent, imagepath, offerlink) VALUES (%s,%s,%s,%s,%s,%s)", (sid, brand, offertitle, offercontent, imagepath, offerlink)) db.commit() db.close() #Data object for html template data = {"sid": sid} print( "\nSuccessfully added clip and adcontent to DB and recorded Fingerprint!" ) return render_template('upload.html', data=data) return render_template('upload.html')
def start_febreze(): djv = Dejavu(config) for ele in learn_music: djv.fingerprint_file('mp3/' + str(ele) + '.mp3') print('Sleeping for 5 seconds') time.sleep(5) print('Listening for sounds') song = djv.recognize(MicrophoneRecognizer, seconds=10) print(song) if song['song_name'] in learn_music and song['confidence'] > 500: response = requests.request("PUT", url, data=payload, headers=headers) temp = json.loads(response.text) if str(temp['status']) == 'OK': print('Dispensed Febreze')
def fingerprint_audio(): # check if the post request has the audio part if ('audio') not in request.files: data = {'message': 'Audio to fingerprint is missing.'} return jsonify(data), 400 elif ('info') not in request.form: data = {'message': 'Info of file is missing'} return jsonify(data), 400 else: print("bat dau xu ly file upload") audio = request.files["audio"] filename = request.form["info"] if audio.filename == "": data = {'message': 'Audio to fingerprint is missing.'} return jsonify(data), 400 else: # Save file in a temporary location try: name, ext = os.path.splitext(audio.filename) print("Save file...") if ext not in ('.mp3'): return {'message': 'File extension not allowed.'} audio.save(os.path.join(path2, str(filename) + ext)) except Exception as ex: template = "An 1 exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) data = {'message': message} return data # Create a Dejavu instance print("Create instance") djv = Dejavu(config) # Fingerprinting audio from a file try: print("recognize audio is starting") song = djv.fingerprint_file( os.path.join(path2, str(filename) + ext)) data = {'message': 'completed'} print(song) os.remove(os.path.join(path2, str(filename) + ext)) return jsonify(data) except Exception as ex: template = "An 2 exception of type {0} occurred. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) data = {'message': message} return data return Response(status=200)
def __main1(): # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("seed", [".wav"]) #print djv.db.get_num_fingerprints() #print "----------------------------17---------" fp = open('result'+'.dat','w+') # Recognize audio from a file prefix ='mp3' start = time.clock() totalNumSong = 0 rightNum = 0 missNum = 0 # for root, dirs, files in os.walk(prefix): # for dirx in dirs: # fp.write('%s\n'%dirx) print "-------------------------------------" fp.write("--Recognizing-----------------------------------\n") for filename in glob.glob(prefix+'/*.wav'): #print filename # filenamex= os.path.basename(filename).replace('.wav','') song = djv.recognize(FileRecognizer, filename) totalNumSong += 1 if not song is None: txtBuffer = "%s; %s"%(filename, filter(str.isalpha, song['song_name'])) if filename[5:7] in filter(str.isalpha, song['song_name']): rightNum += 1 else: txtBuffer = "%s; None"%(filename) missNum +=1 fp.write(txtBuffer+'\n') print txtBuffer end=time.clock() fp.write("--Performance-----------------------------------\n") fp.write("Prediction time duration: %f seconds\n"%(end-start)) fp.write("Total number of predicted songs: %d \n"%totalNumSong) fp.write("Prediction duration per song: %f seconds\n"%(float(end-start)/float(totalNumSong))) fp.write("Correct rate: %f %% \n"%(float(rightNum)/float(totalNumSong)*100)) fp.write("No result rate: %f %%\n"%(float(missNum)/float(totalNumSong)*100)) fp.close()
def fingerprintFile(fileName): config = { "database": { "host": "127.0.0.1", "user": MYSQL_USER, "passwd": MYSQL_PASS, "db": "dejavu", }, "database_type": "mysql", "fingerprint_limit": -1 } djv = Dejavu(config) djv.fingerprint_file(fileName + ".wav", fileName)
def mp_worker(urldata): url = None number = None song = None name = None try: url, number = urldata name = 'recording' + number + '.mp3' except ValueError: pass try: u = urllib.request.urlopen(url) data = u.read(100000) with open(name, 'wb') as file: file.write(data) time.sleep(1) except Exception as e: print(e) try: djv = Dejavu(config) song = djv.recognize(FileRecognizer, name) if song is None: print("NONE") elif song['confidence'] >= 100: db_cls = get_database(config.get("database_type", None)) db = db_cls(**config.get("database", {})) db.setup() # count = db.get_song_count_by_name(song["song_name"]) # db.update_song_count(song["song_name"],count['count']+1) d = datetime.datetime.now() timezone = pytz.timezone("Asia/Katmandu") d_local = timezone.localize(d) db.insert_radio_song(number, song["song_name"], 'Begari Guys', int(song['confidence']), d_local) print("From file we recognized: {} {}\n".format(song["song_name"])) with open('log.txt', 'a') as writeFile: writeFile.write("\n Identified with high confidence %d %s" % (song['confidence'], song["song_name"])) else: with open('log.txt', 'a') as writeFile: writeFile.write( "\n Identified with very less confidence %d %s" % (song['confidence'], song["song_name"])) print("From file we recognized: {} {} {}\n".format( song["song_name"], song['confidence'])) except Exception as e: print(e)
def fingerprint(): uploaded_files = request.files.getlist("files") for file in uploaded_files: filename = secure_filename(file.filename) file.save(os.path.join("song", filename)) print "Mean aii" with open("dejavu.cnf.SAMPLE") as f: config = json.load(f) djv = Dejavu(config) djv.fingerprint_directory("song", [".mp3"]) for file in uploaded_files: filename = secure_filename(file.filename) path = "song/" + filename os.remove(path) print "Delete aii" return render_template('index.html', code=1)
def init(): # load config from a JSON file (or anything outputting a python dictionary) with open("dejavu.cnf") as f: config = json.load(f) # create a Dejavu instance return Dejavu(config)
class _Recognizer: def __init__(self, index: int, callback: Callable[[Sound, int], None]): self._logger = create_logger(f"Recognizer{index}") self._barrier = Barrier(2) self._index = index self._callback = callback self._stop = False Thread(target=self._run).start() def _run(self): self._dejavu = Dejavu(dburl=f"sqlite:///bells{self._index}.sqlite") while not self._stop: self._barrier.wait() self._recognize() def recognize(self): self._barrier.wait() def _recognize(self): song = self._dejavu.recognize(MicrophoneRecognizer, seconds=_listen_seconds, channels=1) if song: sound = Sound(song['song_id']) confidence = song['confidence'] self._callback(sound, confidence) def close(self): self._stop = True self._barrier.abort()
def init(configpath): try: with open(configpath) as f: config = json.load(f) except IOError as err: print("Cannot open configuration: %s. Exiting" % (str(err))) sys.exit(1) return Dejavu(config)
def detectar_sonido(nombre_archivo): # load config from a JSON file (or anything outputting a python dictionary) with open(settings.BASE_DIR + "/dejavu.cnf.SAMPLE") as f: config = json.load(f) djv = Dejavu(config) # # Fingerprint all the mp3's in the directory we give it # djv.fingerprint_directory("mp3", [".mp3"]) # Recognize audio from a file song = djv.recognize( FileRecognizer, settings.BASE_DIR + "/archivos_reconocer/" + nombre_archivo + ".mp3") print "From file we recognized: %s\n" % song ####una vez detectado hay que eliminar el archivo archivo = settings.BASE_DIR + "/archivos_reconocer/" + nombre_archivo + ".mp3" os.remove(archivo) ##debo notificar al afiliado sobre su nueva cancion por medio de una notificacion push push_service = FCMNotification(api_key=settings.FCM_APIKEY) token, datos = nombre_archivo.split('_') try: usuario = Token.objects.get(key=token) usuario_token = TokensFCM.objects.get(usuario=usuario.user) registration_id = usuario_token.token message_title = "Nueva canción detectada" message_body = "Hola " + usuario.user.username + " la canción que estabas escuchando es " + song[ 'song_name'] result = push_service.notify_single_device( registration_id=registration_id, message_title=message_title, message_body=message_body) except Token.DoesNotExist: pass except TokensFCM.DoesNotExist: pass return song
def check_dejavu(path): dburl = 'sqlite:///new.db' djv = Dejavu(dburl=dburl) recognizer = FileRecognizer(djv) song = recognizer.recognize_file(path) if song['confidence'] < DEJAVU_THRESHOLD: return ("Not found", False) return (song['song_id'], True)
def match_file(): if request.method == 'POST': print(str(request)) file = request.files['uploaded_file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['TEMP_FOLDER'], filename)) FILEPATH = TEMP_FOLDER + filename #Initializing Dejavu object with config djv = Dejavu(config) #Passing sample clip for retrieving the original track matched_track = djv.recognize(FileRecognizer, FILEPATH) print(matched_track) #Retrieve AdContent of the matched_track adcontent = getAdContent(matched_track['song_id']) response = { "song_id": matched_track['song_id'], "song_name": matched_track['song_name'], "match_time": matched_track['match_time'], "confidence": matched_track['confidence'], "brand": adcontent[3], "offertitle": adcontent[2], "offercontent": adcontent[4], "offerimage": adcontent[5], "offerlink": adcontent[6], "offerid": adcontent[0] } print(jsonify(response)) #Delete temporary sample clip os.remove(FILEPATH) #Return response to API caller in JSON format return jsonify(response) return render_template('match.html')
def __init__(self, labels_fname, video_name): label_file_type = mimetypes.guess_type(labels_fname)[0] video_file_type = mimetypes.guess_type(video_name)[0] if label_file_type[:3] != "tex": #The file is not a labels file print "Incorrect label file" raise Exception(INCORRECT_LABEL_FILE_ERROR) if video_file_type[:3] != "vid": #The file is not a video file print "Incorrect video file" raise Exception(INCORRECT_VIDEO_FILE_ERROR) self.labels = LabelsFile(labels_fname) self.video_name = video_name self.djv = Dejavu(CONFIG) self.db_content = []
def recognize(self, samples_patch, downloads_patch, file_format): self.samples_patch = samples_patch self.downloads_patch = downloads_patch self.file_format = file_format self.var_check() warnings.filterwarnings('ignore') with open('dejavu.conf') as f: config = json.load(f) djv = Dejavu(config) # -= updating audio fingerprint base =- print ('%s > Updating fingerprints...' % datetime.datetime.now()) djv.fingerprint_directory(self.samples_patch, [self.file_format]) # -= recognizing downloaded files =- print ('%s > Recognizing files...' % datetime.datetime.now()) for i in range(len(names)): file = self.downloads_patch + '/' + names[i] print ('%s > Now recognizing: %s' % (datetime.datetime.now(), names[i])) song = djv.recognize(FileRecognizer, file) recognized.append(song['song_name']) print ('%s > From file we recognized: %s' % (datetime.datetime.now(), recognized[i])) print ('%s > Finished!' % datetime.datetime.now())
def __main(action): # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("mp3", [".wav"]) #print djv.db.get_num_fingerprints() #print "----------------------------17---------" fp = open(action+'.dat','w+') # Recognize audio from a file #prefix ='..\sdl\%s'%action prefix ='mp3\%s'%action files = prefix +'\*.mp3' totalNumSong = 0 start = time.clock() for filename in glob.glob(files): #print filename filenamex= filename.split('\\')[-1] filenamex=filenamex[7:-4] song = djv.recognize(FileRecognizer, filename) totalNumSong += 1 if not song is None: txtBuffer = "%s;%s"%(filenamex, song['song_name']) else: txtBuffer = "%s;None"%(filenamex) fp.write(txtBuffer+'\n') print txtBuffer end=time.clock() fp.write("----------------------------60---------\n") fp.write("Prediction time duration: %f \n"%(end-start)) fp.write("Total number of predicted songs: %d \n"%totalNumSong) fp.write("Prediction duration for per song: %f \n"%(float(end-start)/float(totalNumSong))) fp.close()
def scan_directory(directory, extension): if extension.endswith('.mp3'): print('scanning directory...'.format(directory)) djv = Dejavu(config) djv.fingerprint_directory(directory, [extension], 3) # 3 is processes elif extension.endswith('.m4a'): print('scanning directory...'.format(directory)) djv = Dejavu(config) djv.fingerprint_directory(directory, [extension], 3) # 3 is processes else: print("file format '{0}' not coded yet".format(extension)) # recognise mp3 song = djv.recognize(FileRecognizer, "/home/jonas/Downloads/dejavu/CC.mp3") print("found song '%s\n'" % song)
def __init__(self, video_name): """ Takes the video name. Creates a Dejavu object. Also obtains the duration and number of frames in the video. """ file_type = mimetypes.guess_type(video_name)[0] if file_type[:3] != "vid":#The file is not a video file print "No video file found" raise Exception(INCORRECT_VIDEO_FILE_ERROR) self.video_name = video_name self.djv = Dejavu(CONFIG) #We create the audio of the video given to us ffmpeg.create_audio(self.video_name, TEMP_AUDIO) self.frames, self.Fs, hash_val = decoder.read(TEMP_AUDIO) self.frames = self.frames[0] #Since we always create single channel audio self.duration = int(self.frames.shape[0] / (self.Fs*1.0)) #Duration of the entire video in seconds
# Database URI examples: # mysql: 'mysql+mysqldb://scott:tiger@localhost/foo' # postgresql: 'postgresql://*****:*****@localhost/mydatabase' # sqlite: 'sqlite:///foo.db' # in memory sqlite: 'sqlite://' config = { "database_backend" : "orm", "database_uri": "sqlite:///fingerprints.sqlite", "fingerprint_limit" : 10, } # previous backend can still be used: # config = { # "database_backend" : "plain", # "database": { # "host": "127.0.0.1", # "user": "", # "passwd": "", # "db": "", # }, # "fingerprint_limit" : 10, # } # create a Dejavu instance djv = Dejavu(config) # Fingerprint a file djv.fingerprint_file("tests/test1.mp3")
class songListener(threading.Thread): """This class listens for songs in a background thread using the Dejavu audio fingerprinter. When it hears a song it sets global flags""" global chickenDance global robotDance global discoDance global egyptianDance global ymcaDance global danceStop def __init__(self, config): threading.Thread.__init__(self) #Initialize the dejavu listener self.djv = Dejavu(config) self.running = True def run(self): global chickenDance global robotDance global discoDance global egyptianDance global ymcaDance global danceStop #do the listening while(self.running): response = self.djv.recognize(MicrophoneRecognizer, seconds=5) if(response == None): continue if(response["confidence"] > 30 and response["song_name"] == "chickenDance") and (not (robotDance or discoDance or egyptianDance or ymcaDance)): print "Found " + response["song_name"] print "Confidence: " + str(response["confidence"]) + " units" chickenDance = True danceStop = False elif(response["confidence"] > 30 and response["song_name"] == "technologic") and (not (chickenDance or discoDance or egyptianDance or ymcaDance)): print "Found " + response["song_name"] print "Confidence: " + str(response["confidence"]) + " units" robotDance = True danceStop = False elif(response["confidence"] > 30 and response["song_name"] == "stayingAlive") and (not (chickenDance or robotDance or egyptianDance or ymcaDance)): print "Found " + response["song_name"] print "Confidence: " + str(response["confidence"]) + " units" discoDance = True danceStop = False elif(response["confidence"] > 30 and response["song_name"] == "walkLikeAnEgyptian") and (not (chickenDance or robotDance or discoDance or ymcaDance)): print "Found " + response["song_name"] print "Confidence: " + str(response["confidence"]) + " units" egyptianDance = True danceStop = False elif(response["confidence"] > 30 and response["song_name"] == "YMCA") and (not (chickenDance or robotDance or discoDance or egyptianDance)): print "Found " + response["song_name"] print "Confidence: " + str(response["confidence"]) + " units" ymcaDance = True danceStop = False else: chickenDance = False robotDance = False discoDance = False egyptianDance = False ymcaDance = False def stop(self): self.running = False
import warnings import json import os import time warnings.filterwarnings("ignore") from dejavu import Dejavu from dejavu.recognize import FileRecognizer, MicrophoneRecognizer # load config from a JSON file (or anything outputting a python dictionary) with open("dejavu.cnf") as f: config = json.load(f) if __name__ == '__main__': # create a Dejavu instance config["database"]["passwd"] = os.environ.get(config["database"]["passwd"]) config["database"]["db"] = os.environ.get(config["database"]["db"]) djv = "" for i in range(0, 20): try: djv = Dejavu(config) break except Exception,e: print e time.sleep(1) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("scripts", [".wav"])
# mysql: 'mysql+mysqldb://scott:tiger@localhost/foo' # postgresql: 'postgresql://*****:*****@localhost/mydatabase' # sqlite: 'sqlite:///foo.db' # in memory sqlite: 'sqlite://' config = { "database_backend" : "orm", "database_uri": "sqlite:///fingerprints.sqlite", "fingerprint_limit" : 10, } # previous backend can still be used: # config = { # "database_backend" : "plain", # "database": { # "host": "127.0.0.1", # "user": "", # "passwd": "", # "db": "", # }, # "fingerprint_limit": 10, # } # create a Dejavu instance djv = Dejavu(config) # Recognize audio from a file song = djv.recognize(FileRecognizer, "tests/test1.mp3") print song
try: response = subprocess.check_output( ["ping", "-c", "3", hostname], stderr=subprocess.STDOUT, # get all output universal_newlines=True, # return string not bytes ) except subprocess.CalledProcessError: response = None return response try: blinkLed(10) if check_vpn() is not None: config = getConguration() djv = Dejavu(config) djv.create_session(config["fingerprint"]["id"], config["vpn_ip"], config["remote_ip"]) djv.log_event("action", "boot") atexit.register(exit_handler, djv) if __name__ == "__main__": a = datetime.now() listen = 1 pause = 0.5 it = 1 try: while True: blinkLed(2) song = djv.recognize(MicrophoneRecognizer, seconds=listen) if song is not None: djv.log_event("match", str(song["song_id"])) print "Recognized %s\n" % (song)
warnings.filterwarnings("ignore") # load config from a JSON file (or anything outputting a python dictionary) # with open("dejavu.cnf.SAMPLE") as f: #configrura la base de datos config = { "database": { "host": "127.0.0.1", "user": "******", "passwd": "ksilva", "db": "dejavu" } } # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it/configura el directorio donde estaran alojadas las firmas # la firma de pistas se eejcutara con firmarDejavu.py de forma separada # reconocer archivos de una carpeta para hacer el reconocimiento de pistas de un directorio def find_files(directory, pattern): for root, dirs, files in os.walk(directory): for basename in files: if fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) yield filename # gather files to fingerprint /configura el directorio donde estaran alojadas las pistas a comparar # UNLABELED_AUDIO_DIR = "./lockon/" #UNLABELED_AUDIO_DIR = "./mp3/http:/216.55.186.61:8071"
def __init__(self, config): threading.Thread.__init__(self) #Initialize the dejavu listener self.djv = Dejavu(config) self.running = True
"database": { "host": "127.0.0.1", "user": "******", "passwd": "pass", "db": "dejavu" } } # load config from a JSON file (or anything outputting a python dictionary) #with open("dejavu.cnf.SAMPLE") as f: # config = json.load(f) if __name__ == '__main__': # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("mp3", [".mp3"]) # Recognize audio from a file song = djv.recognize(FileRecognizer, "mp3/Sean-Fournier--Falling-For-You.mp3") print "From file we recognized: %s\n" % song # Or recognize audio from your microphone for `secs` seconds secs = 5 song = djv.recognize(MicrophoneRecognizer, seconds=secs) if song is None: print "Nothing recognized -- did you play the song out loud so your mic could hear it? :)" else: print "From mic with %d seconds we recognized: %s\n" % (secs, song)
__author__ = 'Mark' from dejavu import Dejavu from dejavu.recognize import FileRecognizer if __name__ == '__main__': config = { "database": { "host": "127.0.0.1", "user": "******", "passwd": "root", "db": "dejavu", } } djv = Dejavu(config) songs = djv.recognize(FileRecognizer, "../../../Desktop/media/jeff_alliy_clipped.wav") for song in songs: print song
class Generate(object): """ Given a labelled output file and the corresponding video tags, extracts the commercial segments and fingerprints them. """ def __init__(self, labels_fname, video_name): label_file_type = mimetypes.guess_type(labels_fname)[0] video_file_type = mimetypes.guess_type(video_name)[0] if label_file_type[:3] != "tex": #The file is not a labels file print "Incorrect label file" raise Exception(INCORRECT_LABEL_FILE_ERROR) if video_file_type[:3] != "vid": #The file is not a video file print "Incorrect video file" raise Exception(INCORRECT_VIDEO_FILE_ERROR) self.labels = LabelsFile(labels_fname) self.video_name = video_name self.djv = Dejavu(CONFIG) self.db_content = [] def build_db(self, aud_ext=".wav", vid_ext=".mpg"): """ Build a sql db with the commercials """ #This returns the entire contents of the file, more about structure of return type in fileHandler.py labels = self.labels.read_lables() #Number of files in the directory + 1 filename = len(os.listdir(DB_AUDIO)) + 1 #Try reading contents from csv file, if it already exists try: #Already exists #We open and read the content first with open(DBNAME) as f: lines = f.readlines() self.db_content = [line.split(',')[1] for line in lines[1:]] #Only names of commercials f = open(DBNAME, "a") except: #Creating for the first time print "File does not exist so creating..." f = open(DBNAME, "w") f.write("name, duration, path\n") for data in labels: #Extracting based on structure of return type start = data[0] end = data[1] name = data[2] if self.db_content != [] and (name in self.db_content): print "Already Fingerprinted" continue duration = timeFunc.get_delta_string(start, end) #Create a file in the db folder, audio and video are stored seperately ffmpeg.create_video(start, duration, self.video_name, DB_VIDEO + str(filename) + vid_ext) ffmpeg.create_audio(DB_VIDEO + str(filename) + vid_ext, DB_AUDIO + str(filename) + aud_ext) #Create a corresponding entry in the csv file s = ",".join([name, duration]) s = s + "," + DB_VIDEO + str(filename) + vid_ext + "\n" #Check verified to be true since human tagged f.write(s) filename += 1 def fingerprint_db(self, aud_ext=".wav", no_proc=1): #This fingerprints the entire directory self.djv.fingerprint_directory(DB_AUDIO, [aud_ext]) def clean_db(self, aud_ext=".wav", vid_ext=".mpg"): choice = raw_input("Are you sure you want to remove all commercials in the database? (yes/no)") if choice == "yes": #Clear the mysql db self.djv.clear_data() print "Cleaning database.." #Now we remove files from db/audio and db/video filename = len(os.listdir(DB_AUDIO)) + 1 for i in range(1, filename): try: os.remove(DB_AUDIO + str(i) + aud_ext) os.remove(DB_VIDEO + str(i) + vid_ext) except: print "File already removed, or you don't have permission" os.remove(DBNAME) print "Database is empty" def run(self, aud_ext=".wav", vid_ext=".mpg"): self.build_db(aud_ext, vid_ext) self.fingerprint_db(aud_ext, vid_ext)
from dejavu import Dejavu import warnings import json warnings.filterwarnings("ignore") # load config from a JSON file (or anything outputting a python dictionary) with open("dejavu.cnf") as f: config = json.load(f) # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("mp3", [".mp3"]) # Recognize audio from a file from dejavu.recognize import FileRecognizer song = djv.recognize(FileRecognizer, "mp3/beware.mp3") # Or recognize audio from your microphone for 10 seconds from dejavu.recognize import MicrophoneRecognizer song = djv.recognize(MicrophoneRecognizer, seconds=2) # Or use a recognizer without the shortcut, in anyway you would like from dejavu.recognize import FileRecognizer recognizer = FileRecognizer(djv) song = recognizer.recognize_file("mp3/sail.mp3")
"-s", "--secs", help="how many seconds to fingerprint for recognition", type=int) args = parser.parse_args() # load config from a JSON file (or anything outputting a python dictionary) config = { "database": { "host": "127.0.0.1", "user": "******", "passwd": "", "db": "dejavu" } } if args.secs: config["fingerprint_limit"] = args.secs if __name__ == '__main__': # create a Dejavu instance djv = Dejavu(config) # Recognize audio from a file print("start recognizing") with Timer("djv.recognize") as t: song = djv.recognize(FileRecognizer, args.file) print("From file we recognized: %s\n" % song)
import sys sys.path.insert(0, './dejavu') from dejavu import Dejavu import warnings import json warnings.filterwarnings("ignore") # load config from a JSON file (or anything outputting a python dictionary) with open("dejavu.cnf") as f: config = json.load(f) # create a Dejavu instance djv = Dejavu(config) # Fingerprint all the mp3's in the directory we give it djv.fingerprint_directory("assets", [".mp3"]) # Recognize audio from a file #from dejavu.recognize import FileRecognizer #song = djv.recognize(FileRecognizer, "mp3/beware.mp3") # Or recognize audio from your microphone for 10 seconds #from dejavu.recognize import MicrophoneRecognizer #song = djv.recognize(MicrophoneRecognizer, seconds=2) # Or use a recognizer without the shortcut, in anyway you would like #from dejavu.recognize import FileRecognizer #recognizer = FileRecognizer(djv) #song = recognizer.recognize_file("mp3/sail.mp3")
__author__ = 'Mark' from dejavu import Dejavu if __name__ == '__main__': config = { "database": { "host": "127.0.0.1", "user": "******", "passwd": "root", "db": "dejavu" } } djv = Dejavu(config) djv.fingerprint_directory("../../../Desktop/media/dejavu",[".wav",".mp3",".mov",".MOV"]) print djv.db.get_num_fingerprints()
from dejavu import Dejavu from ConfigParser import ConfigParser import warnings warnings.filterwarnings("ignore") # load config config = ConfigParser() config.read("dejavu.cnf") # create Dejavu object dejavu = Dejavu(config) dejavu.fingerprint("va_us_top_40/mp3", "va_us_top_40/wav", [".mp3"], 5) # recognize microphone audio from dejavu.recognize import Recognizer recognizer = Recognizer(dejavu.fingerprinter, config) song = recognizer.read("va_us_top_40/wav/17_-_#Beautiful_-_Mariah_Carey_ft.wav") # recognize song playing over microphone for 10 seconds #song = recognizer.listen(seconds=1, verbose=True) #print song
#with open("dejavu.cnf.SAMPLE") as f: #Directorio de firmas audiopath= "/home/nego/Descargas/firma" extension= ".mp3" audio_paths = get_files_recursive(audiopath, extension) #Obtener longitud de pistas for path in audio_paths: print "Path %s" % path n = get_length_audio(path, extension) print "Length %s "%(n) config={ "database": { "host": "127.0.0.1", "user": "******", "passwd": "ksilva", "db": "dejavu3" }, "fingerprint_limit": [n] } # create a Dejavu instance djv = Dejavu(config) djv.fingerprint_file(path) #djv.fingerprint_directory(audiopath, [extension]) #Comandos #print len(sys.argv) #print str(sys.arv)
import sys, os, shutil sys.path.insert(1, "../dejavu/") from dejavu import Dejavu def copy_all_wavs_to_dir(olddir="./songs", newdir="./fp_wavs"): for yt_id in filter(lambda x: x[0] != ".", os.listdir(olddir)): fp = os.path.join(olddir, yt_id, "song.wav") new_fp = os.path.join(newdir, yt_id + ".wav") print fp, new_fp shutil.copy(fp, new_fp) if "copy" in sys.argv: copy_all_wavs_to_dir() if "train" in sys.argv: config = {"database": {"host": "127.0.0.1", "user": "******", "passwd": "loganlogan", "db": "dejavu"}} djv = Dejavu(config) djv.fingerprint_directory("./fp_wavs", [".wav"], 5)