Esempio n. 1
0
    def run_ipusegmentation(self, stepidx):
        """
        Execute the SPPAS-IPUSegmentation program.

        @return number of files processed successfully

        """
        # Initializations
        step = self.parameters.get_step(stepidx)
        stepname = self.parameters.get_step_name(stepidx)
        files_processed_success = 0
        self._progress.set_header(stepname)
        self._progress.update(0,"")

        # Get the list of input file names, with the ".wav" extension
        filelist = self.set_filelist(".wav")
        if len(filelist) == 0:
            return 0
        total = len(filelist)

        # Create annotation instance, and fix options
        try:
            seg = sppasSeg(self._logfile)
        except Exception as e:
            if self._logfile is not None:
                self._logfile.print_message( "%s\n"%str(e), indent=1,status=1 )
            return 0

        # Execute the annotation for each file in the list
        for i,f in enumerate(filelist):

            # fix the default values
            seg.fix_options( step.get_options() )

            # Indicate the file to be processed
            if self._logfile is not None:
                self._logfile.print_message(stepname+" of file "+f, indent=1 )
            self._progress.set_text( os.path.basename(f)+" ("+str(i+1)+"/"+str(total)+")" )

            # Fix input/output file name
            outname = os.path.splitext(f)[0] + self.parameters.get_output_format()

            # Is there already an existing IPU-seg (in any format)!
            existoutname = self._get_filename(f, ['.xra', '.TextGrid', '.eaf', '.csv', '.mrk' ])

            # it's existing... but not in the expected format: convert!
            if existoutname is not None and existoutname != outname:
                # just copy the file!
                if self._logfile is not None:
                    self._logfile.print_message('Export '+existoutname, indent=2)
                    self._logfile.print_message('into '+outname, indent=2)
                try:
                    t = annotationdata.io.read(existoutname)
                    annotationdata.io.write(outname,t)
                    # OK, now outname is as expected! (or not...)
                except Exception:
                    pass

            # Execute annotation
            tgfname = utils.fileutils.exists(outname)
            if tgfname is None:
                # No already existing IPU seg., but perhaps a txt.
                txtfile = self._get_filename(f, [".txt"])
                if self._logfile is not None:
                    if txtfile:
                        self._logfile.print_message("A transcription was found, perform Silence/Speech segmentation time-aligned with a transcription %s"%txtfile, indent=2,status=3)
                    else:
                        self._logfile.print_message("No transcription was found, perform Silence/Speech segmentation only.", indent=2,status=3)
                try:
                    seg.run(f, trsinputfile=txtfile, ntracks=None, diroutput=None, tracksext=None, listoutput=None, textgridoutput=outname)
                    files_processed_success += 1
                    if self._logfile is not None:
                        self._logfile.print_message(outname, indent=2,status=0)
                except Exception as e:
                    if self._logfile is not None:
                        self._logfile.print_message( "%s for file %s\n"%(str(e),outname), indent=2,status=-1 )
            else:
                if seg.get_dirtracks() is True:
                    self._logfile.print_message("A time-aligned transcription was found, split into multiple files", indent=2,status=3)
                    try:
                        seg.run(f, trsinputfile=tgfname, ntracks=None, diroutput=None, tracksext=None, listoutput=None, textgridoutput=None)
                        files_processed_success += 1
                        if self._logfile is not None:
                            self._logfile.print_message(tgfname, indent=2,status=0 )
                    except Exception as e:
                        if self._logfile is not None:
                            self._logfile.print_message( "%s for file %s\n"%(str(e),tgfname), indent=2,status=-1 )
                else:
                    if self._logfile is not None:
                        self._logfile.print_message( "because a previous segmentation is existing.", indent=2,status=2 )

            # Indicate progress
            self._progress.set_fraction(float((i+1))/float(total))
            if self._logfile is not None:
                self._logfile.print_newline()

        # Indicate completed!
        self._progress.update(1,"Completed (%d files successfully over %d files).\n"%(files_processed_success,total))
        self._progress.set_header("")

        return files_processed_success
Esempio n. 2
0
    sys.exit(1)

if args.t and args.e and args.t.lower().endswith('.txt'):
    print "You can NOT use both -e with -t option if file is not time-aligned!\nUse option -h for any help."
    sys.exit(1)



# ----------------------------------------------------------------------------
# Automatic IPUs segmentation is here:
# ----------------------------------------------------------------------------

if args.o and not os.path.exists(args.o):
    os.mkdir(args.o)

w = sppasSeg()

if args.shift:  w.set_shift( args.shift )
if args.minvol: w.set_min_volume( args.minvol )
if args.minsil: w.set_min_silence( args.minsil )
if args.minrms: w.set_min_speech( args.minrms )
if args.o: w.set_dirtracks( True )
if args.p: w.set_save_as_trs( True )
if args.e: w.set_save_as_trs( True )

w.run(args.w, args.t, args.n, args.N, args.o, args.e, args.l, args.p)

#     - wavfile is the wav input file name
#     - trsinputfile is a transcription (or 'None')
#     - trstieridx is the tier index with the transcription
#     - ntracks