コード例 #1
0
 public override Task <int> Execute(CommandLineApplication app)
 {
     DrawLongDurationSpectrograms.Execute(this);
     return(this.Ok());
 }
コード例 #2
0
        /// <summary>
        /// HERVE GLOTIN
        /// Combined audio2csv + zooming spectrogram task.
        /// This is used to analyse Herve Glotin's BIRD50 data set.
        /// ############################# IMPORTANT ########################################
        /// In order to analyse the short recordings in BIRD50 dataset, need following change to code:
        /// need to modify    AudioAnalysis.AnalysisPrograms.AcousticIndices.cs #line648
        /// need to change    AnalysisMinSegmentDuration = TimeSpan.FromSeconds(20),
        /// to                AnalysisMinSegmentDuration = TimeSpan.FromSeconds(1),
        /// THIS iS to analyse BIRD50 short recordings.
        /// </summary>
        public static void HiRes1()
        {
            string recordingPath = @"C:\SensorNetworks\WavFiles\TestRecordings\TEST_7min_artificial.wav";

            //// HERVE GLOTIN BIRD50 TRAINING RECORDINGS
            //DirectoryInfo dataDir = new DirectoryInfo(@"D:\SensorNetworks\WavFiles\Glotin\Bird50\AmazonBird50_training_input");
            //string parentDir = @"C:\SensorNetworks\Output\BIRD50";
            //string speciesLabelsFile = parentDir + @"\AmazonBird50_training_output.csv";
            //int speciesCount = 50;
            //////set file name format -depends on train or test. E.g.  "ID0003";
            //string fileStemFormatString = "ID{0:d4}";   // for training files
            //string indexPropertiesConfig = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfigHiRes.yml";
            //string learningMode = "Train";

            //// HERVE GLOTIN BIRD50 TESTING RECORDINGS
            DirectoryInfo dataDir           = new DirectoryInfo(@"D:\SensorNetworks\WavFiles\Glotin\Bird50\AmazonBird50_testing_input");
            string        parentDir         = @"C:\SensorNetworks\Output\BIRD50";
            string        speciesLabelsFile = null;
            int           speciesCount      = 50;
            ////set file name format -depends on train or test. E.g.  "ID0003";
            string fileStemFormatString  = "ID1{0:d3}"; // for testing files
            string indexPropertiesConfig = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfigHiRes.yml";
            string learningMode          = "Test";

            // HERVE GLOTIN BOMBYX WHALE RECORDINGS
            //DirectoryInfo dataDir = new DirectoryInfo(@"C:\SensorNetworks\WavFiles\WhaleFromGlotin");
            //string parentDir = @"C:\SensorNetworks\Output\Glotin\Bombyx_SpermWhales";
            //string speciesLabelsFile = null;
            //int speciesCount = 0;
            //////set file name format -depends on train or test. E.g.  "ID0003";
            //string fileStemFormatString = null;
            ////string fileStemFormatString = "ID1{0:d3}"; // for testing files
            //string indexPropertiesConfig = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfigHiResGianniPavan.yml";
            //string learningMode = "Train";

            // GIANNI PAVAN SASSAFRAS RECORDINGS
            //DirectoryInfo dataDir = new DirectoryInfo(@"C:\SensorNetworks\WavFiles\GianniPavan\SABIOD - TEST SASSOFRATINO");
            //string parentDir = @"C:\SensorNetworks\Output\GianniPavan";
            //string speciesLabelsFile = null;
            //int speciesCount = 0;
            //string fileStemFormatString = null;
            //string indexPropertiesConfig = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfigHiResGianniPavan.yml";
            //string learningMode = "Train";

            // ######################################################################

            string outputDir      = parentDir + @"\" + learningMode;
            string imageOutputDir = parentDir + @"\" + learningMode + "Images";
            string csvDir         = outputDir + @"\Towsey.Acoustic";
            string zoomOutputDir  = outputDir;

            string audio2csvConfigPath = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\Towsey.AcousticHiRes.yml";
            string hiResZoomConfigPath = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\SpectrogramHiResConfig.yml";

            FileInfo[] wavFiles = { new FileInfo(recordingPath) };

            // comment next two lines when debugging a single recording file
            string match = @"*.wav";

            wavFiles = dataDir.GetFiles(match, SearchOption.AllDirectories);

            // READ IN THE SPECIES LABELS FILE AND SET UP THE DATA
            string[] fileID    = new string[wavFiles.Length];
            int[]    speciesID = new int[speciesCount];
            if (speciesLabelsFile != null)
            {
                BirdClefExperiment1.ReadGlotinsSpeciesLabelFile(speciesLabelsFile, wavFiles.Length, out fileID, out speciesID);
            }
            else // make seperate species name for each file
            {
                speciesID = new int[wavFiles.Length];
            }

            //LOOP THROUGH ALL WAV FILES
            //for (int i = 538; i < 539; i++)
            //for (int i = 0; i < 8; i++)
            for (int i = 0; i < wavFiles.Length; i++)
            {
                FileInfo file = wavFiles[i];
                recordingPath = file.FullName;
                string idName = Path.GetFileNameWithoutExtension(file.FullName);
                string name   = string.Format("{0}_Species{1:d2}", idName, speciesID[i]);
                outputDir     = parentDir + @"\" + learningMode + @"\" + name;
                csvDir        = parentDir + @"\" + learningMode + @"\" + name + @"\Towsey.Acoustic";
                zoomOutputDir = outputDir;
                Console.WriteLine("\n\n");
                Console.WriteLine($@">>>>{i}: File<{name}>");

                try
                {
                    // A: analyse the recording files == audio2csv.
                    var audio2csvArguments = new AnalyseLongRecordings.AnalyseLongRecording.Arguments
                    {
                        Source = recordingPath.ToFileInfo(),
                        Config = audio2csvConfigPath,
                        Output = outputDir.ToDirectoryInfo(),
                    };

                    if (!audio2csvArguments.Source.Exists)
                    {
                        LoggedConsole.WriteWarnLine(" >>>>>>>>>>>> WARNING! The Source Recording file cannot be found! This will cause an exception.");
                    }

                    if (!File.Exists(audio2csvArguments.Config))
                    {
                        LoggedConsole.WriteWarnLine(" >>>>>>>>>>>> WARNING! The Configuration file cannot be found! This will cause an exception.");
                    }

                    AnalyseLongRecordings.AnalyseLongRecording.Execute(audio2csvArguments);

                    // B: Concatenate the summary indices and produce images
                    // Use the Zoomingspectrograms action.

                    // need to find out how long the recording is.
                    string        fileName     = audio2csvArguments.Source.BaseName();
                    string        testFileName = fileName + @"__Towsey.Acoustic.ACI.csv";
                    List <string> data         = FileTools.ReadTextFile(Path.Combine(csvDir, testFileName));
                    int           lineCount    = data.Count - 1; // -1 for header.
                    int           imageWidth   = lineCount;

                    //assume scale is index calculation duration = 0.1s
                    // i.e. image resolution  0.1s/px. or 600px/min
                    double focalMinute = (double)lineCount / 600 / 2;
                    if (focalMinute < 0.016666)
                    {
                        focalMinute = 0.016666; // shortest recording = 1 second.
                    }

                    var zoomingArguments = new DrawZoomingSpectrograms.Arguments
                    {
                        // use the default set of index properties in the AnalysisConfig directory.
                        SourceDirectory          = csvDir,
                        Output                   = zoomOutputDir,
                        SpectrogramZoomingConfig = hiResZoomConfigPath,

                        // draw a focused multi-resolution pyramid of images
                        ZoomAction = DrawZoomingSpectrograms.Arguments.ZoomActionType.Focused,
                    };

                    LoggedConsole.WriteLine("# Spectrogram Zooming config  : " + zoomingArguments.SpectrogramZoomingConfig);
                    LoggedConsole.WriteLine("# Input Directory             : " + zoomingArguments.SourceDirectory);
                    LoggedConsole.WriteLine("# Output Directory            : " + zoomingArguments.Output);

                    var common = new ZoomParameters(zoomingArguments.SourceDirectory.ToDirectoryEntry(), zoomingArguments.SpectrogramZoomingConfig.ToFileEntry(), false);

                    // Create directory if not exists
                    if (!Directory.Exists(zoomingArguments.Output))
                    {
                        Directory.CreateDirectory(zoomingArguments.Output);
                    }

                    ZoomFocusedSpectrograms.DrawStackOfZoomedSpectrograms(
                        zoomingArguments.SourceDirectory.ToDirectoryInfo(),
                        zoomingArguments.Output.ToDirectoryInfo(),
                        common,
                        TimeSpan.FromMinutes(focalMinute),
                        imageWidth,
                        AcousticIndices.TowseyAcoustic);

                    // DRAW THE VARIOUS IMAGES
                    string fileStem = fileName;
                    if (fileStemFormatString != null)
                    {
                        fileStem = string.Format(fileStemFormatString, i + 1); // training images
                    }

                    var ldfcSpectrogramArguments = new DrawLongDurationSpectrograms.Arguments
                    {
                        // use the default set of index properties in the AnalysisConfig directory.
                        InputDataDirectory    = csvDir,
                        OutputDirectory       = imageOutputDir,
                        IndexPropertiesConfig = indexPropertiesConfig,
                    };

                    // there are two possible tasks
                    // 1: draw the aggregated grey scale spectrograms
                    int secDuration = DrawLongDurationSpectrograms.DrawAggregatedSpectrograms(ldfcSpectrogramArguments, fileStem);

                    // 2: draw the coloured ridge spectrograms
                    secDuration = DrawLongDurationSpectrograms.DrawRidgeSpectrograms(ldfcSpectrogramArguments, fileStem);

                    // copy files
                    // POW, EVN, SPT, RHZ, RVT, RPS, RNG
                    string[]      copyArray            = { "POW", "EVN", "SPT", "RHZ", "RVT", "RPS", "RNG" };
                    DirectoryInfo sourceDirectory      = new DirectoryInfo(csvDir);
                    string        destinationDirectory = parentDir + @"\TrainingClassifier";
                    foreach (string key in copyArray)
                    {
                        // ID0002__Towsey.Acoustic.BGN.csv    fileName += @"__Towsey.Acoustic.ACI.csv";
                        string sourceFileName        = string.Format(idName + "__Towsey.Acoustic." + key + ".csv");
                        string sourcePath            = Path.Combine(sourceDirectory.FullName, sourceFileName);
                        string nameOfParentDirectory = sourceDirectory.Parent.Name;
                        string destinationFileName   = string.Format(nameOfParentDirectory + "." + key + ".csv");
                        string destinationPath       = Path.Combine(destinationDirectory, destinationFileName);
                        File.Copy(sourcePath, destinationPath, true);
                    }
                } // try block
                catch (Exception e)
                {
                    LoggedConsole.WriteErrorLine(string.Format("ERROR!!!!! RECORDING {0}   FILE {1}", i, name));
                    LoggedConsole.WriteErrorLine(string.Format(e.ToString()));
                }
            } // end loop through all wav files
        }     // HiRes1()
