Compare commits

..

5 Commits

4 changed files with 48 additions and 27 deletions

View File

@ -7,9 +7,8 @@ namespace AIParkingApplication
public class Camera
{
private string streamUrl;
private bool isCapturing;
private object lockSyncObject;
private volatile bool isFrameRequested;
private Thread readStreamThread;
public event CameraEvent OnVideoFrameReceived;
public event CameraEvent OnOneVideoFrameRequested;
@ -17,16 +16,24 @@ namespace AIParkingApplication
public Camera(string streamUrl)
{
this.streamUrl = streamUrl;
isCapturing = true;
isFrameRequested = false;
lockSyncObject = new object();
readStreamThread = new Thread(new ThreadStart(ReadVideoStream));
readStreamThread.IsBackground = true;
}
public void Startcapture()
~Camera()
{
Thread readVideoStreamThread = new Thread(new ThreadStart(ReadVideoStream));
readVideoStreamThread.IsBackground = true;
readVideoStreamThread.Start();
Stop();
}
public void Start()
{
if (!readStreamThread.IsAlive)
{
readStreamThread = new Thread(new ThreadStart(ReadVideoStream));
readStreamThread.IsBackground = true;
readStreamThread.Start();
}
}
public void RequestCaptureOneFrame()
@ -47,10 +54,6 @@ namespace AIParkingApplication
{
Thread.Sleep(50); //Stream Thread Sleep
//Thread.Sleep(40); //Video Thread Sleep
if (!isCapturing)
{
continue;
}
if (videoCapture.Read(videoFrame) && videoFrame.Width > 0 && videoFrame.Height > 0)
{
@ -64,11 +67,11 @@ namespace AIParkingApplication
}
}
public void PauseCapture()
public void Stop()
{
lock (lockSyncObject)
if (readStreamThread.IsAlive)
{
isCapturing = false;
readStreamThread.Abort();
}
}
}

View File

@ -33,9 +33,6 @@ namespace AIParkingApplication
overviewCamera.OnVideoFrameReceived += OverviewCameraOnVideoFrameReceived;
overviewCamera.OnOneVideoFrameRequested += OverviewCamera_OnOneVideoFrameRequested;
overviewCamera.Startcapture();
plateCamera.Startcapture();
plateProcessor = new PlateProcessor(this.isSupportSquarePlate, this.isSupportLongPlate);
}
@ -61,6 +58,8 @@ namespace AIParkingApplication
{
try
{
//TODO: check size before resizing
Cv2.Resize(videoFrame, videoFrame, new Size(1280, 720));
FinalPlateResult finalPlateResult = await plateProcessor.ProcessPlate(videoFrame);
if (isRetryMode && !plateProcessor.IsPlateStringValid(finalPlateResult.PlateString))
@ -97,6 +96,18 @@ namespace AIParkingApplication
}));
}
public void Stop()
{
plateCamera.Stop();
overviewCamera.Stop();
}
public void Start()
{
plateCamera.Start();
overviewCamera.Start();
}
private void OverviewCameraOnVideoFrameReceived(Mat videoFrame)
{
try

View File

@ -29,8 +29,17 @@ namespace AIParkingApplication
Console.WriteLine("Plate Detected Length: " + plateRectsDetected.Length);
if (plateRectsDetected.Length > 0)
{
Mat plateImage = GetBiggestPlate(plateRectsDetected, frame);
return plateImage;
try
{
Mat plateImage = GetBiggestPlate(plateRectsDetected, frame);
Cv2.Resize(plateImage, plateImage, new Size(272, 272));
return plateImage;
}
catch (Exception ex)
{
Console.WriteLine($"DetectPlate\t{ex.Message}");
return frame;
}
}
else
{

View File

@ -34,9 +34,6 @@ namespace AIParkingApplication
{
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;
@ -64,10 +61,7 @@ namespace AIParkingApplication
{
try
{
//TODO: check size before resizing
Cv2.Resize(frame, frame, new OpenCvSharp.Size(1280, 720));
FinalPlateResult plateResult;
if (isSupportLongPlate)
{
plateResult = await DetectPlateAndDoOcrEngineAsync(PlateType.Long, frame);
@ -87,7 +81,11 @@ namespace AIParkingApplication
catch (Exception ex)
{
Console.WriteLine($"{Util.GetCurrentMethodName()}\texMessage: {ex.Message}");
return new FinalPlateResult();
return new FinalPlateResult
{
PlateString = string.Empty,
PlateImage = frame.ToBitmap()
};
}
}