gpt4 book ai didi

python - 使用用于音频指纹识别的 Dejavu 库识别文件夹中所有文件的循环

转载 作者:行者123 更新时间:2023-11-28 17:41:19 26 4
gpt4 key购买 nike

有没有人有使用 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 模块来完成,但由于我是新手,不知道该循环将它放在哪里,所以我没有得到识别的确切文件提示!

最佳答案

不只是在与 dejavu 文件夹相同的目录中创建另一个 .py 文件。不要更改库代码。

你可能想做这样的事情:

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)

希望这就是您的意图。

关于python - 使用用于音频指纹识别的 Dejavu 库识别文件夹中所有文件的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com