-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
63 lines (49 loc) · 1.81 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
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/dnn.hpp>
#include <string>
#include <iostream>
#include <time.h>
using namespace std;
using namespace cv;
using namespace dnn;
int main()
{
//Net cvNet = readNetFromTensorflow("frozen_inference_graph.pb", "ssd_mobilenet_v1_coco.pbtxt");
Net net = readNetFromCaffe("halmet.prototxt", "halmet.caffemodel");
const char* classNames[] = { "background", "w","b","r","y", "head" };
float detect_thresh = 0.24;
VideoCapture cap(-1);
cv::Mat image;
while(1)
{
cap >>image;
clock_t start_t = clock();
net.setInput(blobFromImage(image, 1.0 / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), true, false));
Mat cvOut = net.forward();
cout << "Cost time: " << clock() - start_t << endl;
Mat detectionMat(cvOut.size[2], cvOut.size[3], CV_32F, cvOut.ptr<float>());
for (int i = 0; i < detectionMat.rows; i++)
{
int obj_class = detectionMat.at<float>(i, 1);
float confidence = detectionMat.at<float>(i, 2);
if (confidence > detect_thresh)
{
size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols);
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows);
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols);
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows);
Rect object((int)xLeftBottom, (int)yLeftBottom,
(int)(xRightTop - xLeftBottom),
(int)(yRightTop - yLeftBottom));
rectangle(image, object, Scalar(0, 0, 255), 2);
putText(image, classNames[obj_class], Point(xLeftBottom, yLeftBottom - 10), 3, 0.5, Scalar(0, 0, 255), 2);
}
}
imshow("test", image);
cv::waitKey(1);
}
return 0;
}