|
1 | 1 | namespace ImageProcessing |
2 | 2 |
|
| 3 | +open Argu |
| 4 | +open Argu.ArguAttributes |
3 | 5 | open Brahma.FSharp |
4 | 6 |
|
| 7 | +type Platforms = CPU = 1 | NVidia = 2 | IntelGPU = 3 | AnyGPU = 4 |
| 8 | + |
| 9 | +[<CliPrefix(CliPrefix.DoubleDash)>] |
| 10 | +[<NoAppSettings>] |
| 11 | +type ImageProcessingArguments = |
| 12 | + | [<Mandatory>] Input of string |
| 13 | + | Output of string |
| 14 | + | Platform of Platforms |
| 15 | + with |
| 16 | + interface IArgParserTemplate with |
| 17 | + member arg.Usage = |
| 18 | + match arg with |
| 19 | + | Input _ -> "Image to process." |
| 20 | + | Output _ -> "File to store result." |
| 21 | + | Platform _ -> "Where to run." |
5 | 22 | module Main = |
6 | | - let pathToExamples = "/home/gsv/Projects/TestProj2020/src/ImgProcessing/Examples" |
7 | | - let inputFolder = System.IO.Path.Combine(pathToExamples, "input") |
| 23 | + //let pathToExamples = "/home/gsv/Projects/TestProj2020/src/ImgProcessing/Examples" |
| 24 | + //let inputFolder = System.IO.Path.Combine(pathToExamples, "input") |
| 25 | + //let outputFolder = System.IO.Path.Combine(pathToExamples, "output") |
8 | 26 |
|
9 | | - let demoFile = |
10 | | - System.IO.Path.Combine(inputFolder, "armin-djuhic-ohc29QXbS-s-unsplash.jpg") |
| 27 | + //let demoFileName = "armin-djuhic-ohc29QXbS-s-unsplash.jpg" |
| 28 | + //let demoFile = |
| 29 | + // System.IO.Path.Combine(inputFolder, demoFileName) |
11 | 30 |
|
12 | 31 | [<EntryPoint>] |
13 | 32 | let main (argv: string array) = |
14 | | - let nvidiaDevice = |
15 | | - ClDevice.GetAvailableDevices(platform = Platform.Nvidia) |
16 | | - |> Seq.head |
17 | | - |
18 | | - let intelDevice = |
19 | | - ClDevice.GetAvailableDevices(platform = Platform.Intel) |
20 | | - |> Seq.head |
21 | | - //ClDevice.GetFirstAppropriateDevice() |
22 | | - //printfn $"Device: %A{device.Name}" |
23 | | - |
24 | | - let nvContext = ClContext(nvidiaDevice) |
25 | | - let applyFiltersOnNvGPU = ImageProcessing.applyFiltersGPU nvContext 64 |
26 | | - |
27 | | - let intelContext = ClContext(intelDevice) |
28 | | - let applyFiltersOnIntelGPU = ImageProcessing.applyFiltersGPU intelContext 64 |
29 | | - |
| 33 | + let parser = ArgumentParser.Create<ImageProcessingArguments>(programName = "ImageProcessing") |
| 34 | + let results = parser.ParseCommandLine argv |
| 35 | + let inputFile = results.GetResult(Input, defaultValue = "") |
| 36 | + let outputFile = results.GetResult(Output, defaultValue = "out.jpg") |
| 37 | + let platform = results.GetResult(Platform, defaultValue = Platforms.CPU) |
| 38 | + |
30 | 39 | let filters = [ |
31 | 40 | ImageProcessing.gaussianBlurKernel |
32 | 41 | ImageProcessing.gaussianBlurKernel |
33 | 42 | ImageProcessing.edgesKernel |
34 | 43 | ] |
35 | 44 |
|
36 | | - //let grayscaleImage = ImageProcessing.loadAs2DArray demoFile |
37 | | - //let blur = ImageProcessing.applyFilter ImageProcessing.gaussianBlurKernel grayscaleImage |
38 | | - //let edges = ImageProcessing.applyFilter ImageProcessing.edgesKernel blur |
39 | | - //let edges = applyFiltersGPU [ImageProcessing.gaussianBlurKernel; ImageProcessing.edgesKernel] grayscaleImage |
40 | | - //ImageProcessing.save2DByteArrayAsImage edges "../../../../../out/demo_grayscale.jpg" |
| 45 | + |
| 46 | + match platform with |
| 47 | + | Platforms.CPU -> |
| 48 | + let mutable image = ImageProcessing.loadAs2DArray inputFile |
| 49 | + let start = System.DateTime.Now |
| 50 | + for filter in filters do |
| 51 | + image <- ImageProcessing.applyFilter filter image |
| 52 | + printfn $"CPU processing time: {(System.DateTime.Now - start).TotalMilliseconds} ms" |
| 53 | + ImageProcessing.save2DByteArrayAsImage image outputFile |
| 54 | + | _ -> |
| 55 | + let device = |
| 56 | + match platform with |
| 57 | + | Platforms.AnyGPU -> ClDevice.GetFirstAppropriateDevice() |
| 58 | + | _ -> |
| 59 | + let platform = |
| 60 | + match platform with |
| 61 | + | Platforms.NVidia -> Platform.Nvidia |
| 62 | + | Platforms.IntelGPU -> Platform.Intel |
| 63 | + ClDevice.GetAvailableDevices(platform = platform) |
| 64 | + |> Seq.head |
| 65 | + printfn $"Device: %A{device.Name}" |
| 66 | + |
| 67 | + let context = ClContext(device) |
| 68 | + let applyFiltersOnGPU = ImageProcessing.applyFiltersGPU context 64 |
| 69 | + |
| 70 | + |
| 71 | + let start = System.DateTime.Now |
| 72 | + let grayscaleImage = ImageProcessing.loadAsImage inputFile |
| 73 | + printfn $"Image reading time: {(System.DateTime.Now - start).TotalMilliseconds} ms" |
| 74 | + |
| 75 | + let start = System.DateTime.Now |
| 76 | + let result = applyFiltersOnGPU filters grayscaleImage |
| 77 | + printfn $"GPU processing time: {(System.DateTime.Now - start).TotalMilliseconds} ms" |
| 78 | + ImageProcessing.saveImage result outputFile |
| 79 | + |
| 80 | +(* |
41 | 81 | let start = System.DateTime.Now |
42 | 82 |
|
43 | | - Streaming.processAllFiles inputFolder "../../../../../out/" [ |
44 | | - applyFiltersOnNvGPU filters |
| 83 | + Streaming.processAllFiles inputFolder outputFolder [ |
| 84 | + //applyFiltersOnNvGPU filters |
45 | 85 | applyFiltersOnIntelGPU filters |
46 | 86 | ] |
47 | 87 |
|
48 | 88 | printfn |
49 | 89 | $"TotalTime = %f{(System.DateTime.Now |
50 | 90 | - start) |
51 | 91 | .TotalMilliseconds}" |
52 | | - |
| 92 | +*) |
| 93 | + |
53 | 94 | 0 |
0 commit comments