Thêm tính năng lưu ROI lên Server và Vẽ lại ROI

This commit is contained in:
Le Chau 2020-07-20 11:47:10 +07:00
parent 076290e141
commit 53aa3e31ea
8 changed files with 91 additions and 13 deletions

View File

@ -93,6 +93,9 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http, Version=4.1.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.4.3.4\lib\net46\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
@ -109,7 +112,6 @@
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>

View File

@ -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);

View File

@ -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<SaveLogRespone>();
}
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
{

View File

@ -17,6 +17,10 @@
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.3" newVersion="4.1.1.3" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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<RoiConfig>(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; }
}
}

View File

@ -8,5 +8,6 @@
<package id="OpenCvSharp4" version="4.3.0.20200524" targetFramework="net461" />
<package id="OpenCvSharp4.runtime.win" version="4.3.0.20200524" targetFramework="net461" />
<package id="OpenCvSharp4.Windows" version="4.3.0.20200524" targetFramework="net461" />
<package id="System.Net.Http" version="4.3.4" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.1" targetFramework="net461" />
</packages>