232 lines
8.5 KiB
C#
232 lines
8.5 KiB
C#
using OpenCvSharp;
|
|
using OpenCvSharp.Extensions;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AIParkingApplication
|
|
{
|
|
public partial class LaneOut : 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 IDoorControlAccess doorControlAccess;
|
|
private ApiController apiController;
|
|
|
|
public LaneOut(int doorId,
|
|
string plateStream,
|
|
string overviewStream,
|
|
IDoorControlAccess doorControlAccess,
|
|
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.doorControlAccess = doorControlAccess;
|
|
this.doorControlAccess.OnNewCardReceived += C3Device_OnNewCardReceived;
|
|
|
|
plateCamera.OnVideoFrameReceived += PlateCameraOnVideoFrameReceived;
|
|
plateCamera.OnOpenVideoStreamFailed += PlateCamera_OnOpenVideoStreamFailed;
|
|
|
|
overviewCamera.OnVideoFrameReceived += OverviewCameraOnVideoFrameReceived;
|
|
overviewCamera.OnOpenVideoStreamFailed += OverviewCamera_OnOpenVideoStreamFailed;
|
|
|
|
plateProcessor = new PlateProcessor(this.isSupportSquarePlate, this.isSupportLongPlate);
|
|
}
|
|
|
|
private async void C3Device_OnNewCardReceived(int doorId, string cardNumber)
|
|
{
|
|
if (this.doorId != doorId)
|
|
{
|
|
return;
|
|
}
|
|
ClearPlateAndOverviewImage();
|
|
ClearPlateAndOverviewImageIn();
|
|
var cardInfoResult = await apiController.GetCardInformation(cardNumber);
|
|
if (!cardInfoResult.IsValid)
|
|
{
|
|
lblStatusInfo.UpdateLabel("THẺ KHÔNG HỢP LỆ", Color.Purple);
|
|
return;
|
|
}
|
|
|
|
if (cardInfoResult.Direction != "out")
|
|
{
|
|
lblStatusInfo.UpdateLabel("THẺ ĐÃ ĐƯỢC SỬ DỤNG", Color.Red);
|
|
return;
|
|
}
|
|
|
|
Bitmap plateImageIn = Util.ConvertFromBase64Image(cardInfoResult.PlateImageBase64);
|
|
Bitmap overviewImageIn = Util.ConvertFromBase64Image(cardInfoResult.FrameImageBase64);
|
|
|
|
pictureBoxPlateImageIn.UpdateImage(plateImageIn);
|
|
pictureBoxOverviewImageIn.UpdateImage(overviewImageIn);
|
|
|
|
plateCamera.RequestCaptureOneFrame();
|
|
overviewCamera.RequestCaptureOneFrame();
|
|
await Task.Delay(200);
|
|
|
|
var plateVideoFrame = plateCamera.CurrentFrame;
|
|
FinalPlateResult result = await ProcessFrameImage(plateProcessor, plateVideoFrame, isRetryMode);
|
|
pictureBoxPlateImage.UpdateImage(result.PlateImage.ToBitmap());
|
|
|
|
var overviewVideoFrame = overviewCamera.CurrentFrame;
|
|
pictureBoxOverviewImage.UpdateImage(overviewVideoFrame.ToBitmap());
|
|
|
|
var cardInformation = await apiController.GetCardInformation(cardNumber);
|
|
ShowCardInfoOnUI(cardNumber, result.PlateString, cardInformation.CardType, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT));
|
|
|
|
var saveLogResult = await apiController.SaveLog(LaneDirection.Out, cardInformation.CardRealID.ToString(), "1", result.PlateType, DateTime.Now.ToString(AppConstant.DATETIME_FORMAT), result.PlateString, result.PlateImage, result.PlateImage, result.PlateImage, overviewVideoFrame, cardInformation.LogID.ToString());
|
|
if (saveLogResult.Status)
|
|
{
|
|
if (isAutoOpenDoor)
|
|
{
|
|
OpenDoor(doorId);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lblStatusInfo.UpdateLabel("KHÔNG CÓ KẾT NỐI ĐẾN MÁY CHỦ", Color.Red);
|
|
}
|
|
}
|
|
|
|
private void PlateCamera_OnOpenVideoStreamFailed(Mat videoFrame)
|
|
{
|
|
pictureBoxPlateVideo.UpdateImage(videoFrame.ToBitmap());
|
|
}
|
|
|
|
private void OverviewCamera_OnOpenVideoStreamFailed(Mat videoFrame)
|
|
{
|
|
pictureBoxOverviewVideo.UpdateImage(videoFrame.ToBitmap());
|
|
}
|
|
|
|
private void ClearPlateAndOverviewImage()
|
|
{
|
|
pictureBoxPlateImage.UpdateImage(null);
|
|
pictureBoxOverviewImage.UpdateImage(null);
|
|
}
|
|
|
|
private void ClearPlateAndOverviewImageIn()
|
|
{
|
|
pictureBoxPlateImageIn.UpdateImage(null);
|
|
pictureBoxOverviewImageIn.UpdateImage(null);
|
|
}
|
|
|
|
private void OpenDoor(int doorId)
|
|
{
|
|
if (doorControlAccess.OpenDoor(doorId).HasError)
|
|
{
|
|
lblStatusInfo.UpdateLabel("KHÔNG THỂ MỞ CỬA", Color.Red);
|
|
}
|
|
}
|
|
|
|
private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode)
|
|
{
|
|
try
|
|
{
|
|
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,
|
|
PlateString = string.Empty,
|
|
PlateType = PlateType.Square
|
|
};
|
|
}
|
|
}
|
|
|
|
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.UpdateLabel($"Số thẻ: {cardNumber}");
|
|
lblPlateString.UpdateLabel($"Biển số: {plateString}");
|
|
lblCardType.UpdateLabel($"Loại thẻ: {cardType}");
|
|
lblCardTime.UpdateLabel($"Thời gian: {cardTime}");
|
|
lblStatusInfo.UpdateLabel(string.IsNullOrEmpty(plateString) ? "KHÔNG NHẬN DIỆN ĐƯỢC BIỂN SỐ" : $"MỜI XE {plateString} QUA", string.IsNullOrEmpty(plateString) ? Color.Red : Color.Green);
|
|
}
|
|
|
|
private void OverviewCameraOnVideoFrameReceived(Mat videoFrame)
|
|
{
|
|
try
|
|
{
|
|
pictureBoxOverviewVideo.UpdateImage(videoFrame.ToBitmap());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"{Util.GetCurrentMethodName()}\texMessage: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void PlateCameraOnVideoFrameReceived(Mat videoFrame)
|
|
{
|
|
try
|
|
{
|
|
pictureBoxPlateVideo.UpdateImage(videoFrame.ToBitmap());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"{Util.GetCurrentMethodName()}\texMessage: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void ConnectToDoorAccessControl()
|
|
{
|
|
if (!this.doorControlAccess.Connect().HasError)
|
|
{
|
|
_ = this.doorControlAccess.GetLogToReceiveNewCard();
|
|
}
|
|
else
|
|
{
|
|
lblStatusInfo.UpdateLabel("KHÔNG THỂ KẾT NỐI TỚI C3200", Color.Purple);
|
|
}
|
|
}
|
|
|
|
private void LaneIn_Load(object sender, EventArgs e)
|
|
{
|
|
ConnectToDoorAccessControl();
|
|
}
|
|
}
|
|
}
|