AIParkingApplication/AIParkingApplication/LaneIn.cs

228 lines
8.0 KiB
C#

using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;
namespace AIParkingApplication
{
public partial class LaneIn : UserControl, ILane
{
private Camera overviewCamera;
private Camera plateCamera;
private PlateProcessor plateProcessor;
private int doorId;
private bool isSupportSquarePlate;
private bool isSupportLongPlate;
private bool isAutoOpenDoor;
private bool isRetryMode;
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,
bool isRetryMode = false)
{
InitializeComponent();
this.doorId = doorId;
this.isSupportSquarePlate = isSupportSquarePlate;
this.isSupportLongPlate = isSupportLongPlate;
this.isAutoOpenDoor = isAutoOpenDoor;
this.isRetryMode = isRetryMode;
overviewCamera = new Camera(overviewStream);
plateCamera = new Camera(plateStream);
this.apiController = apiController;
this.c3Device = c3Device;
this.c3Device.OnNewCardReceived += C3Device_OnNewCardReceived;
plateCamera.OnVideoFrameReceived += PlateCameraOnVideoFrameReceived;
plateCamera.OnOpenVideoStreamFailed += PlateCamera_OnOpenVideoStreamFailed;
overviewCamera.OnVideoFrameReceived += OverviewCameraOnVideoFrameReceived;
overviewCamera.OnOpenVideoStreamFailed += OverviewCamera_OnOpenVideoStreamFailed;
plateProcessor = new PlateProcessor(this.isSupportSquarePlate, this.isSupportLongPlate);
if (!this.c3Device.Connect().HasError)
{
_ = this.c3Device.GetLogToReceiveNewCard();
}
}
private async void C3Device_OnNewCardReceived(int doorId, string cardNumber)
{
var cardInfoResult = await apiController.CheckCard(cardNumber);
if (cardInfoResult.IsValid)
{
plateCamera.RequestCaptureOneFrame();
overviewCamera.RequestCaptureOneFrame();
await Task.Delay(200);
var plateVideoFrame = plateCamera.CurrentFrame;
var overviewVideoFrame = overviewCamera.CurrentFrame;
pictureBoxOverviewImage.Invoke(new Action(() =>
{
pictureBoxOverviewImage.Image?.Dispose();
pictureBoxOverviewImage.Image = overviewVideoFrame.ToBitmap();
}));
FinalPlateResult result = await ProcessFrameImage(plateProcessor, plateVideoFrame, isRetryMode);
pictureBoxPlateImage.Invoke(new Action(() =>
{
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)
{
pictureBoxPlateVideo.Invoke(new Action(() =>
{
pictureBoxPlateVideo.Image?.Dispose();
pictureBoxPlateVideo.Image = videoFrame.ToBitmap();
}));
}
private void OverviewCamera_OnOpenVideoStreamFailed(Mat videoFrame)
{
pictureBoxOverviewVideo.Invoke(new Action(() =>
{
pictureBoxOverviewImage.Image?.Dispose();
pictureBoxOverviewVideo.Image = videoFrame.ToBitmap();
}));
}
private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode)
{
try
{
//TODO: check size before resizing
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frame);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
{
Console.WriteLine("ProcessFrameImage Retry Mode");
Thread.Sleep(1000);
overviewCamera.RequestCaptureOneFrame();
finalPlateResult = await plateProcessor.ProcessPlate(frame);
}
return finalPlateResult;
}
catch (Exception ex)
{
Console.WriteLine($"ProcessFrameImage\texMessage: {ex.Message}");
return new FinalPlateResult
{
PlateImage = frame.ToBitmap(),
PlateString = string.Empty
};
}
}
public void Stop()
{
plateCamera.Stop();
overviewCamera.Stop();
}
public void Start()
{
plateCamera.Start();
overviewCamera.Start();
}
private void ShowCardInfoOnUI(string cardNumber, string plateString, string cardType, string cardTime)
{
lblCardNumber.Invoke(new Action(() =>
{
lblCardNumber.Text = $"Số thẻ: {cardNumber}";
}));
lblPlateString.Invoke(new Action(() =>
{
lblPlateString.Text = $"Biển số: {plateString}";
}));
lblCardType.Invoke(new Action(() =>
{
lblCardType.Text = $"Loại thẻ: {cardType}";
}));
lblCardTime.Invoke(new Action(() =>
{
lblCardTime.Text = $"Thời gian: {cardTime}";
}));
lblRecogizePlateStatus.Invoke(new Action(() =>
{
lblRecogizePlateStatus.Text = string.IsNullOrEmpty(plateString) ? "KHÔNG NHẬN DIỆN ĐƯỢC BIỂN SỐ" : $"MỜI XE {plateString} VÀO";
lblRecogizePlateStatus.BackColor = string.IsNullOrEmpty(plateString) ? System.Drawing.Color.Red : System.Drawing.Color.Green;
}));
}
private void OverviewCameraOnVideoFrameReceived(Mat videoFrame)
{
try
{
pictureBoxOverviewVideo.Invoke(new Action(() =>
{
pictureBoxOverviewVideo.Image?.Dispose();
pictureBoxOverviewVideo.Image = videoFrame.ToBitmap();
}));
}
catch (Exception ex)
{
Console.WriteLine($"{Util.GetCurrentMethodName()}\texMessage: {ex.Message}");
}
}
private void PlateCameraOnVideoFrameReceived(Mat videoFrame)
{
try
{
pictureBoxPlateVideo.Invoke(new Action(() =>
{
pictureBoxPlateVideo.Image?.Dispose();
pictureBoxPlateVideo.Image = videoFrame.ToBitmap();
}));
}
catch (Exception ex)
{
Console.WriteLine($"{Util.GetCurrentMethodName()}\texMessage: {ex.Message}");
}
}
}
}