From 5d09d95f909e7d8de0ef25a6826759c2bc4ee7b6 Mon Sep 17 00:00:00 2001 From: DucDangAnh Date: Thu, 18 Jun 2020 02:29:03 +0700 Subject: [PATCH] Use Task To Make UI More Responsive In Event handler: PlateCamera_OnOneVideoFrameRequested. --- AIParkingApplication/LaneIn.cs | 54 ++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/AIParkingApplication/LaneIn.cs b/AIParkingApplication/LaneIn.cs index f8ec612..6f58f56 100644 --- a/AIParkingApplication/LaneIn.cs +++ b/AIParkingApplication/LaneIn.cs @@ -3,6 +3,7 @@ using OpenCvSharp.Extensions; using System; using System.Drawing; using System.IO; +using System.Threading.Tasks; using System.Windows.Forms; namespace AIParkingApplication @@ -44,37 +45,38 @@ namespace AIParkingApplication private void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame) { - Cv2.Resize(videoFrame, videoFrame, new OpenCvSharp.Size(1280, 720)); - Mat result = squarePlateDetector.DetectPlate(videoFrame); - - Cv2.Resize(result, result, new OpenCvSharp.Size(272, 272)); - var response = Util.SendEngineRequestAsync(result, PlateType.Square); - - Bitmap resultPlateImage; - OcrResult ocrResult = response.Result; - if (!string.IsNullOrEmpty(ocrResult.Ocr) && !string.IsNullOrEmpty(ocrResult.Plate)) + Task.Factory.StartNew(new Action(async () => { - var imageData = Convert.FromBase64String(ocrResult.Plate); - using (var ms = new MemoryStream(imageData)) + Cv2.Resize(videoFrame, videoFrame, new OpenCvSharp.Size(1280, 720)); + Mat result = squarePlateDetector.DetectPlate(videoFrame); + + Cv2.Resize(result, result, new OpenCvSharp.Size(272, 272)); + OcrResult ocrResult = await Util.SendEngineRequestAsync(result, PlateType.Square); + + Bitmap resultPlateImage; + if (!string.IsNullOrEmpty(ocrResult.Ocr) && !string.IsNullOrEmpty(ocrResult.Plate)) { - resultPlateImage = new Bitmap(ms); + var imageData = Convert.FromBase64String(ocrResult.Plate); + using (var ms = new MemoryStream(imageData)) + { + resultPlateImage = new Bitmap(ms); + } + } + else + { + resultPlateImage = result.ToBitmap(); } - } - else - { - resultPlateImage = result.ToBitmap(); - } + lblPlateString.Invoke(new Action(() => + { + lblPlateString.Text = ocrResult.Ocr; + })); - lblPlateString.Invoke(new Action(() => - { - lblPlateString.Text = ocrResult.Ocr; - })); - - pictureBoxPlateImage.Invoke(new Action(() => - { - pictureBoxPlateImage.Image?.Dispose(); - pictureBoxPlateImage.Image = resultPlateImage; + pictureBoxPlateImage.Invoke(new Action(() => + { + pictureBoxPlateImage.Image?.Dispose(); + pictureBoxPlateImage.Image = resultPlateImage; + })); })); }