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
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
46 Log.User("Starting of Parasitemia UI ...")
49 match parseArgs args with
51 let config = Config(defaultParameters
)
54 | CmdLine (input
, output) ->
57 config.Debug <- DebugOn output
59 Directory.CreateDirectory output |> ignore
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
) })
64 Log.User (sprintf
"=== New run : %A %A ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]"))
66 let files = match input
with
67 | File file
-> [ file
]
68 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
70 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
73 let images = [ for file
in files -> Path.GetFileNameWithoutExtension(FileInfo(file
).Name), config.Copy(), new Image<Bgr, byte
>(file
) ]
75 Log.LogWithTime("Whole analyze", Severity.USER, (fun () ->
76 let results = ParasitemiaCore.Analysis.doMultipleAnalysis
images None
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))
84 | Window fileToOpen
->
85 if debug
then config.Debug <- DebugOn "."
86 GUI.run
config fileToOpen
88 Log.User("Parasitemia UI closed")
93 Log.Fatal("Error: {0}", ex)