Compare commits

...

7 Commits

8 changed files with 200 additions and 92 deletions

View File

@ -78,7 +78,6 @@
<Compile Include="AppConstant.cs" />
<Compile Include="C3DeviceController.cs" />
<Compile Include="Camera.cs" />
<Compile Include="Lane.cs" />
<Compile Include="LaneIn.cs">
<SubType>UserControl</SubType>
</Compile>
@ -92,6 +91,7 @@
<DependentUpon>LaneOut.cs</DependentUpon>
</Compile>
<Compile Include="PlateDetector.cs" />
<Compile Include="PlateProcessor.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StatusBar.cs">

View File

@ -17,8 +17,8 @@ namespace AIParkingApplication
public Camera(string streamUrl)
{
this.streamUrl = streamUrl;
this.isCapturing = true;
this.isFrameRequested = false;
isCapturing = true;
isFrameRequested = false;
lockSyncObject = new object();
}

View File

@ -1,22 +0,0 @@
namespace AIParkingApplication
{
public class Lane
{
private LaneType laneType;
private string vehicleVideoStream;
private string plateVideoStream;
public Lane(LaneType laneType, string vehicleVideoStream, string plateVideoStream)
{
this.laneType = laneType;
this.vehicleVideoStream = vehicleVideoStream;
this.plateVideoStream = plateVideoStream;
}
}
public enum LaneType
{
IN,
OUT
}
}

View File

