@@ -13,149 +13,149 @@ OpenCVFrameProvider::OpenCVFrameProvider()
13
13
14
14
OpenCVFrameProvider::~OpenCVFrameProvider ()
15
15
{
16
- DeleteCriticalSection (&lock);
16
+ DeleteCriticalSection (&lock);
17
17
}
18
18
19
19
HRESULT OpenCVFrameProvider::Initialize (ID3D11ShaderResourceView* colorSRV, ID3D11Texture2D* outputTexture)
20
20
{
21
- if (IsEnabled ())
22
- {
23
- return S_OK;
24
- }
25
-
26
- InitializeCriticalSection (&lock);
27
- InitializeCriticalSection (&frameAccessCriticalSection);
28
-
29
- _colorSRV = colorSRV;
30
- if (colorSRV != nullptr )
31
- {
32
- colorSRV->GetDevice (&_device);
33
- }
34
-
35
- HRESULT hr = E_PENDING;
36
- videoCapture = new cv::VideoCapture (CAMERA_ID);
37
-
38
- // Attempt to update camera resolution to desired resolution.
39
- // Note: This may fail, and your capture will resume at the camera's native resolution.
40
- // In this case, the Update loop will print an error with the expected frame resolution.
41
- videoCapture->set (cv::CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
42
- videoCapture->set (cv::CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
43
-
44
- videoCapture->open (CAMERA_ID);
45
- if (IsEnabled ())
46
- {
47
- hr = S_OK;
48
- }
49
-
50
- return hr;
21
+ if (IsEnabled ())
22
+ {
23
+ return S_OK;
24
+ }
25
+
26
+ InitializeCriticalSection (&lock);
27
+ InitializeCriticalSection (&frameAccessCriticalSection);
28
+
29
+ _colorSRV = colorSRV;
30
+ if (colorSRV != nullptr )
31
+ {
32
+ colorSRV->GetDevice (&_device);
33
+ }
34
+
35
+ HRESULT hr = E_PENDING;
36
+ videoCapture = new cv::VideoCapture (CAMERA_ID);
37
+
38
+ // Attempt to update camera resolution to desired resolution.
39
+ // Note: This may fail, and your capture will resume at the camera's native resolution.
40
+ // In this case, the Update loop will print an error with the expected frame resolution.
41
+ videoCapture->set (cv::CAP_PROP_FRAME_WIDTH, FRAME_WIDTH);
42
+ videoCapture->set (cv::CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT);
43
+
44
+ videoCapture->open (CAMERA_ID);
45
+ if (IsEnabled ())
46
+ {
47
+ hr = S_OK;
48
+ }
49
+
50
+ return hr;
51
51
}
52
52
53
53
void OpenCVFrameProvider::Update ()
54
54
{
55
- if (!IsEnabled () ||
56
- _colorSRV == nullptr ||
57
- _device == nullptr )
58
- {
59
- return ;
60
- }
61
-
62
- // Get frame time.
63
- LARGE_INTEGER time ;
64
- QueryPerformanceCounter (&time );
65
-
66
- if (videoCapture->grab ())
67
- {
68
- cachedTimestamp = time .QuadPart ;
69
-
70
- concurrency::create_task ([=]
71
- {
72
- cv::Mat frame;
73
- videoCapture->retrieve (frame);
74
-
75
- double width = videoCapture->get (cv::CAP_PROP_FRAME_WIDTH);
76
- double height = videoCapture->get (cv::CAP_PROP_FRAME_HEIGHT);
77
-
78
- if (width != FRAME_WIDTH)
79
- {
80
- OutputDebugString (L" ERROR: captured width does not equal FRAME_WIDTH. Expecting: " );
81
- OutputDebugString (std::to_wstring (width).c_str ());
82
- OutputDebugString (L" \n " );
83
- }
84
-
85
- if (height != FRAME_HEIGHT)
86
- {
87
- OutputDebugString (L" ERROR: captured height does not equal FRAME_HEIGHT. Expecting: " );
88
- OutputDebugString (std::to_wstring (height).c_str ());
89
- OutputDebugString (L" \n " );
90
- }
91
-
92
- // OpenCV returns frames in RGB, convert to BGRA
93
- DirectXHelper::ConvertRGBtoBGRA (frame.data , tmpData, width, height, true );
94
- EnterCriticalSection (&lock);
95
- dirtyFrame = false ;
96
-
97
- memcpy (cachedFrame, tmpData, FRAME_BUFSIZE);
98
-
99
- LeaveCriticalSection (&lock);
100
- });
101
-
102
- if (_device != nullptr && _colorSRV != nullptr )
103
- {
104
- EnterCriticalSection (&lock);
105
- if (!dirtyFrame)
106
- {
107
- DirectXHelper::UpdateSRV (_device, _colorSRV, cachedFrame, FRAME_WIDTH * FRAME_BPP);
108
- dirtyFrame = true ;
109
- }
110
- LeaveCriticalSection (&lock);
111
-
112
- EnterCriticalSection (&frameAccessCriticalSection);
113
- isVideoFrameReady = true ;
114
- LeaveCriticalSection (&frameAccessCriticalSection);
115
- }
116
- }
55
+ if (!IsEnabled () ||
56
+ _colorSRV == nullptr ||
57
+ _device == nullptr )
58
+ {
59
+ return ;
60
+ }
61
+
62
+ // Get frame time.
63
+ LARGE_INTEGER time ;
64
+ QueryPerformanceCounter (&time );
65
+
66
+ if (videoCapture->grab ())
67
+ {
68
+ cachedTimestamp = time .QuadPart ;
69
+
70
+ concurrency::create_task ([=]
71
+ {
72
+ cv::Mat frame;
73
+ videoCapture->retrieve (frame);
74
+
75
+ double width = videoCapture->get (cv::CAP_PROP_FRAME_WIDTH);
76
+ double height = videoCapture->get (cv::CAP_PROP_FRAME_HEIGHT);
77
+
78
+ if (width != FRAME_WIDTH)
79
+ {
80
+ OutputDebugString (L" ERROR: captured width does not equal FRAME_WIDTH. Expecting: " );
81
+ OutputDebugString (std::to_wstring (width).c_str ());
82
+ OutputDebugString (L" \n " );
83
+ }
84
+
85
+ if (height != FRAME_HEIGHT)
86
+ {
87
+ OutputDebugString (L" ERROR: captured height does not equal FRAME_HEIGHT. Expecting: " );
88
+ OutputDebugString (std::to_wstring (height).c_str ());
89
+ OutputDebugString (L" \n " );
90
+ }
91
+
92
+ // OpenCV returns frames in RGB, convert to BGRA
93
+ DirectXHelper::ConvertRGBtoBGRA (frame.data , tmpData, width, height, true );
94
+ EnterCriticalSection (&lock);
95
+ dirtyFrame = false ;
96
+
97
+ memcpy (cachedFrame, tmpData, FRAME_BUFSIZE);
98
+
99
+ LeaveCriticalSection (&lock);
100
+ });
101
+
102
+ if (_device != nullptr && _colorSRV != nullptr )
103
+ {
104
+ EnterCriticalSection (&lock);
105
+ if (!dirtyFrame)
106
+ {
107
+ DirectXHelper::UpdateSRV (_device, _colorSRV, cachedFrame, FRAME_WIDTH * FRAME_BPP);
108
+ dirtyFrame = true ;
109
+ }
110
+ LeaveCriticalSection (&lock);
111
+
112
+ EnterCriticalSection (&frameAccessCriticalSection);
113
+ isVideoFrameReady = true ;
114
+ LeaveCriticalSection (&frameAccessCriticalSection);
115
+ }
116
+ }
117
117
}
118
118
119
119
bool OpenCVFrameProvider::IsVideoFrameReady ()
120
120
{
121
- EnterCriticalSection (&frameAccessCriticalSection);
122
- bool ret = isVideoFrameReady;
123
- if (isVideoFrameReady)
124
- {
125
- isVideoFrameReady = false ;
126
- }
127
- LeaveCriticalSection (&frameAccessCriticalSection);
128
-
129
- return ret;
121
+ EnterCriticalSection (&frameAccessCriticalSection);
122
+ bool ret = isVideoFrameReady;
123
+ if (isVideoFrameReady)
124
+ {
125
+ isVideoFrameReady = false ;
126
+ }
127
+ LeaveCriticalSection (&frameAccessCriticalSection);
128
+
129
+ return ret;
130
130
}
131
131
132
132
LONGLONG OpenCVFrameProvider::GetTimestamp ()
133
133
{
134
- return cachedTimestamp;
134
+ return cachedTimestamp;
135
135
}
136
136
137
137
LONGLONG OpenCVFrameProvider::GetDurationHNS ()
138
138
{
139
- return (1 .0f / 60 .0f ) * QPC_MULTIPLIER;
139
+ return (1 .0f / 60 .0f ) * QPC_MULTIPLIER;
140
140
}
141
141
142
142
bool OpenCVFrameProvider::IsEnabled ()
143
143
{
144
- if (videoCapture != nullptr )
145
- {
146
- return videoCapture->isOpened ();
147
- }
144
+ if (videoCapture != nullptr )
145
+ {
146
+ return videoCapture->isOpened ();
147
+ }
148
148
149
- return false ;
149
+ return false ;
150
150
}
151
151
152
152
void OpenCVFrameProvider::Dispose ()
153
153
{
154
- if (videoCapture != nullptr )
155
- {
156
- videoCapture->release ();
157
- videoCapture = nullptr ;
158
- }
154
+ if (videoCapture != nullptr )
155
+ {
156
+ videoCapture->release ();
157
+ videoCapture = nullptr ;
158
+ }
159
159
}
160
160
161
161
#endif
0 commit comments