ce7cdd48921ea6430d79173d2a855b00aaef16d8
[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 let listener = { new IListener with member this.NewEntry severity mess = logFile.WriteLine(mess) }
63 Log.AddListener(listener)
64
65 Log.User (sprintf "=== New run : %A %A ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]"))
66
67 let files = match input with
68 | File file -> [ file ]
69 | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
70
71 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
72
73
74 let images = [ for file in files -> Path.GetFileNameWithoutExtension(FileInfo(file).Name), config.Copy(), new Image<Bgr, byte>(file) ]
75
76 Log.LogWithTime("Whole analyze", Severity.USER, (fun () ->
77 match ParasitemiaCore.Analysis.doMultipleAnalysis images None with
78 | Some results ->
79 for id, cells in results do
80 let config, img = images |> List.pick (fun (id', config', img') -> if id' = id then Some (config', img') else None)
81 img.Dispose()
82 let total, infected = countCells cells
83 fprintf resultFile "File: %s %d %d %.2f (diameter: %A)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius
84 | None ->
85 fprintf resultFile "Analysis aborted"
86 Some ())) |> ignore
87
88 Log.RmListener(listener)
89 0
90
91 | Window fileToOpen ->
92 if debug then config.Debug <- DebugOn "."
93 GUI.run config fileToOpen
94
95 Log.User("Parasitemia UI closed")
96 result
97
98 with
99 | ex ->
100 Log.Fatal("Error: {0}", ex)
101 1