コード例 #3
0
        } // HiRes2() produces spectrogram images

        /// <summary>
        /// This method is very similar to HiRes2().
        /// I have forgotten what the difference in purpose is!!
        /// It is a method used in February-March 2016 to analyse BIRD50 short recordings.
        /// </summary>
        public static void HiRes3()
        {
            string histoDir  = @"C:\SensorNetworks\Output\BIRD50";
            string histoPath = Path.Combine(histoDir, "TrainingRecordingDurations.png");

            //string histoPath = Path.Combine(histoDir, "TestingRecordingDurations.png");
            // set up  histogram of recording durations
            //int histogramWidth = 600; // equivalent to ten minutes at 0.1 second resolution
            int totalRecordingLength = 0;

            // set up IP and OP directories
            string inputDir       = @"C:\SensorNetworks\Output\BIRD50\Training";
            string imageOutputDir = @"C:\SensorNetworks\Output\BIRD50\TrainingImagesTEMP";

            //string imageOutputDir = @"C:\SensorNetworks\Output\BIRD50\TestingRidgeImages";
            string indexPropertiesConfig = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfigHiRes.yml";

            // comment next two lines when debugging a single recording file
            var inputDirInfo = new DirectoryInfo(inputDir);

            //string match = @"*.wav";
            //DirectoryInfo[] directories = inputDirInfo.GetDirectories(match, SearchOption.AllDirectories);
            DirectoryInfo[] directories = inputDirInfo.GetDirectories();
            int             count       = directories.Length;

            //count = 619;

            //string fileStem = "ID0003";      //\ID0001\Towsey.Acoustic\
            string fileStemFormatString = "ID{0:d4}"; // for training files

            //string fileStemFormatString = "ID1{0:d3}"; // for testing files

            for (int i = 0; i < count; i++)
            {
                string fileStem = string.Format(fileStemFormatString, i + 1);
                Console.WriteLine("\n\n");
                Console.WriteLine($@">>>>{i}: File<{fileStem}>");

                string dataDir = directories[i].FullName + @"\Towsey.Acoustic\";

                //string imageOutputDir = inputDir + @"\" + fileStem;

                var ldfcSpectrogramArguments = new DrawLongDurationSpectrograms.Arguments
                {
                    // use the default set of index properties in the AnalysisConfig directory.
                    InputDataDirectory    = dataDir,
                    OutputDirectory       = imageOutputDir,
                    IndexPropertiesConfig = indexPropertiesConfig,
                };

                // there are two possible tasks
                // 1: draw the aggregated grey scale spectrograms
                int rowCount = DrawLongDurationSpectrograms.DrawAggregatedSpectrograms(ldfcSpectrogramArguments, fileStem);

                // 2: draw the coloured ridge spectrograms
                //DrawLongDurationSpectrograms.DrawRidgeSpectrograms(LDFCSpectrogramArguments, fileStem);

                //if (secDuration >= recordingDurations.Length)
                //    secDuration = recordingDurations.Length - 1;
                totalRecordingLength += rowCount;
                Console.WriteLine("Recording length = " + rowCount);
            }

            //string title = "Recording Duration: Width = " + histogramWidth + "secs";
            //Image histoImage = ImageTools.DrawHistogram(title, recordingDurations, 95, null, histogramWidth, 50);
            //histoImage.Save(histoPath);
            Console.WriteLine("\nTotal recording length = " + totalRecordingLength);
            Console.WriteLine("Av recording length = " + (totalRecordingLength / (double)count));
        } // HiRes3() spectrogram images
