-
Notifications
You must be signed in to change notification settings - Fork 1
/
livedemo.cpp
89 lines (71 loc) · 2.45 KB
/
livedemo.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
80
81
82
83
84
85
86
87
88
89
#include "opencv2/opencv.hpp"
#include "opencv2/videoio.hpp"
#include "Halide.h"
#include "halide_pipeline_aot.h"
//#define USE_PAPI
#ifdef USE_PAPI
#include <papi.h>
#else
#include <ctime>
#endif
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
long long int counter = 0;
namedWindow("Edge Detection - Halide Image Processing DSL");
std::string text2;
#ifdef USE_PAPI
float rtime, ptime;
long long flpops;
float mflops;
PAPI_flops(&rtime, &ptime, &flpops, &mflops);
PAPI_stop_counters();
#endif
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
buffer_t input_buf = {0}, output_buf = {0};
input_buf.host = edges.data;
output_buf.host = edges.data;
input_buf.stride[0] = output_buf.stride[0] = 1;
input_buf.stride[1] = output_buf.stride[1] = frame.cols;
input_buf.extent[0] = output_buf.extent[0] = frame.cols;
input_buf.extent[1] = output_buf.extent[1] = frame.rows;
input_buf.min[0] = output_buf.min[0] = 0;
input_buf.min[1] = output_buf.min[1] = 0;
input_buf.elem_size = output_buf.elem_size = 1;
#ifdef USE_PAPI
PAPI_start_counters();
halide_pipeline_aot(&input_buf, frame.rows, frame.cols, &output_buf);
PAPI_stop_counters();
PAPI_flops(&rtime, &ptime, &flpops, &mflops);
#else
clock_t begin = clock();
halide_pipeline_aot(&input_buf, frame.rows, frame.cols, &output_buf);
clock_t end = clock();
#endif
const long long num_floating_operations = 405*frame.rows*frame.cols;
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
double flop_per_sec = num_floating_operations/elapsed_secs;
std::string text1 = "Halide Pipeline (Edge Detection)";
#ifdef USE_PAPI
if (counter==0 || counter%10==0) // Change the Flops each 100 iterations
text2 = "GFlops : " + std::to_string(mflops/1000);
#else
if (counter==0 || counter%10==0) // Change the Flops each 100 iterations
text2 = "GFlops : " + std::to_string(flop_per_sec/1000000000);
#endif
counter++;
cv::putText(edges, text1, cv::Point(30, 40), CV_FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255), 1, 8, false);
cv::putText(edges, text2, cv::Point(30, 70), CV_FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255), 1, 8, false);
imshow("edges", edges);
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}