MBrace.Core and MBrace.Azure


Example: Using MBrace for image processing

This tutorial is from the MBrace Starter Kit.

In this tutorial, you use the AForge (you can install AForge from Nuget) to turn color images into gray ones by applying a gray filter.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
#r "../../packages/AForge/lib/AForge.dll" 
#r "../../packages/AForge.Math/lib/AForge.Math.dll" 
#r "../../packages/AForge.Imaging/lib/AForge.Imaging.dll" 

open System.Drawing
open AForge.Imaging.Filters
open System.Net
open System.IO

Next, you define a method to download an image from a url, and return a stream containing the downloaded image.

1: 
2: 
3: 
4: 
5: 
let GetStreamFromUrl (url : string) =
    let webClient = new WebClient()
    let data = webClient.DownloadData(url)
    let stream = new MemoryStream(data)
    stream

Next, you create a method that turns the downloaded color image into a gray image by applying an AForge filter, and then uploads the gray image to cloud storage.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
let GrayImageFromWeb (url : string) file =
    cloud {
        // Download image.
        let inputStream =GetStreamFromUrl url
        // Apply the gray filter to the original image.
        let originalBmp = new Bitmap(inputStream)
        let grayedBmp = Grayscale.CommonAlgorithms.BT709.Apply originalBmp        
        // Get the grayed image.
        let outputStream = new MemoryStream()
        do grayedBmp.Save(outputStream, Imaging.ImageFormat.Bmp)        
        // Upload the gray image to cloud storage.
        let! cFile = CloudFile.WriteAllBytes(file, outputStream.ToArray())
        inputStream.Close()
        outputStream.Close()
        return cFile
    }

Last, you perform parallel downloading and image processing in the MBrace cluster.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let urls = [|
    "https://upload.wikimedia.org/wikipedia/commons/thumb/5/54/Tigress_at_Jim_Corbett_National_Park.jpg/330px-Tigress_at_Jim_Corbett_National_Park.jpg";
    "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Poligraf_Poligrafovich.JPG/800px-Poligraf_Poligrafovich.JPG" 
    "https://upload.wikimedia.org/wikipedia/commons/e/eb/Denizli_Atat%C3%BCrk_Stadyumu.jpg"
    |]

let tasks = 
    [|for url in urls -> GrayImageFromWeb url (sprintf ("tmp/%s") (Path.GetFileName(Uri(url).LocalPath)))  |> cluster.CreateProcess |] 

The results are a set of cloud storage file paths for the generated images. You can look these up in your cloud storage account browser.

1: 
2: 
let results = 
    [| for t in tasks -> t.Result |]

In this tutorial, you've learned how to use the AForge image processing library to turn color images to gray ones in MBrace. Continue with further samples to learn more about the MBrace programming model.

Note, you can use the above techniques from both scripts and compiled projects. To see the components referenced by this script, see ThespianCluster.fsx or AzureCluster.fsx.

namespace System
namespace System.IO
namespace MBrace
namespace MBrace.Core
namespace MBrace.Flow
val cluster : MBrace.Thespian.ThespianCluster

Full name: 200-image-processing-example.cluster
module Config
val GetCluster : unit -> MBrace.Thespian.ThespianCluster

Full name: Config.GetCluster


 Gets or creates a new Thespian cluster session.
namespace System.Drawing
namespace AForge
namespace AForge.Imaging
namespace AForge.Imaging.Filters
namespace System.Net
val GetStreamFromUrl : url:string -> MemoryStream

Full name: 200-image-processing-example.GetStreamFromUrl
val url : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val webClient : WebClient
Multiple items
type WebClient =
  inherit Component
  new : unit -> WebClient
  member BaseAddress : string with get, set
  member CachePolicy : RequestCachePolicy with get, set
  member CancelAsync : unit -> unit
  member Credentials : ICredentials with get, set
  member DownloadData : address:string -> byte[] + 1 overload
  member DownloadDataAsync : address:Uri -> unit + 1 overload
  member DownloadFile : address:string * fileName:string -> unit + 1 overload
  member DownloadFileAsync : address:Uri * fileName:string -> unit + 1 overload
  member DownloadString : address:string -> string + 1 overload
  ...

Full name: System.Net.WebClient

--------------------
WebClient() : unit
val data : byte []
WebClient.DownloadData(address: Uri) : byte []
WebClient.DownloadData(address: string) : byte []
val stream : MemoryStream
Multiple items
type MemoryStream =
  inherit Stream
  new : unit -> MemoryStream + 6 overloads
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member Capacity : int with get, set
  member Flush : unit -> unit
  member GetBuffer : unit -> byte[]
  member Length : int64
  member Position : int64 with get, set
  member Read : buffer:byte[] * offset:int * count:int -> int
  ...

Full name: System.IO.MemoryStream

--------------------
MemoryStream() : unit
MemoryStream(capacity: int) : unit
MemoryStream(buffer: byte []) : unit
MemoryStream(buffer: byte [], writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool, publiclyVisible: bool) : unit
val GrayImageFromWeb : url:string -> file:'a -> 'b