コード例 #4
0
        }     // HiRes1()

        /// <summary>
        /// HERVE GLOTIN: This is used to analyse the BIRD50 data set.
        /// Draws HIres spectrogram images AFTER indices have been calculated.
        /// This method does NOT produce acoustic indices.
        /// That is, only call this method if hires1() has already been used to produce the indices.
        /// </summary>
        public static void HiRes2()
        {
            string histoDir  = @"C:\SensorNetworks\Output\BIRD50";
            string histoPath = Path.Combine(histoDir, "TrainingRecordingDurations.png");

            //string histoPath = Path.Combine(histoDir, "TestingRecordingDurations.png");
            // set up  histogram of recording durations
            int histogramWidth = 600; // equivalent to ten minutes at 0.1 second resolution

            int[] recordingDurations = new int[histogramWidth];

            // set up IP and OP directories
            string inputDir       = @"C:\SensorNetworks\Output\BIRD50\Training";
            string imageOutputDir = @"C:\SensorNetworks\Output\BIRD50\TrainingImages";

            //string imageOutputDir = @"C:\SensorNetworks\Output\BIRD50\TestingRidgeImages";
            string indexPropertiesConfig = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfigHiRes.yml";

            // comment next two lines when debugging a single recording file
            var inputDirInfo = new DirectoryInfo(inputDir);

            //string match = @"*.wav";
            //DirectoryInfo[] directories = inputDirInfo.GetDirectories(match, SearchOption.AllDirectories);
            DirectoryInfo[] directories = inputDirInfo.GetDirectories();
            int             count       = directories.Length;

            //count = 3;

            //string fileStem = "ID0003";      //\ID0001\Towsey.Acoustic\
            string fileStemFormatString = "ID{0:d4}"; // for training files

            //string fileStemFormatString = "ID1{0:d3}"; // for testing files

            for (int i = 0; i < count; i++)
            {
                string fileStem = string.Format(fileStemFormatString, i + 1);
                string dataDir  = directories[i].FullName + @"\Towsey.Acoustic\";

                var ldfcSpectrogramArguments = new DrawLongDurationSpectrograms.Arguments
                {
                    // use the default set of index properties in the AnalysisConfig directory.
                    InputDataDirectory    = dataDir,
                    OutputDirectory       = imageOutputDir,
                    IndexPropertiesConfig = indexPropertiesConfig,
                };

                // there are two possible tasks
                // 1: draw the aggregated grey scale spectrograms
                int secDuration = DrawLongDurationSpectrograms.DrawAggregatedSpectrograms(ldfcSpectrogramArguments, fileStem);

                // 2: draw the coloured ridge spectrograms
                DrawLongDurationSpectrograms.DrawRidgeSpectrograms(ldfcSpectrogramArguments, fileStem);

                if (secDuration >= recordingDurations.Length)
                {
                    secDuration = recordingDurations.Length - 1;
                }

                recordingDurations[secDuration]++;
            }

            string title      = "Recording Duration: Width = " + histogramWidth + "secs";
            Image  histoImage = GraphsAndCharts.DrawHistogram(title, recordingDurations, 95, null, histogramWidth, 50);

            histoImage.Save(histoPath);
        } // HiRes2() produces spectrogram images