@ -38,10 +38,10 @@
this.lblCardType = new System.Windows.Forms.Label();
this.lblCardNumber = new System.Windows.Forms.Label();
this.grbCardInformation = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.lblCardTime = new System.Windows.Forms.Label();
this.lblPlateString = new System.Windows.Forms.Label();
this.lblRecogizePlateStatus = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxPlateImage)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxPlateVideo)).BeginInit();
this.grbPlateCamera.SuspendLayout();
@ -53,8 +53,7 @@
//
// pictureBoxPlateImage
//
this.pictureBoxPlateImage.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.pictureBoxPlateImage.Location = new System.Drawing.Point(226, 29);
this.pictureBoxPlateImage.Location = new System.Drawing.Point(226, 23);
this.pictureBoxPlateImage.Name = "pictureBoxPlateImage";
this.pictureBoxPlateImage.Size = new System.Drawing.Size(200, 200);
this.pictureBoxPlateImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
@ -63,8 +62,7 @@
//
// pictureBoxPlateVideo
//
this.pictureBoxPlateVideo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.pictureBoxPlateVideo.Location = new System.Drawing.Point(15, 29);
this.pictureBoxPlateVideo.Location = new System.Drawing.Point(15, 23);
this.pictureBoxPlateVideo.Name = "pictureBoxPlateVideo";
this.pictureBoxPlateVideo.Size = new System.Drawing.Size(200, 200);
this.pictureBoxPlateVideo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
@ -76,16 +74,17 @@
this.grbPlateCamera.Controls.Add(this.pictureBoxPlateImage);
this.grbPlateCamera.Controls.Add(this.pictureBoxPlateVideo);
this.grbPlateCamera.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.grbPlateCamera.Location = new System.Drawing.Point(10, 199);
this.grbPlateCamera.Location = new System.Drawing.Point(1, 208);
this.grbPlateCamera.Margin = new System.Windows.Forms.Padding(0);
this.grbPlateCamera.Name = "grbPlateCamera";
this.grbPlateCamera.Size = new System.Drawing.Size(441, 246);
this.grbPlateCamera.Size = new System.Drawing.Size(441, 237);
this.grbPlateCamera.TabIndex = 12;
this.grbPlateCamera.TabStop = false;
this.grbPlateCamera.Text = "Camera biển số";
//
// pictureBoxOverviewImage
//
this.pictureBoxOverviewImage.Location = new System.Drawing.Point(226, 30);
this.pictureBoxOverviewImage.Location = new System.Drawing.Point(226, 24);
this.pictureBoxOverviewImage.Name = "pictureBoxOverviewImage";
this.pictureBoxOverviewImage.Size = new System.Drawing.Size(200, 200);
this.pictureBoxOverviewImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
@ -94,7 +93,7 @@
//
// pictureBoxOverviewVideo
//
this.pictureBoxOverviewVideo.Location = new System.Drawing.Point(15, 30);
this.pictureBoxOverviewVideo.Location = new System.Drawing.Point(15, 24);
this.pictureBoxOverviewVideo.Name = "pictureBoxOverviewVideo";
this.pictureBoxOverviewVideo.Size = new System.Drawing.Size(200, 200);
this.pictureBoxOverviewVideo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
@ -106,9 +105,10 @@
this.grbOverviewCamera.Controls.Add(this.pictureBoxOverviewImage);
this.grbOverviewCamera.Controls.Add(this.pictureBoxOverviewVideo);
this.grbOverviewCamera.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.grbOverviewCamera.Location = new System.Drawing.Point(10, 456);
this.grbOverviewCamera.Location = new System.Drawing.Point(1, 454);
this.grbOverviewCamera.Margin = new System.Windows.Forms.Padding(0);
this.grbOverviewCamera.Name = "grbOverviewCamera";
this.grbOverviewCamera.Size = new System.Drawing.Size(441, 247);
this.grbOverviewCamera.Size = new System.Drawing.Size(441, 238);
this.grbOverviewCamera.TabIndex = 11;
this.grbOverviewCamera.TabStop = false;
this.grbOverviewCamera.Text = "Camera toàn cảnh";
@ -119,7 +119,8 @@
this.lblLaneLabel.BackColor = System.Drawing.Color.Red;
this.lblLaneLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblLaneLabel.ForeColor = System.Drawing.SystemColors.Control;
this.lblLaneLabel.Location = new System.Drawing.Point(10, 8);
this.lblLaneLabel.Location = new System.Drawing.Point(1, 1);
this.lblLaneLabel.Margin = new System.Windows.Forms.Padding(0);
this.lblLaneLabel.Name = "lblLaneLabel";
this.lblLaneLabel.Padding = new System.Windows.Forms.Padding(138, 5, 137, 5);
this.lblLaneLabel.Size = new System.Drawing.Size(441, 49);
@ -130,7 +131,7 @@
// lblCardType
//
this.lblCardType.AutoSize = true;
this.lblCardType.Location = new System.Drawing.Point(12, 55);
this.lblCardType.Location = new System.Drawing.Point(12, 64);
this.lblCardType.Name = "lblCardType";
this.lblCardType.Size = new System.Drawing.Size(60, 18);
this.lblCardType.TabIndex = 0;
@ -139,7 +140,7 @@
// lblCardNumber
//
this.lblCardNumber.AutoSize = true;
this.lblCardNumber.Location = new System.Drawing.Point(12, 22);
this.lblCardNumber.Location = new System.Drawing.Point(12, 31);
this.lblCardNumber.Name = "lblCardNumber";
this.lblCardNumber.Size = new System.Drawing.Size(51, 18);
this.lblCardNumber.TabIndex = 0;
@ -153,17 +154,28 @@
this.grbCardInformation.Controls.Add(this.lblPlateString);
this.grbCardInformation.Controls.Add(this.lblCardNumber);
this.grbCardInformation.Font = new System.Drawing.Font("Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.grbCardInformation.Location = new System.Drawing.Point(10, 61);
this.grbCardInformation.Location = new System.Drawing.Point(1, 55);
this.grbCardInformation.Margin = new System.Windows.Forms.Padding(0);
this.grbCardInformation.Name = "grbCardInformation";
this.grbCardInformation.Size = new System.Drawing.Size(441, 82);
this.grbCardInformation.Size = new System.Drawing.Size(441, 104);
this.grbCardInformation.TabIndex = 13;
this.grbCardInformation.TabStop = false;
this.grbCardInformation.Text = "Thông tin thẻ";
//
// button1
//
this.button1.Location = new System.Drawing.Point(360, 20);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// lblCardTime
//
this.lblCardTime.AutoSize = true;
this.lblCardTime.Location = new System.Drawing.Point(218, 55);
this.lblCardTime.Location = new System.Drawing.Point(223, 64);
this.lblCardTime.Name = "lblCardTime";
this.lblCardTime.Size = new System.Drawing.Size(68, 18);
this.lblCardTime.TabIndex = 0;
@ -172,7 +184,7 @@
// lblPlateString
//
this.lblPlateString.AutoSize = true;
this.lblPlateString.Location = new System.Drawing.Point(218, 22);
this.lblPlateString.Location = new System.Drawing.Point(223, 31);
this.lblPlateString.Name = "lblPlateString";
this.lblPlateString.Size = new System.Drawing.Size(58, 18);
this.lblPlateString.TabIndex = 0;
@ -184,7 +196,8 @@
this.lblRecogizePlateStatus.BackColor = System.Drawing.Color.Green;
this.lblRecogizePlateStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblRecogizePlateStatus.ForeColor = System.Drawing.SystemColors.Control;
this.lblRecogizePlateStatus.Location = new System.Drawing.Point(10, 150);
this.lblRecogizePlateStatus.Location = new System.Drawing.Point(1, 159);
this.lblRecogizePlateStatus.Margin = new System.Windows.Forms.Padding(0);
this.lblRecogizePlateStatus.Name = "lblRecogizePlateStatus";
this.lblRecogizePlateStatus.Padding = new System.Windows.Forms.Padding(12, 6, 12, 6);
this.lblRecogizePlateStatus.Size = new System.Drawing.Size(441, 41);
@ -192,16 +205,6 @@
this.lblRecogizePlateStatus.Text = "KHÔNG NHẬN DIỆN ĐƯỢC BIỂN SỐ";
this.lblRecogizePlateStatus.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button1
//
this.button1.Location = new System.Drawing.Point(315, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// LaneIn
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -211,8 +214,9 @@
this.Controls.Add(this.grbOverviewCamera);
this.Controls.Add(this.lblLaneLabel);
this.Controls.Add(this.grbCardInformation);
this.Margin = new System.Windows.Forms.Padding(0);
this.Name = "LaneIn";
this.Size = new System.Drawing.Size(460, 710);
this.Size = new System.Drawing.Size(443, 692);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxPlateImage)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxPlateVideo)).EndInit();
this.grbPlateCamera.ResumeLayout(false);

