forked from stephanecharette/DarkHelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_c_api.cpp
78 lines (63 loc) · 2.21 KB
/
using_c_api.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
71
72
73
74
75
76
77
78
/* DarkHelp - C++ helper class for Darknet's C API.
* Copyright 2019-2024 Stephane Charette <[email protected]>
* MIT license applies. See "license.txt" for details.
*/
// This source file is written in C++ so we can use OpenCV easily,
// but the calls demonstrated are from the "C" DarkHelp API.
#include "DarkHelp_C_API.h"
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char * argv[])
{
int rc = 0;
try
{
if (argc != 5)
{
std::cout
<< "Usage:" << std::endl
<< argv[0] << " <filename.cfg> <filename.names> <filename.weights> <filename.jpg>" << std::endl;
throw std::invalid_argument("wrong number of arguments");
}
const std::string fn1 = argv[1];
const std::string fn2 = argv[2];
const std::string fn3 = argv[3];
const std::string image = argv[4];
std::cout
<< "Darknet v" << DarknetVersion() << std::endl
<< "DarkHelp v" << DarkHelpVersion() << std::endl;
DarkHelpPtr ptr = CreateDarkHelpNN(fn1.c_str(), fn2.c_str(), fn3.c_str());
/* All of the DarkHelp settings have default values described in the documentation. Calling these here
* just for demonstration. You don't have to call all of these if the default value works for you.
*/
SetThreshold(ptr, 0.25);
EnableNamesIncludePercentage(ptr, true);
EnableAnnotationAutoHideLabels(ptr, false);
SetAnnotationShadePredictions(ptr, 0.15f);
SetAnnotationFontScale(ptr, 0.5);
SetAnnotationFontThickness(ptr, 1);
SetAnnotationLineThickness(ptr, 1);
EnableAnnotationIncludeDuration(ptr, false);
EnableAnnotationIncludeTimestamp(ptr, false);
EnableTiles(ptr, false);
EnableSnapping(ptr, true);
PredictFN(ptr, image.c_str());
const char * json = GetPredictionResults(ptr);
std::cout << "results=" << json << std::endl;
Annotate(ptr, "testing.jpg");
/* Once the DarkHelp object has been destroyed, make sure you don't make any further
* DarkHelp "C" calls other than CreateDarkHelpNN() to create a new instance.
*/
DestroyDarkHelpNN(ptr);
// see what the output image looks like
cv::Mat mat = cv::imread("testing.jpg");
cv::imshow("annotated", mat);
cv::waitKey();
}
catch (const std::exception & e)
{
std::cout << e.what() << std::endl;
rc = 1;
}
return rc;
}