public IPipeline <TInput, TMapped> Then <TMapped>(FSharpFunc <TOutput, TMapped> nextfunction) { var compose = FSharpFuncUtils.Create <TInput, TMapped>(value => nextfunction.Invoke(_function.Invoke(value))); return(new CsPipeline <TInput, TMapped>(compose)); }
void StartFaceDetection_Pipeline_FSharpFunc(string imagesFolder) { var files = Directory.GetFiles(ImagesFolder); Func <string, Image <Bgr, byte> > imageFn = (fileName) => new Image <Bgr, byte>(fileName); Func <Image <Bgr, byte>, Tuple <Image <Bgr, byte>, Image <Gray, byte> > > grayFn = image => Tuple.Create(image, image.Convert <Gray, byte>()); Func <Tuple <Image <Bgr, byte>, Image <Gray, byte> >, Tuple <Image <Bgr, byte>, System.Drawing.Rectangle[]> > detectFn = frames => Tuple.Create(frames.Item1, CascadeClassifierThreadLocal.Value.DetectMultiScale( frames.Item2, 1.1, 3, System.Drawing.Size.Empty)); Func <Tuple <Image <Bgr, byte>, System.Drawing.Rectangle[]>, Bitmap> drawFn = faces => { foreach (var face in faces.Item2) { faces.Item1.Draw(face, new Bgr(System.Drawing.Color.BurlyWood), 3); } return(faces.Item1.ToBitmap()); }; var imagePipe = Pipeline <string, Image <Bgr, byte> > .Create(imageFn.ToFSharpFunc()) .Then(grayFn.ToFSharpFunc()) .Then(detectFn.ToFSharpFunc()) .Then(drawFn.ToFSharpFunc()); // #A CancellationTokenSource cts = new CancellationTokenSource(); imagePipe.Execute(4, cts.Token); // #B foreach (string fileName in files) { imagePipe.Enqueue(fileName, FSharpFuncUtils.Create <Tuple <string, Bitmap>, Unit> ((tup) => { Application.Current.Dispatcher.Invoke( () => Images.Add(tup.Item2.ToBitmapImage())); return((Unit)Activator.CreateInstance(typeof(Unit), true)); })); // #C } }