ce7cdd48921ea6430d79173d2a855b00aaef16d8
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 let listener = { new IListener with member this
.NewEntry severity mess
= logFile.WriteLine(mess
) }
63 Log.AddListener(listener)
65 Log.User (sprintf
"=== New run : %A %A ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]"))
67 let files = match input
with
68 | File file
-> [ file
]
69 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
71 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
74 let images = [ for file
in files -> Path.GetFileNameWithoutExtension(FileInfo(file
).Name), config.Copy(), new Image<Bgr, byte
>(file
) ]
76 Log.LogWithTime("Whole analyze", Severity.USER, (fun () ->
77 match ParasitemiaCore.Analysis.doMultipleAnalysis
images None with
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)
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
85 fprintf
resultFile "Analysis aborted"
88 Log.RmListener(listener)
91 | Window fileToOpen
->
92 if debug
then config.Debug <- DebugOn "."
93 GUI.run
config fileToOpen
95 Log.User("Parasitemia UI closed")
100 Log.Fatal("Error: {0}", ex)