forked from pranaypratyush/vw_detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvw_detect.cpp
143 lines (124 loc) · 3.19 KB
/
vw_detect.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: vw_test.cpp`
* Author: pranay
*
* Created on 8 May, 2016, 3:36 PM
*/
#include "vw_detect.h"
#include <fstream>
#include <fcntl.h>
#include <iostream>
#include <stdint.h>
#include "ctpl_stl.h"
#include <unistd.h>
using namespace std;
using namespace cv;
void vw_detect::vw_detect_init(char *path_to_hash)
{
/*
ifstream in_file(path_to_hash);
if (in_file.is_open())
{
}
*/
char buff[256];
int file_handle = open(path_to_hash, O_RDONLY);
if (file_handle == -1)
{
cerr << "Cannot open file";
return;
}
for (int h = 0; h < 256; h++)
{
for (int s = 0; s < 256; s++)
{
if (read(file_handle, buff, sizeof (buff)) != 256)
{
cerr << "error reading file at " << h << " " << s << "\n";
return;
}
for (int v = 0; v < 256; v++)
{
hash_table[h][s][v] = (uint_least8_t) (buff[v] - '0');
}
}
}
ctpl::thread_pool pool(n_threads);
}
void vw_detect::getPredictions(Mat original, Mat prediction)
{
Mat hsv_image;
cvtColor(original, hsv_image, CV_BGR2HSV);
// prediction = Mat(original.rows, original.cols, CV_8UC3, Scalar(0, 0, 0));
for (int i = 0; i <= 255; i += original.rows / n_threads)
{
//pool.push(vw_detect::predict_block, hsv_image, prediction, i, i + original.rows / n_threads - 1,this->hash_table);
pool.push([&, this](int id)
{
this->predict_block(hsv_image, prediction, i, i + original.rows / n_threads - 1);
});
}
}
bool vw_detect::is_idle()
{
return (pool.n_idle() == pool.size());
}
void vw_detect::predict_block(Mat hsv_image, Mat prediction, int start, int end)
{
for (int i = start; i <= end; i++)
{
for (int j = 0; j < hsv_image.cols; j++)
{
Vec3b &hsv = hsv_image.at<Vec3b>(i, j);
Vec3b &Color = prediction.at<Vec3b>(i, j);
switch (hash_table[hsv[0]][hsv[1]][hsv[2]])
{
case 1:
Color[0] = 0;
Color[1] = 0;
Color[2] = 0;
break;
case 2:
Color[0] = 0;
Color[1] = 255;
Color[2] = 255;
break;
case 3:
Color[0] = 0;
Color[1] = 255;
Color[2] = 0;
break;
case 4:
Color[0] = 0;
Color[1] = 0;
Color[2] = 255;
break;
default:
cerr << "Unknown prediction at (" << i << "," << j << ")." << endl;
return;
}
}
}
}
void vw_detect::clean_up()
{
pool.stop(0);
}
vw_detect::vw_detect(char *path_to_hash, int n = 4) : n_threads(n)
{
vw_detect_init(path_to_hash);
}
vw_detect::~vw_detect()
{
vw_detect::clean_up();
}
void vw_detect::wait_for_completion()
{
if (!is_idle())
usleep(5000);
}