|
| 1 | +module ImageProcessing.Streaming |
| 2 | + |
| 3 | +let listAllFiles dir = |
| 4 | + let files = System.IO.Directory.GetFiles dir |
| 5 | + List.ofArray files |
| 6 | + |
| 7 | +type msg = |
| 8 | + | Img of string*byte[,] |
| 9 | + | EOS of AsyncReplyChannel<unit> |
| 10 | + |
| 11 | +let imgSaver outDir = |
| 12 | + let outFile (fileFullPath:string) = |
| 13 | + System.IO.Path.Combine(outDir, System.IO.Path.GetFileName fileFullPath) |
| 14 | + |
| 15 | + MailboxProcessor.Start(fun inbox -> |
| 16 | + let rec loop () = |
| 17 | + async{ |
| 18 | + let! msg = inbox.Receive() |
| 19 | + match msg with |
| 20 | + | EOS ch -> |
| 21 | + printfn "Image saver is finished!" |
| 22 | + ch.Reply() |
| 23 | + | Img (file, img) -> |
| 24 | + printfn $"Save: %A{file}" |
| 25 | + ImageProcessing.save2DByteArrayAsImage img (outFile file) |
| 26 | + return! loop () |
| 27 | + } |
| 28 | + loop () |
| 29 | + ) |
| 30 | + |
| 31 | +let imgProcessor filterApplicator (imgSaver:MailboxProcessor<_>) = |
| 32 | + |
| 33 | + let filter = filterApplicator |
| 34 | + |
| 35 | + MailboxProcessor.Start(fun inbox -> |
| 36 | + let rec loop cnt = |
| 37 | + async{ |
| 38 | + let! msg = inbox.Receive() |
| 39 | + match msg with |
| 40 | + | EOS ch -> |
| 41 | + printfn "Image processor is ready to finish!" |
| 42 | + imgSaver.PostAndReply EOS |
| 43 | + printfn "Image processor is finished!" |
| 44 | + ch.Reply() |
| 45 | + | Img (file,img) -> |
| 46 | + printfn $"Filter: %A{file}" |
| 47 | + let filtered = filter img |
| 48 | + imgSaver.Post (Img (file,filtered)) |
| 49 | + return! loop (not cnt) |
| 50 | + } |
| 51 | + loop true |
| 52 | + ) |
| 53 | + |
| 54 | +let processAllFiles inDir outDir filterApplicator = |
| 55 | + let imgSaver = imgSaver outDir |
| 56 | + let imgProcessor = imgProcessor filterApplicator imgSaver |
| 57 | + let filesToProcess = listAllFiles inDir |
| 58 | + for file in filesToProcess do |
| 59 | + imgProcessor.Post <| Img(file, ImageProcessing.loadAs2DArray file) |
| 60 | + imgProcessor.PostAndReply EOS |
0 commit comments