Full name: 200-image-processing-example.GrayImageFromWeb
val file : 'a
Multiple items
type Bitmap =
  inherit Image
  new : filename:string -> Bitmap + 11 overloads
  member Clone : rect:Rectangle * format:PixelFormat -> Bitmap + 1 overload
  member GetHbitmap : unit -> nativeint + 1 overload
  member GetHicon : unit -> nativeint
  member GetPixel : x:int * y:int -> Color
  member LockBits : rect:Rectangle * flags:ImageLockMode * format:PixelFormat -> BitmapData + 1 overload
  member MakeTransparent : unit -> unit + 1 overload
  member SetPixel : x:int * y:int * color:Color -> unit
  member SetResolution : xDpi:float32 * yDpi:float32 -> unit
  member UnlockBits : bitmapdata:BitmapData -> unit
  ...

Full name: System.Drawing.Bitmap

--------------------
Bitmap(filename: string) : unit
   (+0 other overloads)
Bitmap(stream: Stream) : unit
   (+0 other overloads)
Bitmap(original: Image) : unit
   (+0 other overloads)
Bitmap(filename: string, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(type: Type, resource: string) : unit
   (+0 other overloads)
Bitmap(stream: Stream, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(width: int, height: int) : unit
   (+0 other overloads)
Bitmap(original: Image, newSize: Size) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, format: Imaging.PixelFormat) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, g: Graphics) : unit
   (+0 other overloads)
Multiple items
type Grayscale =
  inherit BaseFilter
  new : cr:float * cg:float * cb:float -> Grayscale
  val RedCoefficient : float
  val GreenCoefficient : float
  val BlueCoefficient : float
  member FormatTranslations : Dictionary<PixelFormat, PixelFormat>
  nested type CommonAlgorithms

Full name: AForge.Imaging.Filters.Grayscale

--------------------
Grayscale(cr: float, cg: float, cb: float) : unit
type CommonAlgorithms =
  static val BT709 : Grayscale
  static val RMY : Grayscale
  static val Y : Grayscale

Full name: AForge.Imaging.Filters.Grayscale.CommonAlgorithms
field Grayscale.CommonAlgorithms.BT709
BaseFilter.Apply(image: AForge.Imaging.UnmanagedImage) : AForge.Imaging.UnmanagedImage
BaseFilter.Apply(imageData: Imaging.BitmapData) : Bitmap
BaseFilter.Apply(image: Bitmap) : Bitmap
BaseFilter.Apply(sourceImage: AForge.Imaging.UnmanagedImage, destinationImage: AForge.Imaging.UnmanagedImage) : unit
namespace System.Drawing.Imaging
Multiple items
type ImageFormat =
  new : guid:Guid -> ImageFormat
  member Equals : o:obj -> bool
  member GetHashCode : unit -> int
  member Guid : Guid
  member ToString : unit -> string
  static member Bmp : ImageFormat
  static member Emf : ImageFormat
  static member Exif : ImageFormat
  static member Gif : ImageFormat
  static member Icon : ImageFormat
  ...

Full name: System.Drawing.Imaging.ImageFormat

--------------------
Imaging.ImageFormat(guid: Guid) : unit
property Imaging.ImageFormat.Bmp: Imaging.ImageFormat
val urls : string []

Full name: 200-image-processing-example.urls
val tasks : MBrace.Runtime.CloudProcess<obj> []

Full name: 200-image-processing-example.tasks
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
Path.GetFileName(path: string) : string
Multiple items
type Uri =
  new : uriString:string -> Uri + 5 overloads
  member AbsolutePath : string
  member AbsoluteUri : string
  member Authority : string
  member DnsSafeHost : string
  member Equals : comparand:obj -> bool
  member Fragment : string
  member GetComponents : components:UriComponents * format:UriFormat -> string
  member GetHashCode : unit -> int
  member GetLeftPart : part:UriPartial -> string
  ...

Full name: System.Uri

--------------------
Uri(uriString: string) : unit
Uri(uriString: string, uriKind: UriKind) : unit
Uri(baseUri: Uri, relativeUri: string) : unit
Uri(baseUri: Uri, relativeUri: Uri) : unit
member MBrace.Runtime.MBraceClient.CreateProcess : workflow:MBrace.Core.Cloud<'T> * ?cancellationToken:MBrace.Core.ICloudCancellationToken * ?faultPolicy:MBrace.Core.FaultPolicy * ?target:MBrace.Core.IWorkerRef * ?additionalResources:MBrace.Core.Internals.ResourceRegistry * ?taskName:string -> MBrace.Runtime.CloudProcess<'T>
val results : obj []

Full name: 200-image-processing-example.results
val t : MBrace.Runtime.CloudProcess<obj>
property MBrace.Runtime.CloudProcess.Result: obj
Fork me on GitHub