Compare commits

..

5 Commits

4 changed files with 64 additions and 23 deletions

View File

@ -8,8 +8,10 @@ namespace AIParkingApplication
public AIParkingApplicationForm()
{
InitializeComponent();
string serverBaseAddress = "http://localhost:80/";
ApiController apiController = new ApiController(serverBaseAddress);
C3DeviceController c3Device = new C3DeviceController("192.168.1.200");
LaneIn laneIn = new LaneIn(1, @"C:\HS_test.mp4", @"C:\HS_test.mp4", c3Device, true, false, true);
LaneIn laneIn = new LaneIn(1, @"C:\HS_test.mp4", @"C:\HS_test.mp4", c3Device, apiController, true, false, true);
//LaneIn laneOut = new LaneIn();
Controls.Add(laneIn);
laneIn.Start();

View File

@ -195,7 +195,7 @@ namespace AIParkingApplication
public class CardValidation
{
[JsonProperty("status")]
public string IsValid { get; set; }
public bool IsValid { get; set; }
[JsonProperty("type")]
public string Direction { get; set; }

View File

@ -10,6 +10,7 @@ namespace AIParkingApplication
private string streamUrl;
private volatile bool isFrameRequested;
private Thread readStreamThread;
public volatile Mat CurrentFrame;
public event CameraEvent OnVideoFrameReceived;
public event CameraEvent OnOneVideoFrameRequested;
@ -63,6 +64,7 @@ namespace AIParkingApplication
OnVideoFrameReceived?.Invoke(videoFrame);
if (isFrameRequested)
{
CurrentFrame = videoFrame;
OnOneVideoFrameRequested?.Invoke(videoFrame);
isFrameRequested = false;
}

View File

@ -1,4 +1,5 @@
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -19,11 +20,13 @@ namespace AIParkingApplication
private bool isRetryMode;
private bool isRetryModeUntilOk; //TODO: Test mode
private C3DeviceController c3Device;
private ApiController apiController;
public LaneIn(int doorId,
string plateStream,
string overviewStream,
C3DeviceController c3Device,
ApiController apiController,
bool isSupportSquarePlate = true,
bool isSupportLongPlate = false,
bool isAutoOpenDoor = true,
@ -32,6 +35,7 @@ namespace AIParkingApplication
{
InitializeComponent();
this.doorId = doorId;
this.isSupportSquarePlate = isSupportSquarePlate;
this.isSupportLongPlate = isSupportLongPlate;
this.isAutoOpenDoor = isAutoOpenDoor;
@ -39,6 +43,7 @@ namespace AIParkingApplication
this.isRetryModeUntilOk = isRetryModeUntilOk;
overviewCamera = new Camera(overviewStream);
plateCamera = new Camera(plateStream);
this.apiController = apiController;
this.c3Device = c3Device;
this.c3Device.OnNewCardReceived += C3Device_OnNewCardReceived;
@ -58,18 +63,42 @@ namespace AIParkingApplication
}
}
private void C3Device_OnNewCardReceived(int doorId, string cardNumber)
private async void C3Device_OnNewCardReceived(int doorId, string cardNumber)
{
//Request To Capture And Process Frame.
CaptureAllCamera();
//Request Card Info
if (isAutoOpenDoor)
var cardInfoResult = await apiController.CheckCard(cardNumber);
if (cardInfoResult.IsValid)
{
if (this.doorId == doorId)
plateCamera.RequestCaptureOneFrame();
await Task.Delay(500);
var videoFrame = plateCamera.CurrentFrame;
FinalPlateResult result = await ProcessFrameImage(plateProcessor, videoFrame, isRetryMode, isRetryModeUntilOk);
pictureBoxPlateImage.Invoke(new Action(() =>
{
c3Device.OpenDoor(doorId);
pictureBoxPlateImage.Image?.Dispose();
pictureBoxPlateImage.Image = result.PlateImage;
}));
var cardValidation = await apiController.CheckCard(cardNumber);
ShowCardInfoOnUI(cardNumber, result.PlateString, cardValidation.CardType, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT));
if (isAutoOpenDoor)
{
if (this.doorId == doorId)
{
c3Device.OpenDoor(doorId);
}
}
}
else
{
lblRecogizePlateStatus.Invoke(new Action(() =>
{
lblRecogizePlateStatus.BackColor = Color.Purple;
lblRecogizePlateStatus.Text = "THẺ KHÔNG HỢP LỆ";
}));
}
}
private void PlateCamera_OnOpenVideoStreamFailed(Mat videoFrame)
@ -106,13 +135,13 @@ namespace AIParkingApplication
}
}
private async void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame)
private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode, bool isRetryModeUntilOk)
{
try
{
//TODO: check size before resizing
Cv2.Resize(videoFrame, videoFrame, new Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frame);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
{
@ -123,27 +152,35 @@ namespace AIParkingApplication
}
else
{
Console.WriteLine("PlateCamera_OnOneVideoFrameRequested Retry Mode");
Console.WriteLine("ProcessFrameImage Retry Mode");
Thread.Sleep(1000);
overviewCamera.RequestCaptureOneFrame();
finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
finalPlateResult = await plateProcessor.ProcessPlate(frame);
}
}
pictureBoxPlateImage.Invoke(new Action(() =>
{
pictureBoxPlateImage.Image?.Dispose();
pictureBoxPlateImage.Image = finalPlateResult.PlateImage;
}));
ShowCardInfoOnUI("224", finalPlateResult.PlateString, "Thẻ tháng", DateTime.Now.ToString(AppConstant.DATETIME_FORMAT));
return finalPlateResult;
}
catch (Exception ex)
{
Console.WriteLine($"PlateCamera_OnOneVideoFrameRequested\texMessage: {ex.Message}");
Console.WriteLine($"ProcessFrameImage\texMessage: {ex.Message}");
return new FinalPlateResult
{
PlateImage = frame.ToBitmap(),
PlateString = string.Empty
};
}
}
private async void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame)
{
//FinalPlateResult result = await ProcessFrameImage(plateProcessor, videoFrame, isRetryMode, isRetryModeUntilOk);
//pictureBoxPlateImage.Invoke(new Action(() =>
//{
// pictureBoxPlateImage.Image?.Dispose();
// pictureBoxPlateImage.Image = result.PlateImage;
//}));
}
public void Stop()
{
plateCamera.Stop();