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

View File

@ -29,7 +29,7 @@ namespace AIParkingApplication
private System.Drawing.Point StartPoint { get; set; } private System.Drawing.Point StartPoint { get; set; }
private System.Drawing.Point CurrentCursorPosition { get; set; } private System.Drawing.Point CurrentCursorPosition { get; set; }
private Rectangle roiBox; private Rectangle pictureBoxRoi;
private bool IsDrawBox { get; set; } private bool IsDrawBox { get; set; }
public LaneOut(int doorId, public LaneOut(int doorId,
@ -195,11 +195,14 @@ namespace AIParkingApplication
} }
private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode) private async Task<FinalPlateResult> ProcessFrameImage(PlateProcessor plateProcessor, Mat frame, bool isRetryMode)
{
try
{ {
var starTime = DateTime.Now; var starTime = DateTime.Now;
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720)); Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
Rect roiBoxRect = Util.convertRectangleToRect(pictureBoxRoi, pictureBoxPlateVideo);
Mat frameRoi = frame[roiBoxRect];
try
{
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frame); FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(frame);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString)) if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
@ -326,7 +329,7 @@ namespace AIParkingApplication
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
{ {
StartPoint = e.Location; 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; IsDrawBox = true;
} }
} }
@ -336,19 +339,19 @@ namespace AIParkingApplication
if (IsDrawBox) if (IsDrawBox)
{ {
CurrentCursorPosition = e.Location; CurrentCursorPosition = e.Location;
roiBox.Width = CurrentCursorPosition.X - StartPoint.X; pictureBoxRoi.Width = CurrentCursorPosition.X - StartPoint.X;
roiBox.Height = CurrentCursorPosition.Y - StartPoint.Y; pictureBoxRoi.Height = CurrentCursorPosition.Y - StartPoint.Y;
} }
Refresh(); Refresh();
} }
private void pictureBoxPlateVideo_MouseUp(object sender, MouseEventArgs e) 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; pictureBoxRoi.X = pictureBoxRoi.Y = 0;
roiBox.Width = pictureBoxPlateVideo.Width; pictureBoxRoi.Width = pictureBoxPlateVideo.Width;
roiBox.Height = pictureBoxPlateVideo.Height; pictureBoxRoi.Height = pictureBoxPlateVideo.Height;
} }
IsDrawBox = false; IsDrawBox = false;
} }
@ -356,7 +359,7 @@ namespace AIParkingApplication
private void pictureBoxPlateVideo_Paint(object sender, PaintEventArgs e) private void pictureBoxPlateVideo_Paint(object sender, PaintEventArgs e)
{ {
Pen redPen = new Pen(Color.FromArgb(255, 255, 0, 0), 4); 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 Newtonsoft.Json;
using OpenCvSharp;
using System; using System;
using System.Configuration; using System.Configuration;
using System.Diagnostics; using System.Diagnostics;
@ -7,6 +8,7 @@ using System.IO;
using System.Net; using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms;
namespace AIParkingApplication namespace AIParkingApplication
{ {
@ -135,6 +137,18 @@ namespace AIParkingApplication
Console.WriteLine($"{DateTime.Now.GetTimeFormatted()}\tAddOrUpdateAppSettings\t{ex.Message}"); 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 public class PlateRequestEngineModel