Compare commits

..

3 Commits

3 changed files with 209 additions and 31 deletions

View File

@ -76,6 +76,7 @@
<Compile Include="AIParkingApplicationForm.Designer.cs"> <Compile Include="AIParkingApplicationForm.Designer.cs">
<DependentUpon>AIParkingApplicationForm.cs</DependentUpon> <DependentUpon>AIParkingApplicationForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="ApiController.cs" />
<Compile Include="AppConstant.cs" /> <Compile Include="AppConstant.cs" />
<Compile Include="C3DeviceController.cs" /> <Compile Include="C3DeviceController.cs" />
<Compile Include="Camera.cs" /> <Compile Include="Camera.cs" />

View File

@ -0,0 +1,180 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using OpenCvSharp;
using Newtonsoft.Json;
namespace AIParkingApplication
{
public class ApiController : IDisposable
{
private HttpClient httpClient;
private bool isHttpClientDisposabled;
private int numberOfRetry;
private ApiPath apiPath;
public ApiController(string baseAddress, int numberOfRetry = 5)
{
httpClient = new HttpClient
{
BaseAddress = new Uri(baseAddress),
Timeout = TimeSpan.FromSeconds(5)
};
isHttpClientDisposabled = false;
this.numberOfRetry = numberOfRetry;
}
~ApiController()
{
Dispose();
}
//TODO Old API
public async Task<LoginResponseModel> Login(LoginModel loginData)
{
try
{
HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData);
response.EnsureSuccessStatusCode();
var loginResult = await response.Content.ReadAsAsync<LoginResponseModel>();
return loginResult;
}
catch (Exception ex)
{
Console.WriteLine($"SendEngineRequest : {ex.Message}");
return new LoginResponseModel
{
IsLoggedIn = false
};
}
}
//TODO For New API
//public async Task<LoginResponseModel> Login(LoginModel loginData)
//{
// try
// {
// HttpResponseMessage response = await httpClient.PostAsJsonAsync("/api/login", loginData);
// response.EnsureSuccessStatusCode();
// var loginResult = await response.Content.ReadAsAsync<LoginResponseModel>();
// return loginResult;
// }
// catch (Exception ex)
// {
// Console.WriteLine($"SendEngineRequest : {ex.Message}");
// return new LoginResponseModel
// {
// IsLoggedIn = false
// };
// }
//}
public async void GetApiPathFromServer()
{
try
{
var request = new PlateRequestEngineModel
{
};
HttpResponseMessage response = await httpClient.PostAsJsonAsync("/get-from-frame", request);
response.EnsureSuccessStatusCode();
var ocrResult = await response.Content.ReadAsAsync<OcrResult>();
}
catch (Exception ex)
{
Console.WriteLine($"SendEngineRequest : {ex.Message}");
}
}
public async Task<OcrResult> SaveLogIn(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 httpClient.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 void Dispose()
{
if (httpClient != null && !isHttpClientDisposabled)
{
isHttpClientDisposabled = true;
httpClient.Dispose();
}
}
}
public class ApiPath
{
[JsonProperty("savelogsin")] //wtf is this from server.
public string SaveVehicleIn { get; set; }
}
public class LoginModel
{
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}
public class LoginResponseOldModel
{
}
public class LoginResponseModel
{
[JsonProperty("")]
public bool IsLoggedIn { get; set; }
}
public class PlateLogModel
{
[JsonProperty("card")]
public string CardID { get; set; }
[JsonProperty("plate")]
public string PlateString { get; set; }
[JsonProperty("type")]
public string LaneType { get; set; }
[JsonProperty("mod")]
public string PlateMode { get; set; } //0 or 1
[JsonProperty("camera")]
public string CameraID { get; set; }
[JsonProperty("time")]
public string Time { get; set; }
[JsonProperty("plateImage")]
public string PlateImageBase64 { get; set; }
[JsonProperty("plateResultImage")]
public string PlateResultImageBase64 { get; set; }
[JsonProperty("plateFrameImage")]
public string PlateFrameImageBase64 { get; set; }
[JsonProperty("frameImage")]
public string FrameImageBase64 { get; set; }
}
}

View File

@ -106,45 +106,42 @@ namespace AIParkingApplication
} }
} }
private void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame) private async void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame)
{ {
Task.Factory.StartNew(new Action(async () => try
{ {
try //TODO: check size before resizing
{ Cv2.Resize(videoFrame, videoFrame, new Size(1280, 720));
//TODO: check size before resizing FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
Cv2.Resize(videoFrame, videoFrame, new Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString)) if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
{
if (isRetryModeUntilOk) //TODO: TestMode
{ {
if (isRetryModeUntilOk) //TODO: TestMode Thread.Sleep(500);
{ CaptureAllCamera();
Thread.Sleep(500);
CaptureAllCamera();
}
else
{
Console.WriteLine("PlateCamera_OnOneVideoFrameRequested Retry Mode");
Thread.Sleep(1000);
overviewCamera.RequestCaptureOneFrame();
finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
}
} }
else
pictureBoxPlateImage.Invoke(new Action(() =>
{ {
pictureBoxPlateImage.Image?.Dispose(); Console.WriteLine("PlateCamera_OnOneVideoFrameRequested Retry Mode");
pictureBoxPlateImage.Image = finalPlateResult.PlateImage; Thread.Sleep(1000);
})); overviewCamera.RequestCaptureOneFrame();
finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
}
}
ShowCardInfoOnUI("224", finalPlateResult.PlateString, "Thẻ tháng", DateTime.Now.ToString(AppConstant.DATETIME_FORMAT)); pictureBoxPlateImage.Invoke(new Action(() =>
}
catch (Exception ex)
{ {
Console.WriteLine($"PlateCamera_OnOneVideoFrameRequested\texMessage: {ex.Message}"); pictureBoxPlateImage.Image?.Dispose();
} pictureBoxPlateImage.Image = finalPlateResult.PlateImage;
})); }));
ShowCardInfoOnUI("224", finalPlateResult.PlateString, "Thẻ tháng", DateTime.Now.ToString(AppConstant.DATETIME_FORMAT));
}
catch (Exception ex)
{
Console.WriteLine($"PlateCamera_OnOneVideoFrameRequested\texMessage: {ex.Message}");
}
} }
public void Stop() public void Stop()