Thêm tính năng Cascade trong vùng ROI

This commit is contained in:
Le Chau 2020-07-17 15:19:12 +07:00
parent c0c8b325d0
commit 076290e141
3 changed files with 47 additions and 26 deletions

View File

@ -27,7 +27,7 @@ namespace AIParkingApplication
private System.Drawing.Point StartPoint { get; set; }
private System.Drawing.Point CurrentCursorPosition { get; set; }
private Rectangle roiBox;
private Rectangle pictureBoxRoi;
private bool IsDrawBox { get; set; }
public LaneIn(int doorId,
@ -152,17 +152,19 @@ namespace AIParkingApplication
private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode)
{
var starTime = DateTime.Now;
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
Rect roiBoxRect = Util.convertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo);
Mat frameRoi = frame[roiBoxRect];
try
{
var starTime = DateTime.Now;
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frame);
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frameRoi);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
{
Thread.Sleep(1000);
overviewCamera.RequestCaptureOneFrame();
finalPlateResult = await plateProcessor.ProcessPlate(frame);
finalPlateResult = await plateProcessor.ProcessPlate(frameRoi);
Console.WriteLine("ProcessFrameImage Retry Mode");
}
appLogger.Log(LogLevel.Info, $"ProcessFrameImage: {(DateTime.Now - starTime).TotalMilliseconds} ms");
@ -174,7 +176,7 @@ namespace AIParkingApplication
appLogger.Log(LogLevel.Info, $"ProcessFrameImage: ex: {ex.Message}");
return new FinalPlateResult
{
PlateImage = frame,
PlateImage = frameRoi,
PlateString = string.Empty,
PlateType = PlateType.Square
};
@ -246,7 +248,7 @@ namespace AIParkingApplication
if (e.Button == MouseButtons.Left)
{
StartPoint = e.Location;
roiBox = new Rectangle(StartPoint, new System.Drawing.Size(0, 0));
pictureBoxRoi = new Rectangle(StartPoint, new System.Drawing.Size(0, 0));
IsDrawBox = true;
}
}
@ -256,27 +258,29 @@ namespace AIParkingApplication
if (IsDrawBox)
{
CurrentCursorPosition = e.Location;
roiBox.Width = CurrentCursorPosition.X - StartPoint.X;
roiBox.Height = CurrentCursorPosition.Y - StartPoint.Y;
pictureBoxRoi.Width = CurrentCursorPosition.X - StartPoint.X;
pictureBoxRoi.Height = CurrentCursorPosition.Y - StartPoint.Y;
}
Refresh();
}
private void pictureBoxPlateVideo_MouseUp(object sender, MouseEventArgs e)
{
if (StartPoint.X + roiBox.Width > pictureBoxPlateVideo.Width || StartPoint.Y + roiBox.Height > pictureBoxPlateVideo.Height)
if (StartPoint.X + pictureBoxRoi.Width > pictureBoxPlateVideo.Width || StartPoint.Y + pictureBoxRoi.Height > pictureBoxPlateVideo.Height)
{
roiBox.X = roiBox.Y = 0;
roiBox.Width = pictureBoxPlateVideo.Width;
roiBox.Height = pictureBoxPlateVideo.Height;
pictureBoxRoi.X = pictureBoxRoi.Y = 0;
pictureBoxRoi.Width = pictureBoxPlateVideo.Width;
pictureBoxRoi.Height = pictureBoxPlateVideo.Height;
}
IsDrawBox = false;
}
private void pictureBoxPlateVideo_Paint(object sender, PaintEventArgs e)
{
Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 4);
e.Graphics.DrawRectangle(redPen, roiBox);
Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 3);
e.Graphics.DrawRectangle(redPen, pictureBoxRoi);
}
}
}

View File

@ -29,7 +29,7 @@ namespace AIParkingApplication
private System.Drawing.Point StartPoint { get; set; }
private System.Drawing.Point CurrentCursorPosition { get; set; }
private Rectangle roiBox;
private Rectangle pictureBoxRoi;
private bool IsDrawBox { get; set; }
public LaneOut(int doorId,
@ -196,10 +196,13 @@ namespace AIParkingApplication
private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode)
{
var starTime = DateTime.Now;
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
Rect roiBoxRect = Util.convertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo);
Mat frameRoi = frame[roiBoxRect];
try
{
var starTime = DateTime.Now;
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frame);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
@ -326,7 +329,7 @@ namespace AIParkingApplication
if (e.Button == MouseButtons.Left)
{
StartPoint = e.Location;
roiBox = new Rectangle(StartPoint, new System.Drawing.Size(0, 0));
pictureBoxRoi = new Rectangle(StartPoint, new System.Drawing.Size(0, 0));
IsDrawBox = true;
}
}
@ -336,19 +339,19 @@ namespace AIParkingApplication
if (IsDrawBox)
{
CurrentCursorPosition = e.Location;
roiBox.Width = CurrentCursorPosition.X - StartPoint.X;
roiBox.Height = CurrentCursorPosition.Y - StartPoint.Y;
pictureBoxRoi.Width = CurrentCursorPosition.X - StartPoint.X;
pictureBoxRoi.Height = CurrentCursorPosition.Y - StartPoint.Y;
}
Refresh();
}
private void pictureBoxPlateVideo_MouseUp(object sender, MouseEventArgs e)
{
if (StartPoint.X + roiBox.Width > pictureBoxPlateVideo.Width || StartPoint.Y + roiBox.Height > pictureBoxPlateVideo.Height)
if (StartPoint.X + pictureBoxRoi.Width > pictureBoxPlateVideo.Width || StartPoint.Y + pictureBoxRoi.Height > pictureBoxPlateVideo.Height)
{
roiBox.X = roiBox.Y = 0;
roiBox.Width = pictureBoxPlateVideo.Width;
roiBox.Height = pictureBoxPlateVideo.Height;
pictureBoxRoi.X = pictureBoxRoi.Y = 0;
pictureBoxRoi.Width = pictureBoxPlateVideo.Width;
pictureBoxRoi.Height = pictureBoxPlateVideo.Height;
}
IsDrawBox = false;
}
@ -356,7 +359,7 @@ namespace AIParkingApplication
private void pictureBoxPlateVideo_Paint(object sender, PaintEventArgs e)
{
Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 4);
e.Graphics.DrawRectangle(redPen, roiBox);
e.Graphics.DrawRectangle(redPen, pictureBoxRoi);
}
}
}

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Configuration;
using System.Diagnostics;
@ -7,6 +8,7 @@ using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AIParkingApplication
{
@ -135,6 +137,18 @@ namespace AIParkingApplication
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tAddOrUpdateAppSettings\t{ex.Message}");
}
}
public static Rect convertRectangleToRect(Rectangle pictureBoxRoi, PictureBox pictureBoxPlateVideo)
{
Rect temp = new Rect
{
X = pictureBoxRoi.X * 1280 / pictureBoxPlateVideo.Width,
Y = pictureBoxRoi.Y * 720 / pictureBoxPlateVideo.Height,
Width = pictureBoxRoi.Width * 1280 / pictureBoxPlateVideo.Width,
Height = pictureBoxRoi.Height * 720 / pictureBoxPlateVideo.Height
};
return temp;
}
}
public class PlateRequestEngineModel