Skip to content

Commit

Permalink
make OPENCV compile again
Browse files Browse the repository at this point in the history
  • Loading branch information
uecker committed Jun 28, 2024
1 parent bccff2d commit c45c47e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 44 deletions.
14 changes: 7 additions & 7 deletions include/yolo_v2_class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ class Tracker_optflow {

if (err.rows == cur_bbox_vec.size() && status.rows == cur_bbox_vec.size())
{
for (size_t i = 0; i < cur_bbox_vec.size(); ++i)
for (int i = 0; i < cur_bbox_vec.size(); ++i)
{
cv::Point2f cur_key_pt = cur_pts_flow.at<cv::Point2f>(0, i);
cv::Point2f prev_key_pt = prev_pts_flow.at<cv::Point2f>(0, i);
Expand Down Expand Up @@ -573,7 +573,7 @@ class preview_boxes_t {
preview_box_track_t() : track_id(0), obj_id(0), last_showed_frames_ago(frames_history), current_detection(false) {}
};
std::vector<preview_box_track_t> preview_box_track_id;
size_t const preview_box_size, bottom_offset;
int const preview_box_size, bottom_offset;
bool const one_off_detections;
public:
preview_boxes_t(size_t _preview_box_size = 100, size_t _bottom_offset = 100, bool _one_off_detections = false) :
Expand Down Expand Up @@ -876,8 +876,8 @@ class track_kalman_t
float time_wait = 0.5; // 0.5 second
if (track_id_state_id_time[state_id].track_id > -1)
{
if ((result_vec_pred[state_id].x > img_size.width) ||
(result_vec_pred[state_id].y > img_size.height))
if (((int)result_vec_pred[state_id].x > img_size.width) ||
((int)result_vec_pred[state_id].y > img_size.height))
{
track_id_state_id_time[state_id].track_id = -1;
}
Expand All @@ -897,7 +897,7 @@ class track_kalman_t

float min_dist = std::numeric_limits<float>::max();

for (size_t i = 0; i < max_objects; ++i)
for (int i = 0; i < max_objects; ++i)
{
if (track_id_state_id_time[i].track_id > -1 && result_vec_pred[i].obj_id == find_box.obj_id && busy_vec[i] == false)
{
Expand Down Expand Up @@ -987,7 +987,7 @@ class track_kalman_t
clear_old_states();
std::vector<bbox_t> result_vec;

for (size_t i = 0; i < max_objects; ++i)
for (int i = 0; i < max_objects; ++i)
{
tst_t tst = track_id_state_id_time[i];
if (tst.track_id > -1) {
Expand Down Expand Up @@ -1022,7 +1022,7 @@ class track_kalman_t
calc_dt();
clear_old_states();

for (size_t i = 0; i < max_objects; ++i)
for (int i = 0; i < max_objects; ++i)
track_id_state_id_time[i].detection_count--;

std::vector<tst_t> tst_vec = find_state_ids(result_vec);
Expand Down
15 changes: 10 additions & 5 deletions src/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
int i;

float avg_loss = -1;
//float avg_contrastive_acc = 0;
#ifdef OPENCV
float avg_contrastive_acc = 0;
#endif
char *base = basecfg(cfgfile);
printf("%s\n", base);
printf("%d\n", ngpus);
Expand Down Expand Up @@ -186,8 +188,9 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
else fprintf(stderr, " Tensor Cores are used.\n");
}
}

//int draw_precision = 0;
#ifdef OPENCV
int draw_precision = 0;
#endif
if (calc_topk && (i >= calc_topk_for_each || i == net.max_batches)) {
iter_topk = i;
if (net.contrastive && l.type != SOFTMAX && l.type != COST) {
Expand All @@ -200,7 +203,9 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
topk = validate_classifier_single(datacfg, cfgfile, weightfile, &net, topk_data); // calc TOP-n
printf("\n accuracy %s = %f \n", topk_buff, topk);
}
// draw_precision = 1;
#ifdef OPENCV
draw_precision = 1;
#endif
}

time_remaining = ((net.max_batches - i) / ngpus) * (what_time_is_it_now() - start) / 60 / 60;
Expand Down Expand Up @@ -1280,7 +1285,7 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
int frame_counter = 0;

while(1){
struct timeval tval_before, tval_after, tval_result;
struct timeval tval_before /*, tval_after, tval_result */;
gettimeofday(&tval_before, NULL);

//image in = get_image_from_stream(cap);
Expand Down
12 changes: 6 additions & 6 deletions src/connected_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,6 @@ void forward_connected_layer_gpu(connected_layer l, network_state state)
{
fill_ongpu(l.outputs*l.batch, 0, l.output_gpu, 1);

// int m = l.batch;
// int k = l.inputs;
// int n = l.outputs;
// float * a = state.input;
// float * b = l.weights_gpu;
// float * c = l.output_gpu;
#ifdef CUDNN
//float one = 1; // alpha[0], beta[0]
float alpha = 1, beta = 0;
Expand All @@ -360,6 +354,12 @@ void forward_connected_layer_gpu(connected_layer l, network_state state)
l.dstTensorDesc,
l.output_gpu));
#else // CUDNN
int m = l.batch;
int k = l.inputs;
int n = l.outputs;
float * a = state.input;
float * b = l.weights_gpu;
float * c = l.output_gpu;
gemm_ongpu(0,1,m,n,k,1,a,k,b,k,1,c,n);
#endif // CUDNN

Expand Down
6 changes: 3 additions & 3 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
d.X.vals = (float**)xcalloc(d.X.rows, sizeof(float*));
d.X.cols = h*w*c;

float r1 = 0, r2 = 0, r3 = 0, r4 = 0, r_scale = 0;
float r1 = 0, r2 = 0, r3 = 0, r4 = 0; //, r_scale = 0;
float resize_r1 = 0, resize_r2 = 0;
float dhue = 0, dsat = 0, dexp = 0, flip = 0, blur = 0;
int augmentation_calculated = 0, gaussian_noise = 0;
Expand Down Expand Up @@ -1134,7 +1134,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
flip = use_flip ? random_gen() % 2 : 0;
}

r_scale = random_float();
//r_scale = random_float();

if (!contrastive || contrastive_color || i % 2 == 0)
{
Expand Down Expand Up @@ -1288,7 +1288,7 @@ data load_data_detection(int n, char **paths, int m, int w, int h, int c, int bo
const int bot_shift = min_val_cmp(h - cut_y[i], max_val_cmp(0, (-pbot*h / oh)));


int k, x, y;
int k, y;
for (k = 0; k < c; ++k) {
for (y = 0; y < h; ++y) {
int j = y*w + k*w*h;
Expand Down
2 changes: 1 addition & 1 deletion src/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void *detect_in_thread(void *ptr)
this_thread_yield();
}

layer l = net.layers[net.n - 1];
// layer l = net.layers[net.n - 1];
float *X = det_s.data;
//float *prediction =
network_predict(net, X);
Expand Down
19 changes: 12 additions & 7 deletions src/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
char *base = basecfg(cfgfile);
printf("%s\n", base);
float avg_loss = -1;
// float avg_contrastive_acc = 0;
#ifdef OPENCV
float avg_contrastive_acc = 0;
#endif
network* nets = (network*)xcalloc(ngpus, sizeof(network));

srand(time(0));
Expand Down Expand Up @@ -324,7 +326,9 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
}
printf("\n %d: %f, %f avg loss, %f rate, %lf seconds, %d images, %f hours left\n", iteration, loss, avg_loss, get_current_rate(net), (what_time_is_it_now() - time), iteration*imgs, avg_time);
fflush(stdout);

#ifdef OPENCV
int draw_precision = 0;
#endif
if (calc_map && (iteration >= next_map_calc || iteration == net.max_batches)) {
if (l.random) {
printf("Resizing to initial size: %d x %d ", init_w, init_h);
Expand Down Expand Up @@ -370,8 +374,9 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
sprintf(buff, "%s/%s_best.weights", backup_directory, base);
save_weights(net, buff);
}

// draw_precision = 1;
#ifdef OPENCV
draw_precision = 1;
#endif
}
time_remaining = ((net.max_batches - iteration) / ngpus)*(what_time_is_it_now() - time + load_time) / 60 / 60;
// set initial value, even if resume training from 10000 iteration
Expand Down Expand Up @@ -1799,14 +1804,14 @@ void draw_object(char *datacfg, char *cfgfile, char *weightfile, char *filename,
}

srand(2222222);
char buff[256];
char buff[256] = { 0 };
char *input = buff;

int j;
float nms = .45; // 0.4F
while (1) {
if (filename) {
strncpy(input, filename, 256);
strncpy(input, filename, sizeof buff - 1);
if (strlen(input) > 0)
if (input[strlen(input) - 1] == 0x0d) input[strlen(input) - 1] = 0;
}
Expand Down Expand Up @@ -1868,7 +1873,7 @@ void draw_object(char *datacfg, char *cfgfile, char *weightfile, char *filename,
float avg_loss = get_network_cost(net);
draw_train_loss(windows_name, img, img_size, avg_loss, max_img_loss, iteration, it_num, 0, 0, "mAP%", 0, dont_show, 0, 0);

float inv_loss = 1.0 / max_val_cmp(0.01, avg_loss);
//float inv_loss = 1.0 / max_val_cmp(0.01, avg_loss);
//net.learning_rate = *lr_set * inv_loss;

if (*boxonly) {
Expand Down
2 changes: 1 addition & 1 deletion src/http_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class MJPG_sender
#endif
if (s == sock) // request on master socket, accept and send main header.
{
SOCKADDR_IN address = { 0 };
SOCKADDR_IN address = { };
SOCKET client = ::accept(sock, (SOCKADDR*)&address, &addrlen);
if (client == SOCKET_ERROR)
{
Expand Down
12 changes: 7 additions & 5 deletions src/image_opencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,13 +942,15 @@ extern "C" void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, flo
float red = get_color(2, offset, classes);
float green = get_color(1, offset, classes);
float blue = get_color(0, offset, classes);
#if 0
float rgb[3];

//width = prob*20+2;

rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
#endif
box b = dets[i].bbox;
if (std::isnan(b.w) || std::isinf(b.w)) b.w = 0.5;
if (std::isnan(b.h) || std::isinf(b.h)) b.h = 0.5;
Expand Down Expand Up @@ -1416,18 +1418,18 @@ extern "C" void cv_draw_object(image sized, float *truth_cpu, int max_boxes, int

int it_trackbar_value = 200;
std::string const it_trackbar_name = "iterations";
int it_tb_res = cv::createTrackbar(it_trackbar_name, window_name, &it_trackbar_value, 1000);
(void)cv::createTrackbar(it_trackbar_name, window_name, &it_trackbar_value, 1000);

int lr_trackbar_value = 10;
std::string const lr_trackbar_name = "learning_rate exp";
int lr_tb_res = cv::createTrackbar(lr_trackbar_name, window_name, &lr_trackbar_value, 20);
(void)cv::createTrackbar(lr_trackbar_name, window_name, &lr_trackbar_value, 20);

int cl_trackbar_value = 0;
std::string const cl_trackbar_name = "class_id";
int cl_tb_res = cv::createTrackbar(cl_trackbar_name, window_name, &cl_trackbar_value, classes-1);
(void)cv::createTrackbar(cl_trackbar_name, window_name, &cl_trackbar_value, classes-1);

std::string const bo_trackbar_name = "box-only";
int bo_tb_res = cv::createTrackbar(bo_trackbar_name, window_name, boxonly, 1);
(void)cv::createTrackbar(bo_trackbar_name, window_name, boxonly, 1);

int i = 0;

Expand All @@ -1440,7 +1442,7 @@ extern "C" void cv_draw_object(image sized, float *truth_cpu, int max_boxes, int
if (pressed_key == 27 || pressed_key == 1048603) break;// break; // ESC - save & exit

frame_clone = frame.clone();
char buff[100];
//char buff[100];
std::string lr_value = "learning_rate = " + std::to_string(1.0 / pow(2, lr_trackbar_value));
cv::putText(frame_clone, lr_value, cv::Point2i(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(10, 50, 10), 3);
cv::putText(frame_clone, lr_value, cv::Point2i(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(20, 120, 60), 2);
Expand Down
6 changes: 3 additions & 3 deletions src/yolo_console_dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ std::vector<bbox_t> get_3d_coordinates(std::vector<bbox_t> bbox_vect, cv::Mat xy
void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
int current_det_fps = -1, int current_cap_fps = -1)
{
int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } };
// int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } };

for (auto &i : result_vec) {
cv::Scalar color = obj_id_to_color(i.obj_id);
Expand All @@ -189,7 +189,7 @@ void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std
std::string obj_name = obj_names[i.obj_id];
if (i.track_id > 0) obj_name += " - " + std::to_string(i.track_id);
cv::Size const text_size = getTextSize(obj_name, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0);
int max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
int max_width = (text_size.width > (int)i.w + 2) ? text_size.width : (i.w + 2);
max_width = std::max(max_width, (int)i.w + 2);
//max_width = std::max(max_width, 283);
std::string coords_3d;
Expand All @@ -198,7 +198,7 @@ void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std
ss << std::fixed << std::setprecision(2) << "x:" << i.x_3d << "m y:" << i.y_3d << "m z:" << i.z_3d << "m ";
coords_3d = ss.str();
cv::Size const text_size_3d = getTextSize(ss.str(), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, 1, 0);
int const max_width_3d = (text_size_3d.width > i.w + 2) ? text_size_3d.width : (i.w + 2);
int const max_width_3d = (text_size_3d.width > (int)i.w + 2) ? text_size_3d.width : (i.w + 2);
if (max_width_3d > max_width) max_width = max_width_3d;
}

Expand Down
12 changes: 6 additions & 6 deletions src/yolo_v2_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,24 +433,24 @@ LIB_API std::vector<bbox_t> Detector::tracking_id(std::vector<bbox_t> cur_bbox_v
if (i.size() > 0) prev_track_id_present = true;

if (!prev_track_id_present) {
for (size_t i = 0; i < cur_bbox_vec.size(); ++i)
for (int i = 0; i < (int)cur_bbox_vec.size(); ++i)
cur_bbox_vec[i].track_id = det_gpu.track_id[cur_bbox_vec[i].obj_id]++;
prev_bbox_vec_deque.push_front(cur_bbox_vec);
if (prev_bbox_vec_deque.size() > frames_story) prev_bbox_vec_deque.pop_back();
if ((int)prev_bbox_vec_deque.size() > frames_story) prev_bbox_vec_deque.pop_back();
return cur_bbox_vec;
}

std::vector<unsigned int> dist_vec(cur_bbox_vec.size(), std::numeric_limits<unsigned int>::max());
std::vector<int> dist_vec(cur_bbox_vec.size(), std::numeric_limits<int>::max());

for (auto &prev_bbox_vec : prev_bbox_vec_deque) {
for (auto &i : prev_bbox_vec) {
int cur_index = -1;
for (size_t m = 0; m < cur_bbox_vec.size(); ++m) {
for (int m = 0; m < (int)cur_bbox_vec.size(); ++m) {
bbox_t const& k = cur_bbox_vec[m];
if (i.obj_id == k.obj_id) {
float center_x_diff = (float)(i.x + i.w/2) - (float)(k.x + k.w/2);
float center_y_diff = (float)(i.y + i.h/2) - (float)(k.y + k.h/2);
unsigned int cur_dist = sqrt(center_x_diff*center_x_diff + center_y_diff*center_y_diff);
int cur_dist = sqrt(center_x_diff*center_x_diff + center_y_diff*center_y_diff);
if (cur_dist < max_dist && (k.track_id == 0 || dist_vec[m] > cur_dist)) {
dist_vec[m] = cur_dist;
cur_index = m;
Expand All @@ -475,7 +475,7 @@ LIB_API std::vector<bbox_t> Detector::tracking_id(std::vector<bbox_t> cur_bbox_v

if (change_history) {
prev_bbox_vec_deque.push_front(cur_bbox_vec);
if (prev_bbox_vec_deque.size() > frames_story) prev_bbox_vec_deque.pop_back();
if ((int)prev_bbox_vec_deque.size() > frames_story) prev_bbox_vec_deque.pop_back();
}

return cur_bbox_vec;
Expand Down

0 comments on commit c45c47e

Please sign in to comment.