-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathprocess_video_webcam.cpp
70 lines (61 loc) · 1.91 KB
/
process_video_webcam.cpp
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
/* DarkHelp - C++ helper class for Darknet's C API.
* Copyright 2019-2024 Stephane Charette <[email protected]>
* MIT license applies. See "license.txt" for details.
*/
#include "DarkHelp.hpp"
int main(int argc, char * argv[])
{
int rc = 0;
try
{
if (argc != 4)
{
std::cout
<< "Usage:" << std::endl
<< argv[0] << " <filename.cfg> <filename.names> <filename.weights>" << std::endl;
throw std::invalid_argument("wrong number of arguments");
}
// Load the neural network. The order of the 3 files does not matter, DarkHelp should figure out which file is which.
DarkHelp::NN nn(argv[1], argv[2], argv[3]);
// Use OpenCV to open the webcam. Index zero is the first webcam. Attemp to set a few camera properties.
cv::VideoCapture cap(0);
if (not cap.isOpened())
{
throw std::runtime_error("failed to open the webcam");
}
cap.set(cv::CAP_PROP_FRAME_WIDTH, 640.0);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480.0);
cap.set(cv::CAP_PROP_FPS, 30.0);
while (cap.isOpened())
{
cv::Mat frame;
cap >> frame;
if (frame.empty())
{
break;
}
nn.predict(frame);
frame = nn.annotate();
/* This is an over-simplistic example, where the waitKey() timeout is hard-coded. You want the timeout to be short
* enough for the OpenCV HighGUI event loop to have time to process events, but not too long that the code doesn't
* have time to keep up with new incoming video frames.
*
* An assumption is made that most webcams might default to 30 FPS, meaning 33.33333 milliseconds per frame. Sleep
* just half of that time to process HighGUI events, and instead spend the extra remaining time waiting for the next
* frame to be extracted.
*/
cv::imshow("video", frame);
const auto key = cv::waitKey(15);
if (key == 27)
{
break;
}
}
}
catch (const std::exception & e)
{
std::cout << e.what() << std::endl;
rc = 1;
}
return rc;
}