-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoProcessor.cs
More file actions
149 lines (123 loc) · 4.03 KB
/
VideoProcessor.cs
File metadata and controls
149 lines (123 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using Emgu.CV;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Windows;
using Image = System.Windows.Controls.Image;
using System.ComponentModel;
using Emgu.CV.CvEnum;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Diagnostics;
namespace Quay_Code
{
public partial class VideoProcessor
{
public bool _isProcessing;
Mat frameOut;
Image webcamImage;
Detect _dtc = new();
VideoCapture _vid;
public VideoProcessor(Image imageFrame)
{
StartFrameUpdate(imageFrame);
}
private void StartFrameUpdate(Image imageFrame)
{
webcamImage = imageFrame;
_vid = new(0, VideoCapture.API.DShow);
}
public void TurnOffCamera(Image imageFrame)
{
_isProcessing = false;
int width = (int)imageFrame.ActualWidth;
int height = (int)imageFrame.ActualHeight;
WriteableBitmap blackBitmap = new WriteableBitmap(width, height, 32, 32, PixelFormats.Bgra32, null);
int[] blackPixels = new int[width * height];
for(int i = 0; i < blackPixels.Length; i++)
{
blackPixels[i] = 171717;
}
blackBitmap.WritePixels(new Int32Rect(0, 0, width, height), blackPixels, width * 4, 0);
try
{
_vid.Dispose();
_vid = null;
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
webcamImage.Source = blackBitmap;
imageFrame.Source = blackBitmap;
}
public async void IdentifyFromVideo()
{
//_vid = new(0, VideoCapture.API.DShow);
_isProcessing = true;
try
{
Mat frame = new();
while (_isProcessing)
{
_vid.Read(frame);
//Need to implement alternative methods of detecting different types of codes.
//Same principle as FindSquares, do for Aruco and QR. Need more in depth ways of distinguishing Quay and QR.
Mat[] outputArray = _dtc.FindSquares(frame);
//CvInvoke.Imshow("ThreshMat", outputArray[0]);
frameOut = outputArray[1];
var bitmapSource = frame.ToImageSource();
webcamImage.Source = bitmapSource;
await Task.Delay(50);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error Initialising Webcam: {ex.Message}");
}
}
public Mat ToProcess()
{
return frameOut;
}
}
public static class EmguExtensions
{
public static ImageSource ToImageSource(this Mat mat)
{
return BitmapSourceConvert.ToBitmapSource(mat.ToBitmap());
}
}
public static class BitmapSourceConvert
{
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
public static BitmapSource ToBitmapSource(System.Drawing.Bitmap bitmap)
{
if (bitmap == null)
{
return null;
}
IntPtr hBitmap = IntPtr.Zero;
try
{
hBitmap = bitmap.GetHbitmap();
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
finally
{
if (hBitmap != IntPtr.Zero)
{
DeleteObject(hBitmap);
}
}
}
}
}