Add EngineApiController

This commit is contained in:
DucDangAnh 2020-07-06 17:51:30 +07:00
parent 2c5c923fc9
commit bbe0f7ed64
7 changed files with 63 additions and 35 deletions

View File

@ -117,6 +117,7 @@
<Compile Include="AppConstant.cs" />
<Compile Include="C3DeviceController.cs" />
<Compile Include="Camera.cs" />
<Compile Include="EngineApiController.cs" />
<Compile Include="Enums.cs" />
<Compile Include="IDoorControlAccess.cs" />
<Compile Include="ILane.cs" />

View File

@ -25,12 +25,23 @@ namespace AIParkingApplication
Controls.Add(sidebar);
c3Device = new C3DeviceController("192.168.1.200");
laneIn = new LaneIn(1, this.configOnWeb.CameraData1.StreamUrl, this.configOnWeb.CameraData2.StreamUrl, c3Device, this.apiController, true, false, true);
EngineApiController engineApiController = null;
try
{
engineApiController = new EngineApiController($"http://{this.configOnWeb.APIPath.ApiPlateRecognize.IP}:{this.configOnWeb.APIPath.ApiPlateRecognize.Port}");
}
catch (Exception)
{
MessageBox.Show("Cấu hình API Plate Recognize lỗi!", "Cấu hình API Engine lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
laneIn = new LaneIn(1, this.configOnWeb.CameraData1.StreamUrl, this.configOnWeb.CameraData2.StreamUrl, c3Device, this.apiController, engineApiController, true, false, true);
laneIn.BorderStyle = BorderStyle.FixedSingle;
laneIn.Location = new System.Drawing.Point(sidebar.Location.X + sidebar.Width + 20, 0);
Controls.Add(laneIn);
laneOut = new LaneOut(2, this.configOnWeb.CameraData3.StreamUrl, this.configOnWeb.CameraData4.StreamUrl, c3Device, this.apiController, true, false, true);
laneOut = new LaneOut(2, this.configOnWeb.CameraData3.StreamUrl, this.configOnWeb.CameraData4.StreamUrl, c3Device, this.apiController, engineApiController, true, false, true);
laneOut.BorderStyle = BorderStyle.FixedSingle;
laneOut.Location = new System.Drawing.Point(laneIn.Location.X + laneIn.Width + 20, 0);
Controls.Add(laneOut);

View File

@ -0,0 +1,40 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using OpenCvSharp;
namespace AIParkingApplication
{
public class EngineApiController
{
private HttpClient httpClientEngine;
public EngineApiController(string engineBaseAddress)
{
httpClientEngine = new HttpClient { BaseAddress = new Uri(engineBaseAddress), Timeout = TimeSpan.FromMilliseconds(5000) };
}
public async Task<OcrResult> SendEngineRequestAsync(Mat plateImage, PlateType plateType)
{
string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes());
try
{
var request = new PlateRequestEngineModel
{
Img64 = plateImageBase64,
Mode = plateType == PlateType.Square ? "square" : "long",
Display = "full"
};
HttpResponseMessage response = await httpClientEngine.PostAsJsonAsync("/get-from-frame", request);
response.EnsureSuccessStatusCode();
var ocrResult = await response.Content.ReadAsAsync<OcrResult>();
return ocrResult;
}
catch (Exception ex)
{
Console.WriteLine($"SendEngineRequest : {ex.Message}");
return new OcrResult();
}
}
}
}

View File

@ -26,6 +26,7 @@ namespace AIParkingApplication
string overviewStream,
IDoorControlAccess doorControlAccess,
ApiController apiController,
EngineApiController engineApiController,
bool isSupportSquarePlate = true,
bool isSupportLongPlate = false,
bool isAutoOpenDoor = true,
@ -33,7 +34,6 @@ namespace AIParkingApplication
{
InitializeComponent();
this.doorId = doorId;
this.isSupportSquarePlate = isSupportSquarePlate;
this.isSupportLongPlate = isSupportLongPlate;
this.isAutoOpenDoor = isAutoOpenDoor;
@ -50,7 +50,7 @@ namespace AIParkingApplication
overviewCamera.OnVideoFrameReceived += OverviewCameraOnVideoFrameReceived;
overviewCamera.OnOpenVideoStreamFailed += OverviewCamera_OnOpenVideoStreamFailed;
plateProcessor = new PlateProcessor(this.isSupportSquarePlate, this.isSupportLongPlate);
plateProcessor = new PlateProcessor(engineApiController, this.isSupportSquarePlate, this.isSupportLongPlate);
}
private async void C3Device_OnNewCardReceived(int doorId, string cardNumber)

View File

@ -26,6 +26,7 @@ namespace AIParkingApplication
string overviewStream,
IDoorControlAccess doorControlAccess,
ApiController apiController,
EngineApiController engineApiController,
bool isSupportSquarePlate = true,
bool isSupportLongPlate = false,
bool isAutoOpenDoor = true,
@ -33,7 +34,6 @@ namespace AIParkingApplication
{
InitializeComponent();
this.doorId = doorId;
this.isSupportSquarePlate = isSupportSquarePlate;
this.isSupportLongPlate = isSupportLongPlate;
this.isAutoOpenDoor = isAutoOpenDoor;
@ -50,7 +50,7 @@ namespace AIParkingApplication
overviewCamera.OnVideoFrameReceived += OverviewCameraOnVideoFrameReceived;
overviewCamera.OnOpenVideoStreamFailed += OverviewCamera_OnOpenVideoStreamFailed;
plateProcessor = new PlateProcessor(this.isSupportSquarePlate, this.isSupportLongPlate);
plateProcessor = new PlateProcessor(engineApiController, this.isSupportSquarePlate, this.isSupportLongPlate);
}
private async void C3Device_OnNewCardReceived(int doorId, string cardNumber)
@ -203,7 +203,7 @@ namespace AIParkingApplication
}
else
{
lblStatusInfo.UpdateLabel($"MỜI XE {plateStringEngine} QUA", Color.Red);
lblStatusInfo.UpdateLabel($"MỜI XE {plateStringEngine} QUA", Color.Green);
}
}
}

