/// <summary> /// Initializes a new instance of the <see cref="ParallelFixedLength{TIn, TOut}"/> class. /// </summary> /// <param name="pipeline">The pipeline to add the component to.</param> /// <param name="vectorSize">Vector size.</param> /// <param name="transform">Function mapping keyed input producers to output producers.</param> /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param> /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param> /// <param name="name">Name for this component (defaults to ParallelFixedLength).</param> /// <param name="defaultDeliveryPolicy">Pipeline-level default delivery policy to be used by this component (defaults to <see cref="DeliveryPolicy.Unlimited"/> if unspecified).</param> public ParallelFixedLength(Pipeline pipeline, int vectorSize, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped, TOut defaultValue = default, string name = null, DeliveryPolicy defaultDeliveryPolicy = null) : base(pipeline, name ?? nameof(ParallelFixedLength <TIn, TOut>), defaultDeliveryPolicy) { this.inConnector = this.CreateInputConnectorFrom <TIn[]>(pipeline, nameof(this.inConnector)); this.splitter = this.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.splitter)); this.inConnector.PipeTo(this.splitter); this.branches = new Emitter <TIn> [vectorSize]; var branchResults = new IProducer <TOut> [vectorSize]; for (int i = 0; i < vectorSize; i++) { var subpipeline = Subpipeline.Create(this, $"subpipeline{i}"); var connectorIn = new Connector <TIn>(this, subpipeline, $"connectorIn{i}"); var connectorOut = new Connector <TOut>(subpipeline, this, $"connectorOut{i}"); this.branches[i] = this.CreateEmitter <TIn>(this, $"branch{i}"); this.branches[i].PipeTo(connectorIn); transform(i, connectorIn.Out).PipeTo(connectorOut.In); branchResults[i] = connectorOut; } var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>(); this.join = Operators.Join(branchResults, interpolator); this.outConnector = this.CreateOutputConnectorTo <TOut[]>(pipeline, nameof(this.outConnector)); this.join.PipeTo(this.outConnector); }
private void InitializePipeline() { pipeline = Pipeline.Create(); var webcamConfig = MediaCaptureConfiguration.Default; webcamConfig.DeviceId = WebcamSymbolicLink; webcamConfig.Width = WebcamWidth; webcamConfig.Height = WebcamHeight; webcamConfig.Framerate = 30; webcamConfig.UseInSharedMode = true; var source = new MediaCapture(pipeline, webcamConfig); var flip = new FlipColorVideo(pipeline) { FlipHorizontal = FlipX, FlipVertical = FlipY }; source.PipeTo(flip.In, DeliveryPolicy.SynchronousOrThrottle); var openface = new OpenFace(pipeline) { CameraCalibFx = WebcamFx, CameraCalibFy = WebcamFy, CameraCalibCx = WebcamCx, CameraCalibCy = WebcamCy }; flip.PipeTo(openface.In, DeliveryPolicy.SynchronousOrThrottle); generator = new DisplayCoordianteGenerator(pipeline); var record = openface.Out.Join(generator.Out, Reproducible.Nearest <Vector2>(), (gp, display) => new GazeToDisplayCoordinateMappingRecord(gp, display), DeliveryPolicy.SynchronousOrThrottle, DeliveryPolicy.SynchronousOrThrottle); record.Do((d, e) => { AddRecord(d, e); }, DeliveryPolicy.SynchronousOrThrottle); }
private void InitializePipeline() { pipeline = Pipeline.Create(); var webcamConfig = MediaCaptureConfiguration.Default; webcamConfig.DeviceId = WebcamSymbolicLink; webcamConfig.Width = WebcamWidth; webcamConfig.Height = WebcamHeight; webcamConfig.Framerate = 30; webcamConfig.UseInSharedMode = true; var source = new MediaCapture(pipeline, webcamConfig); var flip = new FlipColorVideo(pipeline) { FlipHorizontal = FlipX, FlipVertical = FlipY }; source.PipeTo(flip.In, DeliveryPolicy.LatestMessage); var openface = new OpenFace(pipeline) { CameraCalibFx = WebcamFx, CameraCalibFy = WebcamFy, CameraCalibCx = WebcamCx, CameraCalibCy = WebcamCy }; flip.PipeTo(openface.In, DeliveryPolicy.LatestMessage); var dispFlip = new FlipColorVideo(pipeline) { FlipHorizontal = !FlipX, FlipVertical = FlipY }; // mirror display source.PipeTo(dispFlip.In, DeliveryPolicy.LatestMessage); var joinedVideoFrame = openface.Out.Join(dispFlip.Out, Reproducible.Exact <Shared <Microsoft.Psi.Imaging.Image> >(), (dataPoint, frame) => new Tuple <PoseAndEyeAndFace, Shared <Microsoft.Psi.Imaging.Image> >(dataPoint, frame), DeliveryPolicy.LatestMessage, DeliveryPolicy.LatestMessage); joinedVideoFrame.Do(UpdateDisplay, DeliveryPolicy.LatestMessage); pipeline.RunAsync(); }
public OpenFaceVisualizer(Pipeline pipeline) : base(pipeline) { DataInConnector = CreateInputConnectorFrom <HeadPoseAndGaze>(pipeline, nameof(DataIn)); ImageInConnector = CreateInputConnectorFrom <Shared <Image> >(pipeline, nameof(ImageIn)); Out = pipeline.CreateEmitter <Shared <Image> >(this, nameof(Out)); var joined = DataInConnector.Out.Join(ImageInConnector.Out, Reproducible.Exact <Shared <Image> >()); joined.Do(Process); pipeline.PipelineCompleted += OnPipelineCompleted; display.PropertyChanged += (sender, e) => { if (e.PropertyName == nameof(display.VideoImage)) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Image))); } }; display.ReceivedFrames.PropertyChanged += (sender, e) => { if (e.PropertyName == nameof(display.RenderedFrames.Rate)) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FrameRate))); } }; }
public void Image_CropViaJoinOperator() { // Test that the pipeline's operator Crop() works on a stream of images and random rectangles using (var pipeline = Pipeline.Create("CropViaOperator")) { using (var sharedImage = ImagePool.GetOrCreate(this.testImage.Width, this.testImage.Height, this.testImage.PixelFormat)) { this.testImage.CopyTo(sharedImage.Resource); // Use a non-insignificant interval for both Sequences to ensure that the Join processes all // messages from both streams (default interval of 1-tick is too small to guarantee this). var images = Generators.Sequence(pipeline, sharedImage, x => sharedImage, 100, TimeSpan.FromMilliseconds(1)); var rects = Generators.Sequence( pipeline, new System.Drawing.Rectangle(0, 0, 1, 1), x => { Random r = new Random(); System.Drawing.Rectangle rect = default(System.Drawing.Rectangle); rect.X = r.Next(0, this.testImage.Width); rect.Y = r.Next(0, this.testImage.Height); rect.Width = r.Next(1, this.testImage.Width - rect.X); rect.Height = r.Next(1, this.testImage.Height - rect.Y); return(rect); }, 100, TimeSpan.FromMilliseconds(1)); images.Join(rects, Reproducible.Nearest <System.Drawing.Rectangle>()).Crop(); pipeline.Run(); } } }
public DeepSpeechRecognizer(Pipeline pipeline) : base(pipeline, nameof(DeepSpeechRecognizer)) { // Create connector this.audioIn = this.CreateInputConnectorFrom <AudioBuffer>(pipeline, nameof(this.AudioIn)); // Define the outputs var textOut = this.CreateOutputConnectorTo <String>(pipeline, nameof(this.TextOut)); this.TextOut = textOut.Out; // sub-recognizer var recognizer = new DeepSpeechSubRecognizer(this); // voice activity detector var voiceDet = new SystemVoiceActivityDetector(this); this.audioIn.Out.PipeTo(voiceDet); //this.audioIn.Do(x => //{ // Console.Write('.'); //}); var voiceAudio = this.audioIn.Out.Join(voiceDet.Out, Reproducible.Nearest <bool>(RelativeTimeInterval.Future())); voiceAudio.PipeTo(recognizer.In); recognizer.PipeTo(textOut); }
public void SparseJoin() { var results = new List <Dictionary <string, int> >(); using (var p = Pipeline.Create()) { var sourceA = Generators.Sequence(p, 100, i => i + 1, 30, TimeSpan.FromTicks(10)); var sourceB = Generators.Sequence(p, 100, i => i + 1, 10, TimeSpan.FromTicks(30)); var sourceC = Generators.Sequence(p, 100, i => i + 1, 3, TimeSpan.FromTicks(100)); var keyMapping = sourceA.Select(i => (i % 10 != 0) ? new Dictionary <string, int> { { "zero", 0 }, { "one", 1 } } : new Dictionary <string, int> { { "zero", 0 }, { "two", 2 } }); Operators .Join(keyMapping, new[] { sourceA, sourceB, sourceC }, Reproducible.Nearest <int>(TimeSpan.FromTicks(5))) .Do(t => results.Add(t.DeepClone())); p.Run(new ReplayDescriptor(DateTime.Now, DateTime.MaxValue)); } Assert.AreEqual(12, results.Count); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 100 }, { "two", 100 } }, results[0]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 103 }, { "one", 101 } }, results[1]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 106 }, { "one", 102 } }, results[2]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 109 }, { "one", 103 } }, results[3]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 110 }, { "two", 101 } }, results[4]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 112 }, { "one", 104 } }, results[5]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 115 }, { "one", 105 } }, results[6]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 118 }, { "one", 106 } }, results[7]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 120 }, { "two", 102 } }, results[8]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 121 }, { "one", 107 } }, results[9]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 124 }, { "one", 108 } }, results[10]); CollectionAssert.AreEqual(new Dictionary <string, int> { { "zero", 127 }, { "one", 109 } }, results[11]); }
/// <summary> /// Samples the input stream and applies a high-pass filter (using MathNet.Filtering) over the sampled input stream. Removes low frequencies. /// </summary> /// <param name="input">The input source of doubles.</param> /// <param name="sampleRate">The desired sample rate for the filter.</param> /// <param name="cutoffRate">The desired cutoff for the filter.</param> /// <param name="impulseResponse">Specifies how the filter will respond to an impulse input (Finite or Infinite). Default is Finite.</param> /// <param name="sampleInterpolator">Optional sampling interpolator over the input. Default is a reproducible linear interpolation.</param> /// <param name="alignmentDateTime">If non-null, this parameter specifies a time to align the sampled messages with, and the sampled messages /// will have originating times that align with (i.e., are an integral number of intervals away from) the specified alignment time.</param> /// <param name="deliveryPolicy">An optional delivery policy for the input stream.</param> /// <returns>A high-pass filtered version of the input.</returns> public static IProducer <double> HighpassFilter( this IProducer <double> input, double sampleRate, double cutoffRate, ImpulseResponse impulseResponse = ImpulseResponse.Finite, Interpolator <double, double> sampleInterpolator = null, DateTime?alignmentDateTime = null, DeliveryPolicy <double> deliveryPolicy = null) { var filter = OnlineFilter.CreateHighpass(impulseResponse, sampleRate, cutoffRate); return(MathNetFilter(input, sampleRate, sampleInterpolator ?? Reproducible.Linear(), alignmentDateTime, filter, deliveryPolicy)); }
/// <summary> /// Initializes a new instance of the <see cref="ParallelVariableLength{TIn, TOut}"/> class. /// </summary> /// <param name="pipeline">Pipeline to which this component belongs.</param> /// <param name="transform">Function mapping keyed input producers to output producers.</param> /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param> /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param> public ParallelVariableLength(Pipeline pipeline, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped = false, TOut defaultValue = default) { this.pipeline = pipeline; this.parallelTransform = transform; this.In = pipeline.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.In)); this.activeBranchesEmitter = pipeline.CreateEmitter <int>(this, nameof(this.activeBranchesEmitter)); var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>(); this.join = new Join <int, TOut, TOut[]>( pipeline, interpolator, (count, values) => values, 0, count => Enumerable.Range(0, count)); this.activeBranchesEmitter.PipeTo(this.join.InPrimary); }
public void TupleCollapsingJoin() { using (var pipeline = Pipeline.Create()) { var range = Generators.Range(pipeline, 0, 10, TimeSpan.FromMilliseconds(10)); var sourceA = range.Select(x => $"A{x}"); var sourceB = range.Select(x => $"B{x}"); var sourceC = range.Select(x => $"C{x}"); var sourceD = range.Select(x => $"D{x}"); var sourceE = range.Select(x => $"E{x}"); var sourceF = range.Select(x => $"F{x}"); var sourceG = range.Select(x => $"G{x}"); var tuples = sourceA .Join(sourceB, Reproducible.Nearest <string>()) .Join(sourceC, Reproducible.Nearest <string>()) .Join(sourceD, Reproducible.Nearest <string>()) .Join(sourceE, Reproducible.Nearest <string>()) .Join(sourceF, Reproducible.Nearest <string>()) .Join(sourceG, Reproducible.Nearest <string>()) .ToObservable().ToListObservable(); pipeline.Run(); var results = tuples.AsEnumerable().ToArray(); Assert.IsTrue(Enumerable.SequenceEqual( new ValueTuple <string, string, string, string, string, string, string>[] { ValueTuple.Create("A0", "B0", "C0", "D0", "E0", "F0", "G0"), ValueTuple.Create("A1", "B1", "C1", "D1", "E1", "F1", "G1"), ValueTuple.Create("A2", "B2", "C2", "D2", "E2", "F2", "G2"), ValueTuple.Create("A3", "B3", "C3", "D3", "E3", "F3", "G3"), ValueTuple.Create("A4", "B4", "C4", "D4", "E4", "F4", "G4"), ValueTuple.Create("A5", "B5", "C5", "D5", "E5", "F5", "G5"), ValueTuple.Create("A6", "B6", "C6", "D6", "E6", "F6", "G6"), ValueTuple.Create("A7", "B7", "C7", "D7", "E7", "F7", "G7"), ValueTuple.Create("A8", "B8", "C8", "D8", "E8", "F8", "G8"), ValueTuple.Create("A9", "B9", "C9", "D9", "E9", "F9", "G9"), }, results)); } }
/// <summary> /// Initializes a new instance of the <see cref="ParallelVariableLength{TIn, TOut}"/> class. /// </summary> /// <param name="pipeline">Pipeline to which this component belongs.</param> /// <param name="transform">Function mapping keyed input producers to output producers.</param> /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param> /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param> /// <param name="name">Name for this component (defaults to ParallelVariableLength).</param> /// <param name="defaultDeliveryPolicy">Pipeline-level default delivery policy to be used by this component (defaults to <see cref="DeliveryPolicy.Unlimited"/> if unspecified).</param> public ParallelVariableLength(Pipeline pipeline, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped = false, TOut defaultValue = default, string name = null, DeliveryPolicy defaultDeliveryPolicy = null) : base(pipeline, name ?? nameof(ParallelVariableLength <TIn, TOut>), defaultDeliveryPolicy) { this.parallelTransform = transform; this.inConnector = this.CreateInputConnectorFrom <TIn[]>(pipeline, nameof(this.inConnector)); this.splitter = this.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.splitter)); this.inConnector.PipeTo(this.splitter); this.activeBranchesEmitter = this.CreateEmitter <int>(this, nameof(this.activeBranchesEmitter)); var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>(); this.join = new Join <int, TOut, TOut[]>( this, interpolator, (count, values) => values, 0, count => Enumerable.Range(0, count)); this.activeBranchesEmitter.PipeTo(this.join.InPrimary); this.outConnector = this.CreateOutputConnectorTo <TOut[]>(pipeline, nameof(this.outConnector)); this.join.PipeTo(this.outConnector); }
/// <summary> /// Initializes a new instance of the <see cref="ParallelFixedLength{TIn, TOut}"/> class. /// </summary> /// <param name="pipeline">Pipeline to which this component belongs.</param> /// <param name="vectorSize">Vector size.</param> /// <param name="transform">Function mapping keyed input producers to output producers.</param> /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param> /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param> public ParallelFixedLength(Pipeline pipeline, int vectorSize, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped, TOut defaultValue = default) { this.In = pipeline.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.In)); this.branches = new Emitter <TIn> [vectorSize]; var branchResults = new IProducer <TOut> [vectorSize]; for (int i = 0; i < vectorSize; i++) { var subpipeline = Subpipeline.Create(pipeline, $"subpipeline{i}"); var connectorIn = new Connector <TIn>(pipeline, subpipeline, $"connectorIn{i}"); var connectorOut = new Connector <TOut>(subpipeline, pipeline, $"connectorOut{i}"); this.branches[i] = pipeline.CreateEmitter <TIn>(this, $"branch{i}"); this.branches[i].PipeTo(connectorIn); transform(i, connectorIn.Out).PipeTo(connectorOut.In); branchResults[i] = connectorOut; } var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>(); this.join = Operators.Join(branchResults, interpolator); }
public void LinearInterpolate() { var evenres = new List <(double, DateTime)>(); var oddres = new List <(double, DateTime)>(); var results = new List <double>(); using (var p = Pipeline.Create()) { var source = Generators.Sequence(p, 0, i => i + 1, 10, TimeSpan.FromTicks(50)); var even = source.Where(i => i % 2 == 0).Select(v => (double)v).Do((m, e) => evenres.Add((m, e.OriginatingTime))); var odd = source.Where(i => i % 2 == 1).Select(v => (double)v).Do((m, e) => oddres.Add((m, e.OriginatingTime))); even .Interpolate(odd, Reproducible.Linear()) .Do(results.Add); p.Run(); } Assert.AreEqual(results.Count, 4); Assert.AreEqual(results[0], 1, 0.00000001); Assert.AreEqual(results[1], 3, 0.00000001); Assert.AreEqual(results[2], 5, 0.00000001); Assert.AreEqual(results[3], 7, 0.00000001); }