Skip to content

Commit

Permalink
Create KoalaV3.cs
Browse files Browse the repository at this point in the history
Issue #297 Start Koala recognizer version 3.
  • Loading branch information
towsey committed May 2, 2020
1 parent e79f6e2 commit aec2584
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions src/AnalysisPrograms/Recognizers/KoalaV3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// <copyright file="KoalaV3.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace AnalysisPrograms.Recognizers
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Acoustics.Shared.ConfigFile;
using AnalysisBase;
using AnalysisPrograms.Recognizers.Base;
using AudioAnalysisTools;
using AudioAnalysisTools.DSP;
using AudioAnalysisTools.Events;
using AudioAnalysisTools.Events.Types;
using AudioAnalysisTools.Indices;
using AudioAnalysisTools.StandardSpectrograms;
using AudioAnalysisTools.WavTools;
using log4net;
using SixLabors.ImageSharp;
using TowseyLibrary;
using static AnalysisPrograms.Recognizers.GenericRecognizer;
using Path = System.IO.Path;

/// <summary>
/// A recognizer for the Australian Boobook Owl, /// https://en.wikipedia.org/wiki/Australian_boobook .
/// </summary>
internal class KoalaV3 : RecognizerBase
{
private static readonly ILog BoobookLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

public override string Author => "Towsey";

public override string SpeciesName => "PhascolarctosCinereus";

public override string Description => "[ALPHA] Detects acoustic events for the Koala.";

public override AnalyzerConfig ParseConfig(FileInfo file)
{
RuntimeHelpers.RunClassConstructor(typeof(KoalaConfig).TypeHandle);
var config = ConfigFile.Deserialize<KoalaConfig>(file);

// validation of configs can be done here
GenericRecognizer.ValidateProfileTagsMatchAlgorithms(config.Profiles, file);

// This call sets a restriction so that only one generic algorithm is used.
// CHANGE this to accept multiple generic algorithms as required.
//if (result.Profiles.SingleOrDefault() is ForwardTrackParameters)
if (config.Profiles?.Count == 1 && config.Profiles.First().Value is ForwardTrackParameters)
{
return config;
}

throw new ConfigFileException("Koala expects one and only one ForwardTrack algorithm.", file);
}

/// <summary>
/// This method is called once per segment (typically one-minute segments).
/// </summary>
/// <param name="audioRecording">one minute of audio recording.</param>
/// <param name="config">config file that contains parameters used by all profiles.</param>
/// <param name="segmentStartOffset">when recording starts.</param>
/// <param name="getSpectralIndexes">not sure what this is.</param>
/// <param name="outputDirectory">where the recognizer results can be found.</param>
/// <param name="imageWidth"> assuming ????.</param>
/// <returns>recognizer results.</returns>
public override RecognizerResults Recognize(
AudioRecording audioRecording,
Config config,
TimeSpan segmentStartOffset,
Lazy<IndexCalculateResult[]> getSpectralIndexes,
DirectoryInfo outputDirectory,
int? imageWidth)
{
//class NinoxBoobookConfig is define at bottom of this file.
var genericConfig = (KoalaConfig)config;
var recognizer = new GenericRecognizer();

RecognizerResults combinedResults = recognizer.Recognize(
audioRecording,
genericConfig,
segmentStartOffset,
getSpectralIndexes,
outputDirectory,
imageWidth);

// DO POST-PROCESSING of EVENTS

// Convert events to spectral events for possible combining.
// Combine overlapping events. If the dB threshold is set low, may get lots of little events.
var events = combinedResults.NewEvents;
var spectralEvents = events.Select(x => (SpectralEvent)x).ToList();
var newEvents = CompositeEvent.CombineOverlappingEvents(spectralEvents);

if (genericConfig.CombinePossibleSyllableSequence)
{
// convert events to spectral events for possible combining.
//var spectralEvents = events.Select(x => (SpectralEvent)x).ToList();
spectralEvents = newEvents.Cast<SpectralEvent>().ToList();
var startDiff = genericConfig.SyllableStartDifference;
var hertzDiff = genericConfig.SyllableHertzGap;
newEvents = CompositeEvent.CombineSimilarProximalEvents(spectralEvents, TimeSpan.FromSeconds(startDiff), (int)hertzDiff);
}

combinedResults.NewEvents = newEvents;

//UNCOMMENT following line if you want special debug spectrogram, i.e. with special plots.
// NOTE: Standard spectrograms are produced by setting SaveSonogramImages: "True" or "WhenEventsDetected" in <Towsey.PteropusSpecies.yml> config file.
//GenericRecognizer.SaveDebugSpectrogram(territorialResults, genericConfig, outputDirectory, audioRecording.BaseName);
return combinedResults;
}

/*
/// <summary>
/// Summarize your results. This method is invoked exactly once per original file.
/// </summary>
public override void SummariseResults(
AnalysisSettings settings,
FileSegment inputFileSegment,
EventBase[] events,
SummaryIndexBase[] indices,
SpectralIndexBase[] spectralIndices,
AnalysisResult2[] results)
{
// No operation - do nothing. Feel free to add your own logic.
base.SummariseResults(settings, inputFileSegment, events, indices, spectralIndices, results);
}
*/

/// <inheritdoc cref="KoalaConfig"/> />
public class KoalaConfig : GenericRecognizerConfig, INamedProfiles<object>
{
public bool CombinePossibleSyllableSequence { get; set; } = false;

public double SyllableStartDifference { get; set; } = 0.5;

public double SyllableHertzGap { get; set; } = 200;
}
}
}

0 comments on commit aec2584

Please sign in to comment.