ganesh333's picture
Upload folder using huggingface_hub
575e16b verified
Raw History Blame Contribute Delete
1.27 kB
import argparse, json, sys
from pathlib import Path
from src.inference import predict_wav
ROOT=Path(__file__).resolve().parent
def resolve(p):
"""Use the path as given if it exists, else relative to this project folder (so the script works from any cwd)."""
p=Path(p)
return p if p.exists() or p.is_absolute() else ROOT/p
p=argparse.ArgumentParser(description='Run the local experimental Kernel insect classifier')
p.add_argument('wav'); p.add_argument('--model-dir',default='.')
p.add_argument('--device',default='cpu')
a=p.parse_args()
wav=Path(a.wav); model_dir=resolve(a.model_dir)
if not wav.is_file(): sys.exit(f"Audio file not found: {wav.resolve()}\nPass the path to a real recording (.wav, or .flac/.ogg/.mp3 with soundfile installed), e.g. python predict.py C:/path/to/clip.wav --model-dir full_model")
if not (model_dir/'model.h5').is_file(): sys.exit(f"No model.h5 in {model_dir}. Use --model-dir . (baseline), full_model or finetuned_cnn.")
try:
print(json.dumps(predict_wav(wav,model_dir,a.device),indent=2))
except (ValueError,RuntimeError) as e:
# soundfile raises LibsndfileError (a RuntimeError) for unreadable files.
sys.exit(f"Could not read {wav} as audio ({e}). Convert it first, e.g. ffmpeg -i input.mp3 -ac 1 output.wav")