View File

@ -1,8 +1,7 @@
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AIParkingApplication
@ -11,18 +10,20 @@ namespace AIParkingApplication
{
private Camera overviewCamera;
private Camera plateCamera;
private PlateDetector squarePlateDetector;
private PlateProcessor plateProcessor;
public LaneIn()
{
InitializeComponent();
string overviewCameraVideo = @"rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"; //@"C:\HS_test.mp4"
string plateCameraVideo = @"C:\HS_test.mp4";
//string plateCameraVideo = @"C:\CongRa_1.mp4";
overviewCamera = new Camera(overviewCameraVideo);
plateCamera = new Camera(plateCameraVideo);
plateCamera.OnVideoFrameReceived += PlateCameraOnVideoFrameReceived;
plateCamera.OnOneVideoFrameRequested += PlateCamera_OnOneVideoFrameRequested; ;
plateCamera.OnOneVideoFrameRequested += PlateCamera_OnOneVideoFrameRequested;
overviewCamera.OnVideoFrameReceived += OverviewCameraOnVideoFrameReceived;
overviewCamera.OnOneVideoFrameRequested += OverviewCamera_OnOneVideoFrameRequested;
@ -30,7 +31,7 @@ namespace AIParkingApplication
overviewCamera.Startcapture();
plateCamera.Startcapture();
squarePlateDetector = new PlateDetector(PlateType.Square, PlateDetectorConstant.MIN_SIZE_DEFAULT_SQUARE_PLATE, PlateDetectorConstant.MAX_SIZE_DEFAULT_SQUARE_PLATE, PlateDetectorConstant.SCALE_FACTOR_DEFAULT_SQUARE_PLATE, PlateDetectorConstant.MIN_NEIGHBORS_DEFAULT_SQUARE_PLATE);
plateProcessor = new PlateProcessor(true, false);
}
private void OverviewCamera_OnOneVideoFrameRequested(Mat videoFrame)
@ -44,37 +45,20 @@ namespace AIParkingApplication
private void PlateCamera_OnOneVideoFrameRequested(Mat videoFrame)
{
Cv2.Resize(videoFrame, videoFrame, new OpenCvSharp.Size(1280, 720));
Mat result = squarePlateDetector.DetectPlate(videoFrame);
Cv2.Resize(result, result, new OpenCvSharp.Size(272, 272));
var response = Util.SendEngineRequestAsync(result, PlateType.Square);
Bitmap resultPlateImage;
OcrResult ocrResult = response.Result;
if (!string.IsNullOrEmpty(ocrResult.Ocr) && !string.IsNullOrEmpty(ocrResult.Plate))
Task.Factory.StartNew(new Action(async () =>
{
var imageData = Convert.FromBase64String(ocrResult.Plate);
using (var ms = new MemoryStream(imageData))
var finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
lblPlateString.Invoke(new Action(() =>
{
resultPlateImage = new Bitmap(ms);
}
}
else
{
resultPlateImage = result.ToBitmap();
}
lblPlateString.Text = finalPlateResult.PlateString;
}));
lblPlateString.Invoke(new Action(() =>
{
lblPlateString.Text = ocrResult.Ocr;
}));
pictureBoxPlateImage.Invoke(new Action(() =>
{
pictureBoxPlateImage.Image?.Dispose();
pictureBoxPlateImage.Image = resultPlateImage;
pictureBoxPlateImage.Invoke(new Action(() =>
{
pictureBoxPlateImage.Image?.Dispose();
pictureBoxPlateImage.Image = finalPlateResult.PlateImage;
}));
}));
}
@ -98,8 +82,8 @@ namespace AIParkingApplication
private void CaptureAllCamera()
{
this.plateCamera.RequestCaptureOneFrame();
this.overviewCamera.RequestCaptureOneFrame();
plateCamera.RequestCaptureOneFrame();
overviewCamera.RequestCaptureOneFrame();
}
private void button1_Click(object sender, EventArgs e)

View File

@ -156,6 +156,15 @@
<metadata name="lblPlateString.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="button1.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="lblCardTime.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="lblPlateString.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="lblRecogizePlateStatus.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>

View File

@ -38,11 +38,38 @@ namespace AIParkingApplication
}
}
private Rect ExtendPlateRect(Rect plateRect, int extendWidth = 50, int extendHeigh = 50) //TODO: 50 for plate square.
{
int newX = plateRect.X - extendWidth;
int newY = plateRect.Y - extendHeigh;
int newWidth = plateRect.Width + extendWidth * 2;
int newHeigh = plateRect.Height + extendHeigh * 2;
return new Rect(newX, newY, newWidth, newHeigh);
}
private Mat GetBiggestPlate(Rect[] plateRects, Mat frame)
{
Rect biggestPlateRect = plateRects.OrderByDescending(x => x.Width).FirstOrDefault();
Console.WriteLine("===================================================");
for (int i = 0; i < plateRects.Length; i++)
{
Console.WriteLine($"{i}\t{plateRects[i].Width}\t{plateRects[i].Height}");
}
Console.WriteLine($"GetBiggestPlate - Width: {biggestPlateRect.Width}\tHeight: {biggestPlateRect.Height}");
Mat plateImage = frame[biggestPlateRect];
Rect extendedRect = ExtendPlateRect(biggestPlateRect);
//TODO: check oversize frame
Mat plateImage;
try
{
plateImage = frame[extendedRect];
}
catch (Exception ex)
{
Console.WriteLine("Error While Extending Plate Rect");
plateImage = frame[biggestPlateRect];
throw;
}
return plateImage;
}
}
@ -57,7 +84,7 @@ namespace AIParkingApplication
{
public const double SCALE_FACTOR_DEFAULT_SQUARE_PLATE = 1.03;
public static int MIN_NEIGHBORS_DEFAULT_SQUARE_PLATE = 2;
public static Size MIN_SIZE_DEFAULT_SQUARE_PLATE = new Size(80, 75);
public static Size MIN_SIZE_DEFAULT_SQUARE_PLATE = new Size(105, 100);
public static Size MAX_SIZE_DEFAULT_SQUARE_PLATE = new Size(450, 400);
public static double SCALE_FACTOR_DEFAULT_LONG_PLATE = 1.5;

View File

@ -0,0 +1,106 @@
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
namespace AIParkingApplication
{
public class PlateProcessor
{
private bool isSupportSquarePlate;
private bool isSupportLongPlate;
private PlateDetector squarePlateDetector;
private PlateDetector longPlateDetector;
public PlateProcessor(bool isSupportSquarePlate = true, bool isSupportLongPlate = false)
{
this.isSupportSquarePlate = isSupportSquarePlate;
this.isSupportLongPlate = isSupportLongPlate;
if (this.isSupportSquarePlate)
{
squarePlateDetector = new PlateDetector(PlateType.Square, PlateDetectorConstant.MIN_SIZE_DEFAULT_SQUARE_PLATE, PlateDetectorConstant.MAX_SIZE_DEFAULT_SQUARE_PLATE, PlateDetectorConstant.SCALE_FACTOR_DEFAULT_SQUARE_PLATE, PlateDetectorConstant.MIN_NEIGHBORS_DEFAULT_SQUARE_PLATE);
}
if (this.isSupportLongPlate)
{
longPlateDetector = new PlateDetector(PlateType.Long, PlateDetectorConstant.MIN_SIZE_DEFAULT_LONG_PLATE, PlateDetectorConstant.MAX_SIZE_DEFAULT_LONG_PLATE, PlateDetectorConstant.SCALE_FACTOR_DEFAULT_LONG_PLATE, PlateDetectorConstant.MIN_NEIGHBORS_DEFAULT_LONG_PLATE);
}
}
private async Task<FinalPlateResult> DetectPlateAndDoOcrEngineAsync(PlateType plateType, Mat frame)
{
//TODO: check size before resizing
Mat plateDetected = plateType == PlateType.Square ? squarePlateDetector.DetectPlate(frame) : longPlateDetector.DetectPlate(frame);
//TODO: check size before resizing
Cv2.Resize(plateDetected, plateDetected, new OpenCvSharp.Size(272, 272));
//TODO: Check if plateDetected empty
OcrResult plateOcrResultFromEngine = await Util.SendEngineRequestAsync(plateDetected, plateType);
Bitmap finalPlateImage;
if (!string.IsNullOrEmpty(plateOcrResultFromEngine.Ocr) && !string.IsNullOrEmpty(plateOcrResultFromEngine.Plate))
{
byte[] imageData = Convert.FromBase64String(plateOcrResultFromEngine.Plate);
using (var ms = new MemoryStream(imageData))
{
finalPlateImage = new Bitmap(ms);
}
}
else
{
finalPlateImage = plateDetected.ToBitmap();
}
return new FinalPlateResult
{
PlateString = plateOcrResultFromEngine.Ocr,
PlateImage = finalPlateImage
};
}
public async Task<FinalPlateResult> ProcessPlate(Mat frame)
{
//TODO: check size before resizing
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
FinalPlateResult plateResult;
if (isSupportLongPlate)
{
plateResult = await DetectPlateAndDoOcrEngineAsync(PlateType.Long, frame);
if (string.IsNullOrEmpty(plateResult.PlateString))
{
plateResult = await DetectPlateAndDoOcrEngineAsync(PlateType.Square, frame);
}
}
else
{
Console.WriteLine("Recall Square Plates");
plateResult = await DetectPlateAndDoOcrEngineAsync(PlateType.Square, frame);
}
return plateResult;
}
//TODO: Complete this
private string NormalizePlateString(string plateString)
{
return plateString;
}
//TODO: Complete this
private bool IsPlateStringValid(string plateString)
{
bool isValid = true;
return isValid;
}
public class FinalPlateResult
{
public string PlateString { get; set; }
public Bitmap PlateImage { get; set; }
}
}
}