def transcribe_corpus(args): command = 'transcribe' all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) if args.config_path: transcribe_config = transcribe_yaml_to_config(args.config_path) else: transcribe_config = load_basic_transcribe() data_directory = os.path.join(temp_dir, corpus_name) if getattr(args, 'clean', False) and os.path.exists(data_directory): print('Cleaning old directory!') shutil.rmtree(data_directory, ignore_errors=True) logger = setup_logger(command, data_directory) os.makedirs(data_directory, exist_ok=True) os.makedirs(args.output_directory, exist_ok=True) os.makedirs(data_directory, exist_ok=True) conf_path = os.path.join(data_directory, 'config.yml') if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = { 'dirty': False, 'begin': time.time(), 'version': __version__, 'type': 'transcribe', 'corpus_directory': args.corpus_directory, 'dictionary_path': args.dictionary_path, 'acoustic_model_path': args.acoustic_model_path, 'language_model_path': args.language_model_path, } if conf['dirty'] or conf['type'] != command \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__ \ or conf['dictionary_path'] != args.dictionary_path \ or conf['language_model_path'] != args.language_model_path \ or conf['acoustic_model_path'] != args.acoustic_model_path: logger.warning( 'WARNING: Using old temp directory, this might not be ideal for you, use the --clean flag to ensure no ' 'weird behavior for previous versions of the temporary directory.') if conf['dirty']: logger.debug('Previous run ended in an error (maybe ctrl-c?)') if conf['type'] != command: logger.debug( 'Previous run was a different subcommand than {} (was {})'. format(command, conf['type'])) if conf['corpus_directory'] != args.corpus_directory: logger.debug('Previous run used source directory ' 'path {} (new run: {})'.format( conf['corpus_directory'], args.corpus_directory)) if conf['version'] != __version__: logger.debug('Previous run was on {} version (new run: {})'.format( conf['version'], __version__)) if conf['dictionary_path'] != args.dictionary_path: logger.debug('Previous run used dictionary path {} ' '(new run: {})'.format(conf['dictionary_path'], args.dictionary_path)) if conf['acoustic_model_path'] != args.acoustic_model_path: logger.debug('Previous run used acoustic model path {} ' '(new run: {})'.format(conf['acoustic_model_path'], args.acoustic_model_path)) if conf['language_model_path'] != args.language_model_path: logger.debug('Previous run used language model path {} ' '(new run: {})'.format(conf['language_model_path'], args.language_model_path)) try: if args.evaluate: corpus = AlignableCorpus( args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs, use_mp=transcribe_config.use_mp) else: corpus = TranscribeCorpus( args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs, use_mp=transcribe_config.use_mp) print(corpus.speaker_utterance_info()) acoustic_model = AcousticModel(args.acoustic_model_path, root_directory=data_directory) language_model = LanguageModel(args.language_model_path, root_directory=data_directory) dictionary = Dictionary(args.dictionary_path, data_directory) acoustic_model.validate(dictionary) begin = time.time() t = Transcriber(corpus, dictionary, acoustic_model, language_model, transcribe_config, temp_directory=data_directory, debug=getattr(args, 'debug', False), evaluation_mode=args.evaluate) if args.debug: print('Setup pretrained aligner in {} seconds'.format(time.time() - begin)) begin = time.time() t.transcribe() if args.debug: print('Performed transcribing in {} seconds'.format(time.time() - begin)) if args.evaluate: t.evaluate(args.output_directory) best_config_path = os.path.join(args.output_directory, 'best_transcribe_config.yaml') save_config(t.transcribe_config, best_config_path) t.export_transcriptions(args.output_directory) else: begin = time.time() t.export_transcriptions(args.output_directory) if args.debug: print('Exported transcriptions in {} seconds'.format( time.time() - begin)) print('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as _: conf['dirty'] = True raise finally: handlers = logger.handlers[:] for handler in handlers: handler.close() logger.removeHandler(handler) if os.path.exists(data_directory): with open(conf_path, 'w') as f: yaml.dump(conf, f)
def transcribe_corpus(args): all_begin = time.time() if not args.temp_directory: temp_dir = TEMP_DIR else: temp_dir = os.path.expanduser(args.temp_directory) corpus_name = os.path.basename(args.corpus_directory) if corpus_name == '': args.corpus_directory = os.path.dirname(args.corpus_directory) corpus_name = os.path.basename(args.corpus_directory) data_directory = os.path.join(temp_dir, corpus_name) print(data_directory, os.path.exists(data_directory)) os.makedirs(data_directory, exist_ok=True) os.makedirs(args.output_directory, exist_ok=True) os.makedirs(data_directory, exist_ok=True) conf_path = os.path.join(data_directory, 'config.yml') if os.path.exists(conf_path): with open(conf_path, 'r') as f: conf = yaml.load(f, Loader=yaml.SafeLoader) else: conf = { 'dirty': False, 'begin': time.time(), 'version': __version__, 'type': 'align', 'corpus_directory': args.corpus_directory, 'dictionary_path': args.dictionary_path } if getattr(args, 'clean', False) \ or conf['dirty'] or conf['type'] != 'align' \ or conf['corpus_directory'] != args.corpus_directory \ or conf['version'] != __version__ \ or conf['dictionary_path'] != args.dictionary_path: pass # FIXME # shutil.rmtree(data_directory, ignore_errors=True) try: if args.evaluate: corpus = AlignableCorpus( args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs) else: corpus = TranscribeCorpus( args.corpus_directory, data_directory, speaker_characters=args.speaker_characters, num_jobs=args.num_jobs) print(corpus.speaker_utterance_info()) acoustic_model = AcousticModel(args.acoustic_model_path) language_model = LanguageModel(args.language_model_path) dictionary = Dictionary(args.dictionary_path, data_directory) acoustic_model.validate(dictionary) if args.config_path: transcribe_config = transcribe_yaml_to_config(args.config_path) else: transcribe_config = load_basic_transcribe() begin = time.time() t = Transcriber(corpus, dictionary, acoustic_model, language_model, transcribe_config, temp_directory=data_directory, debug=getattr(args, 'debug', False), evaluation_mode=args.evaluate) if args.debug: print('Setup pretrained aligner in {} seconds'.format(time.time() - begin)) begin = time.time() t.transcribe() if args.debug: print('Performed transcribing in {} seconds'.format(time.time() - begin)) if args.evaluate: t.evaluate(args.output_directory) best_config_path = os.path.join(args.output_directory, 'best_transcribe_config.yaml') save_config(t.transcribe_config, best_config_path) t.export_transcriptions(args.output_directory) else: begin = time.time() t.export_transcriptions(args.output_directory) if args.debug: print('Exported transcriptions in {} seconds'.format( time.time() - begin)) print('Done! Everything took {} seconds'.format(time.time() - all_begin)) except Exception as _: conf['dirty'] = True raise finally: if os.path.exists(data_directory): with open(conf_path, 'w') as f: yaml.dump(conf, f)