1
module ParasitemiaUI.Main
12 open ParasitemiaCore.Utils
13 open ParasitemiaCore.Config
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.
23 type Arguments = RunningMode * bool // bool : true if in debug mode.
25 let parseArgs (args
: string[]) : Arguments =
27 let output = Array.tryFindIndex
((=) "--output") args
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])
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])
38 Window (if args.Length > 0 && not
(args.[0].StartsWith ("--")) then Some args.[0] else None)
40 runningMode, Array.exists
((=) "--debug") args
43 Console.WriteLine Utils.argsHelp
45 [<System.Runtime.InteropServices.DllImport "kernel32.dll">]
46 extern
bool AttachConsole (int dwProcessId)
51 // To redirect stdout to the attached console.
52 AttachConsole -1 |> ignore
// -1 to attach to the parent process.
54 if Array.exists
(fun e
-> e
= "--help" || e = "-h") args then
59 Log.Info "Starting of Parasitemia UI ..."
62 match parseArgs args with
64 let config = Config defaultParameters
67 | CmdLine (input
, output) ->
69 config.Debug <- DebugOn output
71 Directory.CreateDirectory output |> ignore
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
77 Log.Info "=== New run : %O %s ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]")
79 let files = match input
with
80 | File file
-> [ file
]
81 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
83 use resultFile = new StreamWriter (new FileStream (Path.Combine (output, "results.txt"), FileMode.Append, FileAccess.Write))
85 let images = [ for file
in files -> Path.GetFileNameWithoutExtension (FileInfo(file
).Name), config.Copy (), new Image<Bgr, byte
> (file
) ]
87 Log.LogWithTime Severity.INFO (
89 match ParasitemiaCore.Analysis.doMultipleAnalysis
images None with
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)
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
97 fprintf
resultFile "Analysis aborted"
99 ) "Whole analyze" |> ignore
101 Log.RmListener listener
104 | Window fileToOpen
->
105 if debug
then config.Debug <- DebugOn "."
106 GUI.run
config fileToOpen
108 Log.Info "Parasitemia UI closed"
113 Log.Fatal "Error: %A" ex