Upgrade the logger component
[master-thesis.git] / Parasitemia / ParasitemiaUI / Program.fs
1 module ParasitemiaUI.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 Logger
11
12 open ParasitemiaCore.Utils
13 open ParasitemiaCore.Config
14
15 type Input =
16 | File of string
17 | Dir of string
18
19 type RunningMode =
20 | CmdLine of Input * string // A file or a directory to process and the output directory.
21 | Window of string option // An optional path to a file to open can be given in window mode.
22
23 type Arguments = RunningMode * bool // bool : true if in debug mode.
24
25 let parseArgs (args : string[]) : Arguments =
26
27 let output = Array.tryFindIndex ((=) "--output") args
28
29 let runningMode =
30 match Array.tryFindIndex ((=) "--folder") args, output with
31 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
32 CmdLine ((Dir args.[i + 1]), args.[i_output + 1])
33 | _ ->
34 match Array.tryFindIndex ((=) "--file") args, output with
35 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
36 CmdLine ((File args.[i + 1]), args.[i_output + 1])
37 |_ ->
38 Window (if args.Length > 0 && not (args.[0].StartsWith ("--")) then Some args.[0] else None)
39
40 runningMode, Array.exists ((=) "--debug") args
41
42 let showArgsHelp () =
43 Console.WriteLine Utils.argsHelp
44
45 [<System.Runtime.InteropServices.DllImport "kernel32.dll">]
46 extern bool AttachConsole (int dwProcessId)
47
48 [<EntryPoint>]
49 [<STAThread ()>]
50 let main args =
51 // To redirect stdout to the attached console.
52 AttachConsole -1 |> ignore // -1 to attach to the parent process.
53
54 if Array.exists (fun e -> e = "--help" || e = "-h") args then
55 showArgsHelp ()
56 0
57 else
58 try
59 Log.Info "Starting of Parasitemia UI ..."
60
61 let result =
62 match parseArgs args with
63 | mode, debug ->
64 let config = Config defaultParameters
65
66 match mode with
67 | CmdLine (input, output) ->
68 if debug then
69 config.Debug <- DebugOn output
70
71 Directory.CreateDirectory output |> ignore
72
73 use logFile = new StreamWriter (new FileStream (Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
74 let listener = { new IListener with member this.NewEntry severity header mess = logFile.WriteLine (header + " : " + mess) }
75 Log.AddListener listener
76
77 Log.Info "=== New run : %O %s ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]")
78
79 let files = match input with
80 | File file -> [ file ]
81 | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
82
83 use resultFile = new StreamWriter (new FileStream (Path.Combine (output, "results.txt"), FileMode.Append, FileAccess.Write))
84
85 let images = [ for file in files -> Path.GetFileNameWithoutExtension (FileInfo(file).Name), config.Copy (), new Image<Bgr, byte> (file) ]
86
87 Log.LogWithTime Types.Severity.INFO (
88 fun () ->
89 match ParasitemiaCore.Analysis.doMultipleAnalysis images None with
90 | Some results ->
91 for id, result in results do
92 let config, img = images |> List.pick (fun (id', config', img') -> if id' = id then Some (config', img') else None)
93 img.Dispose ()
94 let total, infected = countCells result.Cells
95 fprintf resultFile "File: %s %d %d %.2f (diameter: %O)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius
96 | None ->
97 fprintf resultFile "Analysis aborted"
98 Some ()
99 ) "Whole analyze" |> ignore
100
101 Log.RemoveListener listener
102 0
103
104 | Window fileToOpen ->
105 if debug then config.Debug <- DebugOn "."
106 GUI.run config fileToOpen
107
108 Log.Info "Parasitemia UI closed"
109 result
110
111 with
112 | ex ->
113 Log.Fatal "Error: %A" ex
114 1