Add an about window.
[master-thesis.git] / Parasitemia / Parasitemia / Program.fs
1 module Parasitemia.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 Config
11
12 type Input =
13 | File of string
14 | Dir of string
15
16 type RunningMode =
17 | CmdLine of Input * string // A file or a directory to process and the output directory.
18 | Window of string option // An optional path to a file to open can be given in window mode.
19
20 type Arguments = RunningMode * bool
21
22 let parseArgs (args: string[]) : Arguments =
23
24 let output = Array.tryFindIndex ((=) "--output") args
25
26 let runningMode =
27 match Array.tryFindIndex ((=) "--folder") args, output with
28 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
29 CmdLine ((Dir args.[i+1]), args.[i_output + 1])
30 | _ ->
31 match Array.tryFindIndex ((=) "--file") args, output with
32 | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 ->
33 CmdLine ((File args.[i+1]), args.[i_output + 1])
34 |_ ->
35 Window (if args.Length > 0 && not (args.[0].StartsWith("--")) then Some args.[0] else None)
36
37 runningMode, Array.exists ((=) "--debug") args
38
39 [<EntryPoint>]
40 [<STAThread()>]
41 let main args =
42
43 let e = Ellipse.ellipse2 -11.4 -7.8 -0.169811 -23.75 0.8 -3.885714 -19. 1.5
44
45 match parseArgs args with
46 | mode, debug ->
47 let config = Config(defaultParameters)
48
49 match mode with
50 | CmdLine (input, output) ->
51 if debug
52 then
53 config.Debug <- DebugOn output
54
55 Directory.CreateDirectory output |> ignore
56
57 use logFile = new StreamWriter(new FileStream(Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
58 Utils.log <- (fun m -> logFile.WriteLine(m))
59 Utils.log (sprintf "=== New run : %A %A ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]"))
60
61 let files = match input with
62 | File file -> [ file ]
63 | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
64
65 use resultFile = new StreamWriter(new FileStream(Path.Combine(output, "results.txt"), FileMode.Append, FileAccess.Write))
66
67 //try
68 let images = [ for file in files -> Path.GetFileNameWithoutExtension(FileInfo(file).Name), config.Copy(), new Image<Bgr, byte>(file) ]
69
70
71 Utils.logTime "Whole analyze" (fun () ->
72 let results = ImageAnalysis.doMultipleAnalysis images None
73
74 for id, cells in results do
75 let config = images |> List.pick (fun (id', config', _) -> if id' = id then Some config' else None)
76 let total, infected = Utils.countCells cells
77 fprintf resultFile "File: %s %d %d %.2f (diameter: %A)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius)
78
79 //Utils.log (sprintf "== File: %A" file)
80 //with
81 //| :? IOException as ex -> Utils.log (sprintf "Unable to open the image '%A': %A" file ex)
82 0
83
84 | Window fileToOpen ->
85 (*let display (window : Views.MainWindow) (img : IImage) =
86 let imgControl = window.Root.FindName("img") :?> Controls.Image
87 imgControl.Source <- BitmapSourceConvert.ToBitmapSource(img)
88
89 let log (window : Views.MainWindow) (mess : string) =
90 let txtLog = window.Root.FindName("txtLog") :?> Controls.TextBlock
91 txtLog.Text <- txtLog.Text + mess + "\n"*)
92
93 if debug then config.Debug <- DebugOn "."
94 GUI.Main.run config fileToOpen