dejavu/example.py

38 lines
1.2 KiB
Python
Raw Normal View History

import json
import warnings
2013-11-19 02:51:27 +00:00
2015-03-28 23:23:18 +00:00
from dejavu import Dejavu
from dejavu.recognize import FileRecognizer, MicrophoneRecognizer
warnings.filterwarnings("ignore")
# load config from a JSON file (or anything outputting a python dictionary)
with open("dejavu.cnf.SAMPLE") as f:
config = json.load(f)
2015-03-28 23:23:18 +00:00
2015-03-22 22:50:42 +00:00
if __name__ == '__main__':
2013-11-19 02:51:27 +00:00
# create a Dejavu instance
djv = Dejavu(config)
# Fingerprint all the mp3's in the directory we give it
djv.fingerprint_directory("mp3", [".mp3"])
2013-11-19 02:51:27 +00:00
# Recognize audio from a file
song = djv.recognize(FileRecognizer, "mp3/Sean-Fournier--Falling-For-You.mp3")
print(f"From file we recognized: {song}\n")
2013-11-19 02:51:27 +00:00
# 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(f"From mic with %d seconds we recognized: {(secs, song)}\n")
# Or use a recognizer without the shortcut, in anyway you would like
recognizer = FileRecognizer(djv)
song = recognizer.recognize_file("mp3/Josh-Woodward--I-Want-To-Destroy-Something-Beautiful.mp3")
print(f"No shortcut, we recognized: {song}\n")