-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
28 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
# TODO Add machine listening help functionality to easily categorize downloaded audio. | ||
"""Machine listening help functionality to easily categorize downloaded audio.""" | ||
import logging | ||
from mediapipe.tasks.python import BaseOptions, audio | ||
from mediapipe.tasks.python.components.containers import AudioData | ||
import importlib.resources | ||
import soundfile as sf | ||
|
||
MODULE_PATH = importlib.resources.files(__package__) | ||
OPTIONS = audio.AudioClassifierOptions(base_options=BaseOptions(model_asset_path=MODULE_PATH / "yamnet.tflite")) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def classify(audio_path) -> audio.AudioClassifierResult: | ||
with audio.AudioClassifier.create_from_options(OPTIONS) as f: | ||
logger.info(f"Classifying {audio_path}") | ||
audio_data = AudioData.create_from_array(*sf.read(audio_path)) | ||
predictions = f.classify(audio_data) | ||
logger.debug(predictions) | ||
# TODO Average predictions over time. | ||
# TODO Use more tags than just music. | ||
is_music = predictions[0].classifications[0].categories[0].category_name == "Music" | ||
logger.info(f"Classified {audio_path} as music: {is_music}") | ||
return predictions |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import audioscrape.classify as classify | ||
|
||
|
||
def test_classify(): | ||
results = classify.classify("tests/example.mp3") |