From 53aa3e31eadc197431701caab52e0a0bfd8efcf7 Mon Sep 17 00:00:00 2001 From: Le Chau Date: Mon, 20 Jul 2020 11:47:10 +0700 Subject: [PATCH] =?UTF-8?q?Th=C3=AAm=20t=C3=ADnh=20n=C4=83ng=20l=C6=B0u=20?= =?UTF-8?q?ROI=20l=C3=AAn=20Server=20v=C3=A0=20V=E1=BA=BD=20l=E1=BA=A1i=20?= =?UTF-8?q?ROI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AIParkingApplication.csproj | 4 ++- .../AIParkingApplicationForm.cs | 8 ++--- AIParkingApplication/ApiController.cs | 32 +++++++++++++++++++ AIParkingApplication/App.config | 4 +++ AIParkingApplication/LaneIn.cs | 13 +++++--- AIParkingApplication/LaneOut.cs | 10 ++++-- AIParkingApplication/Util.cs | 32 ++++++++++++++++++- AIParkingApplication/packages.config | 1 + 8 files changed, 91 insertions(+), 13 deletions(-) diff --git a/AIParkingApplication/AIParkingApplication.csproj b/AIParkingApplication/AIParkingApplication.csproj index 5b41175..14cc15e 100644 --- a/AIParkingApplication/AIParkingApplication.csproj +++ b/AIParkingApplication/AIParkingApplication.csproj @@ -93,6 +93,9 @@ + + ..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll @@ -109,7 +112,6 @@ - diff --git a/AIParkingApplication/AIParkingApplicationForm.cs b/AIParkingApplication/AIParkingApplicationForm.cs index aac0eb0..024be92 100644 --- a/AIParkingApplication/AIParkingApplicationForm.cs +++ b/AIParkingApplication/AIParkingApplicationForm.cs @@ -79,22 +79,22 @@ namespace AIParkingApplication private void InitAllLanes() { - laneIn12 = new LaneIn(10, configOnWeb.CameraData1.Id.ToString(), configOnWeb.CameraData1.StreamUrl, configOnWeb.CameraData2.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true); + laneIn12 = new LaneIn(10, configOnWeb.CameraData1.Id.ToString(), configOnWeb.CameraData1.StreamUrl, configOnWeb.CameraData2.StreamUrl, c3Device, apiController, engineApiController, appLogger, configOnWeb.CameraData1.Roi_config, true, false, true); laneIn12.BorderStyle = BorderStyle.FixedSingle; laneIn12.Hide(); Controls.Add(laneIn12); - laneOut34 = new LaneOut(20, configOnWeb.CameraData3.Id.ToString(), configOnWeb.CameraData3.StreamUrl, configOnWeb.CameraData4.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true); + laneOut34 = new LaneOut(20, configOnWeb.CameraData3.Id.ToString(), configOnWeb.CameraData3.StreamUrl, configOnWeb.CameraData4.StreamUrl, c3Device, apiController, engineApiController, appLogger, configOnWeb.CameraData3.Roi_config, true, false, true); laneOut34.BorderStyle = BorderStyle.FixedSingle; laneOut34.Hide(); Controls.Add(laneOut34); - laneIn56 = new LaneIn(10, configOnWeb.CameraData5.Id.ToString(), configOnWeb.CameraData5.StreamUrl, configOnWeb.CameraData6.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true); + laneIn56 = new LaneIn(10, configOnWeb.CameraData5.Id.ToString(), configOnWeb.CameraData5.StreamUrl, configOnWeb.CameraData6.StreamUrl, c3Device, apiController, engineApiController, appLogger, configOnWeb.CameraData5.Roi_config, true, false, true); laneIn56.BorderStyle = BorderStyle.FixedSingle; laneIn56.Hide(); Controls.Add(laneIn56); - laneOut78 = new LaneOut(20, configOnWeb.CameraData7.Id.ToString(), configOnWeb.CameraData7.StreamUrl, configOnWeb.CameraData8.StreamUrl, c3Device, apiController, engineApiController, appLogger, true, false, true); + laneOut78 = new LaneOut(20, configOnWeb.CameraData7.Id.ToString(), configOnWeb.CameraData7.StreamUrl, configOnWeb.CameraData8.StreamUrl, c3Device, apiController, engineApiController, appLogger, configOnWeb.CameraData7.Roi_config, true, false, true); laneOut78.BorderStyle = BorderStyle.FixedSingle; laneOut78.Hide(); Controls.Add(laneOut78); diff --git a/AIParkingApplication/ApiController.cs b/AIParkingApplication/ApiController.cs index 40951ab..001860b 100644 --- a/AIParkingApplication/ApiController.cs +++ b/AIParkingApplication/ApiController.cs @@ -5,6 +5,7 @@ using OpenCvSharp; using Newtonsoft.Json; using System.Net; using NLog; +using System.Drawing; namespace AIParkingApplication { @@ -150,6 +151,36 @@ namespace AIParkingApplication } } + public async void SaveRoiToServer(Rectangle roiRegion, string cameraId) + { + int requestCounter = 1; + try + { + var request = new RoiConfig + { + CameraID = cameraId, + X = roiRegion.X, + Y = roiRegion.Y, + Width = roiRegion.Width, + Height = roiRegion.Height + }; + HttpResponseMessage response; + + do + { + response = await httpClient.PostAsJsonAsync("/api/update-roi", request); + response.EnsureSuccessStatusCode(); + requestCounter += 1; + } while (response.StatusCode != HttpStatusCode.OK && requestCounter < numberOfRetry); + SaveLogRespone saveLogRespone = await response.Content.ReadAsAsync(); + } + catch (Exception ex) + { + Console.WriteLine($"SaveRoiConfig Exception:\t{DateTime.Now.GetTimeFormatted()} \t {ex.Message}"); + appLogger.Log(LogLevel.Error, string.Format("Không thể lưu ROI Config | exMessage: " + ex.Message)); + } + } + public void Dispose() { if (httpClient != null && !isHttpClientDisposabled) @@ -487,6 +518,7 @@ namespace AIParkingApplication public int Logs { get; set; } } + #region LaneConfig public class LaneConfig { diff --git a/AIParkingApplication/App.config b/AIParkingApplication/App.config index 78684e9..269b7fd 100644 --- a/AIParkingApplication/App.config +++ b/AIParkingApplication/App.config @@ -17,6 +17,10 @@ + + + + diff --git a/AIParkingApplication/LaneIn.cs b/AIParkingApplication/LaneIn.cs index a5fd0c6..efbeaa9 100644 --- a/AIParkingApplication/LaneIn.cs +++ b/AIParkingApplication/LaneIn.cs @@ -1,9 +1,11 @@ using System; using System.Diagnostics; using System.Drawing; +using System.Drawing.Printing; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using Newtonsoft.Json; using NLog; using OpenCvSharp; using OpenCvSharp.Extensions; @@ -29,6 +31,7 @@ namespace AIParkingApplication private System.Drawing.Point CurrentCursorPosition { get; set; } private Rectangle pictureBoxRoi; private bool IsDrawBox { get; set; } + private Rect roiBoxRect; public LaneIn(int doorId, string cameraId, @@ -38,6 +41,7 @@ namespace AIParkingApplication ApiController apiController, EngineApiController engineApiController, Logger appLogger, + string roiConfig, bool isSupportSquarePlate = true, bool isSupportLongPlate = false, bool isAutoOpenDoor = true, @@ -56,6 +60,7 @@ namespace AIParkingApplication this.apiController = apiController; this.doorControlAccess = doorControlAccess; plateProcessor = new PlateProcessor(engineApiController, this.isSupportSquarePlate, this.isSupportLongPlate); + this.pictureBoxRoi = Util.ConvertStringToRectangle(roiConfig); } private async void C3Device_OnNewCardReceived(int doorId, string cardNumber) @@ -154,10 +159,11 @@ namespace AIParkingApplication { var starTime = DateTime.Now; Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720)); - Rect roiBoxRect = Util.convertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo); + roiBoxRect = Util.ConvertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo); Mat frameRoi = frame[roiBoxRect]; try { + FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frameRoi); if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString)) @@ -266,13 +272,14 @@ namespace AIParkingApplication private void pictureBoxPlateVideo_MouseUp(object sender, MouseEventArgs e) { + IsDrawBox = false; if (StartPoint.X + pictureBoxRoi.Width > pictureBoxPlateVideo.Width || StartPoint.Y + pictureBoxRoi.Height > pictureBoxPlateVideo.Height) { pictureBoxRoi.X = pictureBoxRoi.Y = 0; pictureBoxRoi.Width = pictureBoxPlateVideo.Width; pictureBoxRoi.Height = pictureBoxPlateVideo.Height; } - IsDrawBox = false; + apiController.SaveRoiToServer(pictureBoxRoi, cameraId); } private void pictureBoxPlateVideo_Paint(object sender, PaintEventArgs e) @@ -280,7 +287,5 @@ namespace AIParkingApplication Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 3); e.Graphics.DrawRectangle(redPen, pictureBoxRoi); } - - } } \ No newline at end of file diff --git a/AIParkingApplication/LaneOut.cs b/AIParkingApplication/LaneOut.cs index 24963c2..a2e1bd7 100644 --- a/AIParkingApplication/LaneOut.cs +++ b/AIParkingApplication/LaneOut.cs @@ -31,6 +31,7 @@ namespace AIParkingApplication private System.Drawing.Point CurrentCursorPosition { get; set; } private Rectangle pictureBoxRoi; private bool IsDrawBox { get; set; } + private Rect roiBoxRect; public LaneOut(int doorId, string cameraId, @@ -40,6 +41,7 @@ namespace AIParkingApplication ApiController apiController, EngineApiController engineApiController, Logger appLogger, + string roiConfig, bool isSupportSquarePlate = true, bool isSupportLongPlate = false, bool isAutoOpenDoor = true, @@ -60,6 +62,7 @@ namespace AIParkingApplication printer = new Printer(appLogger); plateProcessor = new PlateProcessor(engineApiController, this.isSupportSquarePlate, this.isSupportLongPlate); ReadUsePrinterConfig(); + this.pictureBoxRoi = Util.ConvertStringToRectangle(roiConfig); } private async void C3Device_OnNewCardReceived(int doorId, string cardNumber) @@ -198,7 +201,7 @@ namespace AIParkingApplication { var starTime = DateTime.Now; Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720)); - Rect roiBoxRect = Util.convertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo); + Rect roiBoxRect = Util.ConvertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo); Mat frameRoi = frame[roiBoxRect]; try { @@ -347,18 +350,19 @@ namespace AIParkingApplication private void pictureBoxPlateVideo_MouseUp(object sender, MouseEventArgs e) { + IsDrawBox = false; if (StartPoint.X + pictureBoxRoi.Width > pictureBoxPlateVideo.Width || StartPoint.Y + pictureBoxRoi.Height > pictureBoxPlateVideo.Height) { pictureBoxRoi.X = pictureBoxRoi.Y = 0; pictureBoxRoi.Width = pictureBoxPlateVideo.Width; pictureBoxRoi.Height = pictureBoxPlateVideo.Height; } - IsDrawBox = false; + apiController.SaveRoiToServer(pictureBoxRoi, cameraId); } private void pictureBoxPlateVideo_Paint(object sender, PaintEventArgs e) { - Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 4); + Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 3); e.Graphics.DrawRectangle(redPen, pictureBoxRoi); } } diff --git a/AIParkingApplication/Util.cs b/AIParkingApplication/Util.cs index bfac376..705488e 100644 --- a/AIParkingApplication/Util.cs +++ b/AIParkingApplication/Util.cs @@ -138,7 +138,7 @@ namespace AIParkingApplication } } - public static Rect convertRectangleToRect(Rectangle pictureBoxRoi, PictureBox pictureBoxPlateVideo) + public static Rect ConvertRectangleToRect(Rectangle pictureBoxRoi, PictureBox pictureBoxPlateVideo) { Rect temp = new Rect { @@ -149,6 +149,18 @@ namespace AIParkingApplication }; return temp; } + + public static Rectangle ConvertStringToRectangle(string input) + { + var roi = JsonConvert.DeserializeObject(input); + return new Rectangle + { + X = roi.X, + Y = roi.Y, + Height = roi.Height, + Width = roi.Width + }; + } } public class PlateRequestEngineModel @@ -179,4 +191,22 @@ namespace AIParkingApplication public const string HTTP_POST_METHOD = "POST"; public const string HTTP_POST_CONTENTTYPE = "application/json"; } + + public class RoiConfig + { + [JsonProperty("x")] + public int X { get; set; } + + [JsonProperty("y")] + public int Y { get; set; } + + [JsonProperty("width")] + public int Width { get; set; } + + [JsonProperty("height")] + public int Height { get; set; } + + [JsonProperty("camera_id")] + public string CameraID { get; set; } + } } diff --git a/AIParkingApplication/packages.config b/AIParkingApplication/packages.config index 8f2f805..c94a4ac 100644 --- a/AIParkingApplication/packages.config +++ b/AIParkingApplication/packages.config @@ -8,5 +8,6 @@ + \ No newline at end of file