-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
79 lines (64 loc) · 2.07 KB
/
main.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
79
#include <iostream>
#include <memory>
#include <opencv2/opencv.hpp>
#include "faceDetect.h"
#define DISP_WINNAME "camera"
#define QUIT_KEY 'q'
#define CAMID 0
using namespace std;
int main(int argc, const char* argv[]) {
double ftick, etick;
double ticksPerUs = cv::getTickFrequency() / 1000000;
//string file = "D:/software/cmake_libtorch_tools/example_release/data.txt";
string modelpath = "D:/software/cmake_libtorch_tools/example_release/facebox_ir.pt";
//¼ÓÔØÄ£ÐÍ
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load(modelpath);
module->to(at::kCUDA);
assert(module != nullptr);
vector <defaultbox> boxes;
Facedetect F;
//¶ÁÈ¡Êý×é
//F.read_txt(boxes_v1, file);
F.computDefaultbox(boxes);
assert(boxes.size() != 0);
cout << "boxes_v1:"<<boxes.size()<<endl;
cv::VideoCapture camera(CAMID);
if (!camera.isOpened()) {
std::cerr << "failed to camera" << std::endl;
return 1;
}
cv::namedWindow(DISP_WINNAME, cv::WINDOW_AUTOSIZE);
cv::Mat frame;
do {
camera >> frame;
if (!frame.data)
{
std::cerr << "Capture video failed" << std::endl;
break;
}
vector<float> finalBox;
cv::Mat frame_gray;
cv::cvtColor(frame.clone(),frame_gray,cv::COLOR_BGR2GRAY);
ftick = cv::getCPUTickCount();
int note;
note = F.detect(frame_gray, finalBox, module, boxes);
cout << "frame.cols" << frame.cols << endl;
cout << "frame.rows"<<frame.rows<<endl;
if (note == -1) {
std::cout << "Error: Face detect failed" << std::endl;
}
etick = cv::getCPUTickCount();
for (int i = 0; i < finalBox.size(); i++) {
int x1 = int(finalBox[0]);
int y1 = int(finalBox[1]);
int x2 = int(finalBox[2]);
int y2 = int(finalBox[3]);
cv::rectangle(frame, cv::Rect(x1, y1, x2 - x1, y2 - y1), cv::Scalar(0, 255, 255), 3);
}
std::cout << "total detected: " << finalBox.size() << "faces. used" << (etick - ftick) / ticksPerUs << "us" << std::endl;
cv::imshow(DISP_WINNAME, frame);
finalBox.clear();
} while (QUIT_KEY != cv::waitKey(1));
boxes.clear();
return 0;
}