Use Task To Make UI More Responsive In Event handler: PlateCamera_OnOneVideoFrameRequested.

This commit is contained in:
DucDangAnh 2020-06-18 02:29:03 +07:00
parent b731d24880
commit 5d09d95f90

View File

@ -3,6 +3,7 @@ using OpenCvSharp.Extensions;
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace AIParkingApplication namespace AIParkingApplication
@ -43,15 +44,16 @@ namespace AIParkingApplication
} }
private void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame) private void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame)
{
Task.Factory.StartNew(new Action(async () =>
{ {
Cv2.Resize(videoFrame, videoFrame, new OpenCvSharp.Size(1280, 720)); Cv2.Resize(videoFrame, videoFrame, new OpenCvSharp.Size(1280, 720));
Mat result = squarePlateDetector.DetectPlate(videoFrame); Mat result = squarePlateDetector.DetectPlate(videoFrame);
Cv2.Resize(result, result, new OpenCvSharp.Size(272, 272)); Cv2.Resize(result, result, new OpenCvSharp.Size(272, 272));
var response = Util.SendEngineRequestAsync(result, PlateType.Square); OcrResult ocrResult = await Util.SendEngineRequestAsync(result, PlateType.Square);
Bitmap resultPlateImage; Bitmap resultPlateImage;
OcrResult ocrResult = response.Result;
if (!string.IsNullOrEmpty(ocrResult.Ocr) && !string.IsNullOrEmpty(ocrResult.Plate)) if (!string.IsNullOrEmpty(ocrResult.Ocr) && !string.IsNullOrEmpty(ocrResult.Plate))
{ {
var imageData = Convert.FromBase64String(ocrResult.Plate); var imageData = Convert.FromBase64String(ocrResult.Plate);
@ -65,7 +67,6 @@ namespace AIParkingApplication
resultPlateImage = result.ToBitmap(); resultPlateImage = result.ToBitmap();
} }
lblPlateString.Invoke(new Action(() => lblPlateString.Invoke(new Action(() =>
{ {
lblPlateString.Text = ocrResult.Ocr; lblPlateString.Text = ocrResult.Ocr;
@ -76,6 +77,7 @@ namespace AIParkingApplication
pictureBoxPlateImage.Image?.Dispose(); pictureBoxPlateImage.Image?.Dispose();
pictureBoxPlateImage.Image = resultPlateImage; pictureBoxPlateImage.Image = resultPlateImage;
})); }));
}));
} }
private void OverviewCameraOnVideoFrameReceived(Mat videoFrame) private void OverviewCameraOnVideoFrameReceived(Mat videoFrame)