View File

@ -2,7 +2,6 @@
using OpenCvSharp.Extensions;
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
namespace AIParkingApplication
@ -13,11 +12,13 @@ namespace AIParkingApplication
private bool isSupportLongPlate;
private PlateDetector squarePlateDetector;
private PlateDetector longPlateDetector;
private EngineApiController engineApiController;
public PlateProcessor(bool isSupportSquarePlate = true, bool isSupportLongPlate = false)
public PlateProcessor(EngineApiController engineApiController, bool isSupportSquarePlate = true, bool isSupportLongPlate = false)
{
this.isSupportSquarePlate = isSupportSquarePlate;
this.isSupportLongPlate = isSupportLongPlate;
this.engineApiController = engineApiController;
if (this.isSupportSquarePlate)
{
@ -35,7 +36,7 @@ namespace AIParkingApplication
Mat plateDetected = plateType == PlateType.Square ? squarePlateDetector.DetectPlate(frame) : longPlateDetector.DetectPlate(frame);
//TODO: Check if plateDetected empty
OcrResult plateOcrResultFromEngine = await Util.SendEngineRequestAsync(plateDetected, plateType);
OcrResult plateOcrResultFromEngine = await engineApiController.SendEngineRequestAsync(plateDetected, plateType);
Mat finalPlateImage;
if (!string.IsNullOrEmpty(plateOcrResultFromEngine.PlateString) && !string.IsNullOrEmpty(plateOcrResultFromEngine.PlateImageBase64))
{

View File

@ -14,8 +14,6 @@ namespace AIParkingApplication
{
public static class Util
{
private static HttpClient httpClientEngine = new HttpClient { BaseAddress = new Uri("http://localhost:8080/"), Timeout = TimeSpan.FromMilliseconds(5000) };
public static bool IsPingable(string destinationIPAddress, out long pingTime, int timeOut = UtilConstant.PING_TIMEOUT_MS)
{
var pingTask = Task.Factory.StartNew(async () =>
@ -56,29 +54,6 @@ namespace AIParkingApplication
return sf.GetMethod().Name;
}
public static async Task<OcrResult> SendEngineRequestAsync(Mat plateImage, PlateType plateType)
{
string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes());
try
{
var request = new PlateRequestEngineModel
{
Img64 = plateImageBase64,
Mode = plateType == PlateType.Square ? "square" : "long",
Display = "full"
};
HttpResponseMessage response = await httpClientEngine.PostAsJsonAsync("/get-from-frame", request);
response.EnsureSuccessStatusCode();
var ocrResult = await response.Content.ReadAsAsync<OcrResult>();
return ocrResult;
}
catch (Exception ex)
{
Console.WriteLine($"SendEngineRequest : {ex.Message}");
return new OcrResult();
}
}
public static void UpdateImage(this PictureBox pictureBox, Bitmap image)
{
if (pictureBox.IsDisposed)