有没有人有使用Dejavu库进行音频指纹识别和Python识别的经验?它工作正常,但我感兴趣的是,到目前为止,我只能使用以下方法识别一个文件:
print djv.recognize(FileRecognizer, 'path/song_name.mp3')
任何人都知道如何以及在何处创建一个循环,从上面为文件夹中的所有文件执行该打印命令?我认为循环应该在这里创建:
https://github.com/worldveil/dejavu/blob/master/dejavu/recognize.py
import dejavu.fingerprint as fingerprint
import dejavu.decoder as decoder
import numpy as np
import pyaudio
import time
class BaseRecognizer(object):
def __init__(self, dejavu):
self.dejavu = dejavu
self.Fs = fingerprint.DEFAULT_FS
def _recognize(self, *data):
matches = []
for d in data:
matches.extend(self.dejavu.find_matches(d, Fs=self.Fs))
return self.dejavu.align_matches(matches)
def recognize(self):
pass # base class does nothing
class FileRecognizer(BaseRecognizer):
def __init__(self, dejavu):
super(FileRecognizer, self).__init__(dejavu)
def recognize_file(self, filename):
frames, self.Fs = decoder.read(filename, self.dejavu.limit)
t = time.time()
match = self._recognize(*frames)
t = time.time() - t
if match:
match['match_time'] = t
return match
def recognize(self, filename):
return self.recognize_file(filename)
我知道应该使用glob模块来完成,但是因为我是新手,不知道在哪个环节放置它,所以我没有得到提示用于识别的确切文件!
答案 0 :(得分:2)
不只是在与.py
文件夹相同的目录中创建另一个dejavu
文件。不要改变库代码。
你可能想做这样的事情:
from dejavu import Dejavu
from dejavu.recognize import FileRecognizer
import os, fnmatch
def find_files(directory, pattern):
"""http://stackoverflow.com/a/2186673/712997"""
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
config = {
"database": {
"host": "127.0.0.1",
"user": "root",
"passwd": "Password123",
"db": "dejavu_db",
},
"database_type" : "mysql",
"fingerprint_limit" : 10
}
# create a dejavu object
djv = Dejavu(config)
# gather files to fingerprint
UNLABELED_AUDIO_DIR = "/home/me/music/unknown/"
PATTERN = "*.mp3"
audio_paths = find_files(UNLABELED_AUDIO_DIR, PATTERN)
# recognize them one at a time
original_file_to_song = {}
for path in audio_paths:
print "Attempting to recognize %s..." % path
song = djv.recognize(FileRecognizer, path)
original_file_to_song[path] = song
# see the songs you've recognized
for path, song in original_file_to_song.iteritems():
print "Audio file at: %s was recognized as %s" % (path, song)
希望这就是你的意图。