87e1c26add5322a75b11151066a7a34b60f10235
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
43 printfn
"Usage of Parasitemia :"
44 printfn
"Non-interactive mode:"
45 printfn
" %s (--folder <folder>|--file <file>) --output <folder> [--debug]" System.AppDomain.CurrentDomain.FriendlyName
46 printfn
" --folder <folder> : an input folder containing images to analyze"
47 printfn
" --file <file> : an image file to be analyzed"
48 printfn
" --output <folder> : a folder to put the results"
49 printfn
" --debug : output more information like intermediate images if set"
51 printfn
"Interactive mode:"
52 printfn
" %s [<document-file>] [--debug]" System.AppDomain.CurrentDomain.FriendlyName
53 printfn
" <document-file> : a PIAZ file to automatically open at startup"
54 printfn
" --debug : output information like intermediate images if set in the current directory"
56 [<System.Runtime.InteropServices.DllImport "kernel32.dll">]
57 extern
bool AttachConsole (int dwProcessId)
62 // To redirect stdout to the attached console.
63 AttachConsole -1 |> ignore
// -1 to attach to the parent process.
65 if Array.exists
(fun e
-> e
= "--help" || e = "-h") args then
70 Log.Info "Starting of Parasitemia UI ..."
73 match parseArgs args with
75 let config = Config defaultParameters
78 | CmdLine (input
, output) ->
80 config.Debug <- DebugOn output
82 Directory.CreateDirectory output |> ignore
84 use logFile = new StreamWriter (new FileStream (Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
85 let listener = { new IListener with member this
.NewEntry severity header mess
= logFile.WriteLine (header
+ " : " + mess
) }
86 Log.AddListener listener
88 Log.Info "=== New run : %O %s ===" DateTime.Now (if debug
then "[DEBUG]" else "[RELEASE]")
90 let files = match input
with
91 | File file -> [ file ]
92 | Dir dir
-> Directory.EnumerateFiles dir |> List.ofSeq
94 use resultFile = new StreamWriter (new FileStream (Path.Combine (output, "results.txt"), FileMode.Append, FileAccess.Write))
96 let images = [ for file in files -> Path.GetFileNameWithoutExtension (FileInfo(file).Name), config.Copy(), new Image<Bgr, byte
> (file) ]
98 Log.LogWithTime Severity.INFO (
100 match ParasitemiaCore.Analysis.doMultipleAnalysis
images None with
102 for id
, result in results
do
103 let config, img
= images |> List.pick
(fun (id
', config', img
') -> if id' = id
then Some (config', img') else None)
105 let total, infected
= countCells
result.Cells
106 fprintf
resultFile "File: %s %d %d %.2f (diameter: %O)\n" id
total infected (100. * (float infected) / (float total)) config.RBCRadius
108 fprintf
resultFile "Analysis aborted"
110 ) "Whole analyze" |> ignore
112 Log.RmListener (listener)
115 | Window fileToOpen
->
116 if debug
then config.Debug <- DebugOn "."
117 GUI.run
config fileToOpen
119 Log.Info "Parasitemia UI closed"
124 Log.Fatal "Error: %A" ex