Use real unit (um and ppi) instead of pixel in the parameters.
[master-thesis.git] / Parasitemia / Parasitemia / Program.fs
1 module Parasitemia.Main
2
3 open System
4 open System.IO
5 open System.Threading
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Config
11
12 type Input =
13 | File of string
14 | Dir of string
15
16 type RunningMode =
17 | CmdLine of Input * string
18 | Window
19
20 type Arguments = RunningMode * bool
21
22 let parseArgs (args: string[]) : Arguments =
23
24 let output = Array.tryFindIndex ((=) "--output") args
25
26 let runningMode =
27 match Array.tryFindIndex ((=) "--folder") args, output with
28 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
29 CmdLine ((Dir args.[i+1]), args.[i_output + 1])
30 | _ ->
31 match Array.tryFindIndex ((=) "--file") args, output with
32 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
33 CmdLine ((File args.[i+1]), args.[i_output + 1])
34 |_ ->
35 Window
36
37 runningMode, Array.exists ((=) "--debug") args
38
39
40 [<EntryPoint>]
41 [<STAThread()>]
42 let main args =
43 match parseArgs args with
44 | mode, debug ->
45 let config = Config(defaultParameters)
46
47 match mode with
48 | CmdLine (input, output) ->
49 if debug
50 then
51 config.Debug <- DebugOn output
52
53 Directory.CreateDirectory output |> ignore
54
55 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
56 Utils.log <- (fun m -> logFile.WriteLine(m))
57 Utils.log (sprintf "=== New run : %A %A ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]"))
58
59 let files = match input with
60 | File file -> [ file ]
61 | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
62
63 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
64
65 //try
66 let images = [ for file in files -> Path.GetFileNameWithoutExtension(FileInfo(file).Name), new Image<Bgr, byte>(file) ]
67
68
69 Utils.logTime "Whole analyze" (fun () ->
70 let results = ImageAnalysis.doMultipleAnalysis images config None
71
72 for id, _, cells in results do
73 let total, infected = Utils.countCells cells
74 fprintf resultFile "File: %s %d %d %.2f\n" id total infected (100. * (float infected) / (float total)))
75
76 //Utils.log (sprintf "== File: %A" file)
77 //with
78 //| :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
79 0
80
81 | Window ->
82 (*let display (window : Views.MainWindow) (img : IImage) =
83 let imgControl = window.Root.FindName("img") :?> Controls.Image
84 imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img)
85
86 let log (window : Views.MainWindow) (mess : string) =
87 let txtLog = window.Root.FindName("txtLog") :?> Controls.TextBlock
88 txtLog.Text <- txtLog.Text + mess + "\n"*)
89
90 if debug then config.Debug <- DebugOn "."
91 GUI.Main.run config