Add a status bar.
[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
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 [<EntryPoint>]
43 [<STAThread()>]
44 let main args =
45 try
46 Log.User("Starting of Parasitemia UI ...")
47
48 let result =
49 match parseArgs args with
50 | mode, debug ->
51 let config = Config(defaultParameters)
52
53 match mode with
54 | CmdLine (input, output) ->
55 if debug
56 then
57 config.Debug <- DebugOn output
58
59 Directory.CreateDirectory output |> ignore
60
61 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
62 Log.AddListener({ new IListener with member this.NewEntry mess severity = logFile.WriteLine(mess) })
63
64 Log.User (sprintf "=== New run : %A %A ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]"))
65
66 let files = match input with
67 | File file -> [ file ]
68 | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
69
70 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
71
72
73 let images = [ for file in files -> Path.GetFileNameWithoutExtension(FileInfo(file).Name), config.Copy(), new Image<Bgr, byte>(file) ]
74
75 Log.LogWithTime("Whole analyze", Severity.USER, (fun () ->
76 let results = ParasitemiaCore.Analysis.doMultipleAnalysis images None
77
78 for id, cells in results do
79 let config = images |> List.pick (fun (id', config', _) -> if id' = id then Some config' else None)
80 let total, infected = countCells cells
81 fprintf resultFile "File: %s %d %d %.2f (diameter: %A)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius))
82 0
83
84 | Window fileToOpen ->
85 if debug then config.Debug <- DebugOn "."
86 GUI.run config fileToOpen
87
88 Log.User("Parasitemia UI closed")
89 result
90
91 with
92 | _ as ex ->
93 Log.Fatal("Error: {0}", ex)
94 1