Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.90 KB | None | 0 0
  1. #include <iostream>
  2. #include "stdafx.h"
  3.  
  4. //dlib - http://dlib.net/
  5. #include <dlib/matrix.h>
  6. #include <dlib/dnn.h>
  7. #include <dlib/opencv.h>
  8. #include <dlib/image_io.h>
  9. using namespace dlib;
  10.  
  11. //opencv - https://opencv.org/
  12. #include <opencv2/opencv.hpp>
  13. using namespace std;
  14. using namespace cv;
  15. using namespace cv::ml;
  16.  
  17. struct space
  18. {
  19. int x01, y01, x02, y02, x03, y03, x04, y04, occup;
  20. };
  21.  
  22. int load_parking_geometry(const char *filename, space *spaces);
  23. void extract_space(space *spaces, Mat in_mat, std::vector<Mat> &vector_images);
  24. void draw_detection(space *spaces, Mat &frame);
  25. void evaluation(fstream &detectorOutputFile, fstream &groundTruthFile);
  26.  
  27. void train_parking_dlib();
  28. void test_parking();
  29. void test_parking_dlib();
  30.  
  31. void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData);
  32.  
  33. int spaces_num = 56;
  34. cv::Size space_size(80, 80);
  35.  
  36. //LeNet
  37. using net_type = loss_binary_log<
  38. fc<1,
  39. relu<fc<84,
  40. relu<fc<120,
  41. max_pool<2, 2, 2, 2, relu<con<16, 5, 5, 1, 1,
  42. max_pool<2, 2, 2, 2, relu<con<6, 5, 5, 1, 1,
  43. input<matrix<bgr_pixel>>
  44. >>>>>>>>>>>>;
  45.  
  46.  
  47. //AlexNet
  48. /*using net_type = loss_binary_log<
  49. fc<1,
  50. relu<fc<4096,
  51. relu<fc<4096,
  52. max_pool<3, 3, 2, 2,
  53. relu<con<256, 3, 3, 1, 1,
  54. relu<con<384, 3, 3, 1, 1,
  55. relu<con<384, 3, 3, 1, 1,
  56. max_pool<3, 3, 2, 2,
  57. relu<con<256, 5, 5, 1, 1,
  58. max_pool<3, 3, 2, 2,
  59. relu<con<96, 11, 11, 4, 4,
  60. input<matrix<bgr_pixel>>
  61. >>>>>>>>>>>>>>>>>>>;*/
  62.  
  63. template <typename SUBNET> using i3a_1 = relu<con<64, 1, 1, 1, 1, SUBNET>>;
  64. template <typename SUBNET> using i3a_2 = relu<con<128, 3, 3, 1, 1, relu<con<96, 1, 1, 1, 1, SUBNET>>>>;
  65. template <typename SUBNET> using i3a_3 = relu<con<32, 5, 5, 1, 1, relu<con<16, 1, 1, 1, 1, SUBNET>>>>;
  66. template <typename SUBNET> using i3a_4 = relu<con<32, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  67. template <typename SUBNET> using incept_3a = inception4<i3a_1, i3a_2, i3a_3, i3a_4, SUBNET>;
  68.  
  69. template <typename SUBNET> using i3b_1 = relu<con<128, 1, 1, 1, 1, SUBNET>>;
  70. template <typename SUBNET> using i3b_2 = relu<con<192, 3, 3, 1, 1, relu<con<128, 1, 1, 1, 1, SUBNET>>>>;
  71. template <typename SUBNET> using i3b_3 = relu<con<96, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
  72. template <typename SUBNET> using i3b_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  73. template <typename SUBNET> using incept_3b = inception4<i3b_1, i3b_2, i3b_3, i3b_4, SUBNET>;
  74.  
  75. template <typename SUBNET> using i4a_1 = relu<con<192, 1, 1, 1, 1, SUBNET>>;
  76. template <typename SUBNET> using i4a_2 = relu<con<208, 3, 3, 1, 1, relu<con<96, 1, 1, 1, 1, SUBNET>>>>;
  77. template <typename SUBNET> using i4a_3 = relu<con<48, 5, 5, 1, 1, relu<con<16, 1, 1, 1, 1, SUBNET>>>>;
  78. template <typename SUBNET> using i4a_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  79. template <typename SUBNET> using incept_4a = inception4<i4a_1, i4a_2, i4a_3, i4a_4, SUBNET>;
  80.  
  81. template <typename SUBNET> using i4b_1 = relu<con<160, 1, 1, 1, 1, SUBNET>>;
  82. template <typename SUBNET> using i4b_2 = relu<con<224, 3, 3, 1, 1, relu<con<112, 1, 1, 1, 1, SUBNET>>>>;
  83. template <typename SUBNET> using i4b_3 = relu<con<64, 5, 5, 1, 1, relu<con<24, 1, 1, 1, 1, SUBNET>>>>;
  84. template <typename SUBNET> using i4b_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  85. template <typename SUBNET> using incept_4b = inception4<i4b_1, i4b_2, i4b_3, i4b_4, SUBNET>;
  86.  
  87. template <typename SUBNET> using i4c_1 = relu<con<128, 1, 1, 1, 1, SUBNET>>;
  88. template <typename SUBNET> using i4c_2 = relu<con<256, 3, 3, 1, 1, relu<con<128, 1, 1, 1, 1, SUBNET>>>>;
  89. template <typename SUBNET> using i4c_3 = relu<con<64, 5, 5, 1, 1, relu<con<24, 1, 1, 1, 1, SUBNET>>>>;
  90. template <typename SUBNET> using i4c_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  91. template <typename SUBNET> using incept_4c = inception4<i4c_1, i4c_2, i4c_3, i4c_4, SUBNET>;
  92.  
  93. template <typename SUBNET> using i4d_1 = relu<con<112, 1, 1, 1, 1, SUBNET>>;
  94. template <typename SUBNET> using i4d_2 = relu<con<288, 3, 3, 1, 1, relu<con<144, 1, 1, 1, 1, SUBNET>>>>;
  95. template <typename SUBNET> using i4d_3 = relu<con<64, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
  96. template <typename SUBNET> using i4d_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  97. template <typename SUBNET> using incept_4d = inception4<i4d_1, i4d_2, i4d_3, i4d_4, SUBNET>;
  98.  
  99. template <typename SUBNET> using i4e_1 = relu<con<256, 1, 1, 1, 1, SUBNET>>;
  100. template <typename SUBNET> using i4e_2 = relu<con<320, 3, 3, 1, 1, relu<con<160, 1, 1, 1, 1, SUBNET>>>>;
  101. template <typename SUBNET> using i4e_3 = relu<con<128, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
  102. template <typename SUBNET> using i4e_4 = relu<con<128, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  103. template <typename SUBNET> using incept_4e = inception4<i4e_1, i4e_2, i4e_3, i4e_4, SUBNET>;
  104.  
  105. template <typename SUBNET> using i5a_1 = relu<con<256, 1, 1, 1, 1, SUBNET>>;
  106. template <typename SUBNET> using i5a_2 = relu<con<320, 3, 3, 1, 1, relu<con<160, 1, 1, 1, 1, SUBNET>>>>;
  107. template <typename SUBNET> using i5a_3 = relu<con<128, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
  108. template <typename SUBNET> using i5a_4 = relu<con<128, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  109. template <typename SUBNET> using incept_5a = inception4<i5a_1, i5a_2, i5a_3, i5a_4, SUBNET>;
  110.  
  111. template <typename SUBNET> using i5b_1 = relu<con<384, 1, 1, 1, 1, SUBNET>>;
  112. template <typename SUBNET> using i5b_2 = relu<con<384, 3, 3, 1, 1, relu<con<192, 1, 1, 1, 1, SUBNET>>>>;
  113. template <typename SUBNET> using i5b_3 = relu<con<128, 5, 5, 1, 1, relu<con<48, 1, 1, 1, 1, SUBNET>>>>;
  114. template <typename SUBNET> using i5b_4 = relu<con<128, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
  115. template <typename SUBNET> using incept_5b = inception4<i5b_1, i5b_2, i5b_3, i5b_4, SUBNET>;
  116.  
  117. /*using net_type =
  118. loss_binary_log<
  119. fc<1,
  120. dropout<avg_pool<7, 7, 1, 1,
  121. incept_5b<incept_5b<
  122. max_pool<3, 3, 2, 2,
  123. incept_4e<incept_4d<incept_4c<incept_4b<incept_4a<
  124. max_pool<3, 3, 2, 2,
  125. incept_3b<incept_3a<
  126. max_pool<3, 3, 2, 2, relu<con<192, 3, 3, 1, 1,
  127. relu<con<64, 1, 1, 1, 1,
  128. max_pool<3, 3, 2, 2,
  129. relu<con<64, 7, 7, 2, 2,
  130. input<matrix<bgr_pixel>>
  131. >>>>>>>>>>>>>>>>>>>>>>>;*/
  132.  
  133. using net_type2 =
  134. loss_binary_log<
  135. fc<1,
  136. dlib::multiply<avg_pool<7, 7, 1, 1,
  137. incept_5b<incept_5b<
  138. max_pool<3, 3, 2, 2,
  139. incept_4e<incept_4d<incept_4c<incept_4b<incept_4a<
  140. max_pool<3, 3, 2, 2,
  141. incept_3b<incept_3a<
  142. max_pool<3, 3, 2, 2,
  143. relu<con<192, 3, 3, 1, 1,
  144. relu<con<64, 1, 1, 1, 1,
  145. max_pool<3, 3, 2, 2,
  146. relu<con<64, 7, 7, 2, 2,
  147. input<matrix<bgr_pixel>>
  148. >>>>>>>>>>>>>>>>>>>>>>>;
  149.  
  150.  
  151. int main(int argc, char** argv)
  152. {
  153. cout << "Test OpenCV Start" << endl;
  154. test_parking();
  155. cout << "Test OpenCV End" << endl << endl;
  156.  
  157. cout << "Train Dlib Start" << endl;
  158. train_parking_dlib();
  159. cout << "Train Dlib End" << endl << endl;
  160.  
  161. cout << "Test Dlib Start" << endl;
  162. test_parking_dlib();
  163. cout << "Test Dlib End" << endl;
  164. }
  165.  
  166. void train_parking_dlib()
  167. {
  168. std::vector<matrix<bgr_pixel>> training_images;
  169. matrix<bgr_pixel> image;
  170. std::vector<float> training_labels;
  171.  
  172. fstream train_file("train_images_dlib.txt");
  173. string train_path;
  174.  
  175. while (train_file >> train_path)
  176. {
  177. load_png(image, train_path);
  178.  
  179. matrix<bgr_pixel> impom(80, 80);
  180. resize_image(image, impom);
  181. training_images.push_back(move(impom));
  182. float label = -1.0f;
  183. if (train_path.find("full") != std::string::npos) label = 1.0f;
  184. training_labels.push_back(label);
  185. }
  186.  
  187. cout << "Train images: " << training_images.size() << endl;
  188. cout << "Train labels: " << training_labels.size() << endl;
  189.  
  190. double lr = 0.0001;
  191. int batch_size = 16;
  192.  
  193. net_type net;
  194. dnn_trainer<net_type> trainer(net);
  195. trainer.set_learning_rate(lr);
  196. trainer.set_learning_rate_shrink_factor(0.1);
  197. trainer.set_iterations_without_progress_threshold(1000000);
  198. trainer.set_test_iterations_without_progress_threshold(1000000);
  199. trainer.set_mini_batch_size(batch_size);
  200.  
  201. /*trainer.set_max_num_epochs(40);
  202. trainer.be_verbose();
  203.  
  204. trainer.train(training_images, training_labels);
  205. */
  206.  
  207. trainer.be_quiet();
  208.  
  209. std::vector<matrix<bgr_pixel>> validation_images;
  210. std::vector<float> validation_labels;
  211. double val = 0.1;
  212. int val_segment;
  213. int es_max_epochs = 5;
  214. double es_min_test_loss = 999;
  215. bool train_flag = true;
  216.  
  217. std::vector<matrix<bgr_pixel>> validation_mini_batch_samples;
  218. std::vector<float> validation_mini_batch_labels;
  219.  
  220. std::vector<matrix<bgr_pixel>> mini_batch_samples;
  221. std::vector<float> mini_batch_labels;
  222.  
  223. val_segment = training_images.size() * val;
  224. for (int j = 0; j < val_segment; j++)
  225. {
  226. validation_images.push_back(training_images.back());
  227. training_images.pop_back();
  228.  
  229. validation_labels.push_back(training_labels.back());
  230. training_labels.pop_back();
  231. }
  232.  
  233. std::vector<matrix<bgr_pixel>> training_images_copy;
  234. std::vector<float> training_labels_copy;
  235.  
  236. std::vector<matrix<bgr_pixel>> validation_images_copy;
  237. std::vector<float> validation_labels_copy;
  238. dlib::rand rnd(time(0));
  239. int epoch_step_calls = 0;
  240. int epoch_counter = 1;
  241. int es_counter = 0;
  242.  
  243. while (train_flag)
  244. {
  245. training_images_copy = training_images;
  246. training_labels_copy = training_labels;
  247. int iterations = training_images_copy.size();
  248. for (int i = 0; i < iterations; i++) {
  249. int a = std::rand() % iterations;
  250. int b = std::rand() % iterations;
  251.  
  252. matrix<bgr_pixel> pom = training_images_copy[a];
  253. training_images_copy[a] = training_images_copy[b];
  254. training_images_copy[b] = pom;
  255.  
  256. float pom2 = training_labels_copy[a];
  257. training_labels_copy[a] = training_labels_copy[b];
  258. training_labels_copy[b] = pom2;
  259. }
  260.  
  261. for (int i = 1; i <= iterations; i++)
  262. {
  263. mini_batch_samples.push_back(training_images_copy.back());
  264. training_images_copy.pop_back();
  265.  
  266. mini_batch_labels.push_back(training_labels_copy.back());
  267. training_labels_copy.pop_back();
  268. if (mini_batch_samples.size() % batch_size == 0 || i == iterations)
  269. {
  270. trainer.train_one_step(mini_batch_samples, mini_batch_labels);
  271.  
  272. mini_batch_samples.clear();
  273. mini_batch_labels.clear();
  274. }
  275. }
  276.  
  277. validation_images_copy = validation_images;
  278. validation_labels_copy = validation_labels;
  279. iterations = validation_images_copy.size();
  280. for (int i = 0; i < iterations; i++) {
  281. int a = std::rand() % iterations;
  282. int b = std::rand() % iterations;
  283. matrix<bgr_pixel> pom = validation_images_copy[a];
  284. validation_images_copy[a] = validation_images_copy[b];
  285. validation_images_copy[b] = pom;
  286.  
  287. float pom2 = validation_labels_copy[a];
  288. validation_labels_copy[a] = validation_labels_copy[b];
  289. validation_labels_copy[b] = pom2;
  290. }
  291.  
  292. /*test_loss = trainer.get_net().compute_loss(validation_images.begin(), validation_images.end(), validation_labels.begin());
  293.  
  294. double train_loss = trainer.get_average_loss();
  295. trainer.clear_average_loss();
  296. if (test_loss < min_test_loss)
  297. {
  298. min_test_loss = test_loss;
  299. epochs_without_progress = 0;
  300. }
  301. else
  302. epochs_without_progress++;
  303.  
  304. if (epochs_without_progress == ES_max_epochs)
  305. {
  306. train_flag = false;
  307. cout << "Early stopping" << endl;
  308. }
  309.  
  310. cout << epoch_counter++ << ". Epoch - training set loss error(epoch average): " << train_loss << " - validation set loss error(end of epoch): " << test_loss << " - epochs without progress: "
  311. << epochs_without_progress << " - lr: " << trainer.get_learning_rate() << endl;
  312. */
  313.  
  314. for (int i = 1; i <= iterations; i++)
  315. {
  316. validation_mini_batch_samples.push_back(validation_images_copy.back());
  317. validation_images_copy.pop_back();
  318. validation_mini_batch_labels.push_back(validation_labels_copy.back());
  319. validation_labels_copy.pop_back();
  320. if (validation_mini_batch_samples.size() % batch_size == 0 || i == iterations)
  321. {
  322. trainer.test_one_step(validation_mini_batch_samples, validation_mini_batch_labels);
  323. epoch_step_calls++;
  324. validation_mini_batch_samples.clear();
  325. validation_mini_batch_labels.clear();
  326. }
  327. }
  328.  
  329. double train_loss = trainer.get_average_loss();
  330. double test_loss = trainer.get_average_test_loss();
  331. trainer.clear_average_loss();
  332.  
  333. int epochs_without_progress = round((float)trainer.get_test_steps_without_progress() / (float)epoch_step_calls) - 1;
  334. if (epochs_without_progress == -1)
  335. epochs_without_progress = 0;
  336.  
  337. if (epochs_without_progress >= 5)
  338. {
  339. train_flag = false;
  340. cout << "Dlib Early stopping" << endl;
  341. }
  342.  
  343. cout << epoch_counter++ << ". Epoch - training set loss error(epoch average): " << train_loss << " - validation set loss error(moving average): " << test_loss << " - epochs without progress: "
  344. << epochs_without_progress << " - test step calls: " << trainer.get_test_one_step_calls() << " - lr: " << trainer.get_learning_rate() << endl;
  345.  
  346. epoch_step_calls = 0;
  347. }
  348.  
  349. net.clean();
  350. serialize("GoogLeNet.dat") << net;
  351. }
  352.  
  353.  
  354.  
  355. void test_parking_dlib()
  356. {
  357. space *spaces = new space[spaces_num];
  358. load_parking_geometry("parking_map.txt", spaces);
  359.  
  360. fstream test_file("test_images.txt");
  361. ofstream out_label_file("out_prediction.txt");
  362. string test_path;
  363.  
  364. net_type2 net2;
  365. deserialize("GoogLeNet.dat") >> net2;
  366.  
  367. while (test_file >> test_path)
  368. {
  369. Mat frame = imread(test_path, 1);
  370. Mat draw_frame = frame.clone();
  371. //cvtColor(frame, frame, COLOR_BGR2GRAY);
  372.  
  373. std::vector<Mat> test_images;
  374. extract_space(spaces, frame, test_images);
  375.  
  376. int colNum = 0;
  377. for (int i = 0; i < test_images.size(); i++)
  378. {
  379. Mat pFrame = test_images[i];
  380. Mat test_image;
  381.  
  382. resize(pFrame, test_image, Size(80, 80));
  383. cv_image<bgr_pixel> cimg(test_image);
  384. matrix<bgr_pixel> dlibFrame = dlib::mat(cimg);
  385. float predict_label2 = net2(dlibFrame);
  386.  
  387. int predict_label;
  388.  
  389. if (predict_label2 > 0)
  390. predict_label = 1;
  391. else if (predict_label2 <= 0)
  392. predict_label = 0;
  393.  
  394. out_label_file << predict_label << endl;
  395. spaces[i].occup = predict_label;
  396. //imshow("original", pFrame);
  397. //imshow("test input", test_image);
  398. //waitKey(2);
  399. }
  400.  
  401. //draw detection
  402. draw_detection(spaces, draw_frame);
  403. namedWindow("draw_frame", 0);
  404. imshow("draw_frame", draw_frame);
  405. waitKey(2);
  406.  
  407. }
  408.  
  409. //evaluation
  410. fstream detector_file("out_prediction.txt");
  411. fstream groundtruth_file("groundtruth.txt");
  412. evaluation(detector_file, groundtruth_file);
  413. }
  414.  
  415. void test_parking()
  416. {
  417. space *spaces = new space[spaces_num];
  418. load_parking_geometry("parking_map.txt", spaces);
  419.  
  420. fstream test_file("test_images.txt");
  421. ofstream out_label_file("out_prediction.txt");
  422. string test_path;
  423.  
  424. while (test_file >> test_path)
  425. {
  426. //cout << "test_path: " << test_path << endl;
  427. Mat frame, gradX, gradY, blur;
  428.  
  429. frame = imread(test_path, 1);
  430. Mat draw_frame = frame.clone();
  431. cvtColor(frame, frame, COLOR_BGR2GRAY);
  432.  
  433. std::vector<Mat> test_images;
  434. extract_space(spaces, frame, test_images);
  435.  
  436. int colNum = 0;
  437.  
  438. int numEdgePixels;
  439. for (int i = 0; i < test_images.size(); i++)
  440. {
  441. frame = test_images[i];
  442.  
  443. Mat gradX, gradY, blur, abs_grad_x, abs_grad_y, test_image;
  444. medianBlur(frame, blur, 9);
  445. Sobel(frame, gradX, CV_8U, 1, 0, 3);
  446. Sobel(frame, gradY, CV_8U, 0, 1, 3);
  447. convertScaleAbs(gradX, abs_grad_x);
  448. convertScaleAbs(gradY, abs_grad_y);
  449. addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, test_image);
  450.  
  451. numEdgePixels = 0;
  452. for (int j = 0; j < test_image.rows; j++)
  453. for (int k = 0; k < test_image.cols; k++)
  454. {
  455. if (test_image.at<uchar>(j, k) > 100)
  456. numEdgePixels++;
  457. }
  458. //cout << numEdgePixels << endl;
  459.  
  460. //imshow("blur", blur);
  461. //imshow("grad", test_image);
  462.  
  463. int predict_label = 0;
  464. if (numEdgePixels > 140)
  465. predict_label = 1;
  466.  
  467. out_label_file << predict_label << endl;
  468. spaces[i].occup = predict_label;
  469. //imshow("test_img", test_images[i]);
  470. //waitKey(2);
  471. }
  472.  
  473. //draw detection
  474. draw_detection(spaces, draw_frame);
  475. namedWindow("draw_frame", 0);
  476. imshow("draw_frame", draw_frame);
  477. waitKey(2);
  478.  
  479. }
  480.  
  481. //evaluation
  482. fstream detector_file("out_prediction.txt");
  483. fstream groundtruth_file("groundtruth.txt");
  484. evaluation(detector_file, groundtruth_file);
  485. }
  486.  
  487. int load_parking_geometry(const char *filename, space *spaces)
  488. {
  489. FILE *file = fopen(filename, "r");
  490. if (file == NULL) return -1;
  491. int ps_count, i, count;
  492. count = fscanf(file, "%d\n", &ps_count); // read count of polygons
  493. for (i = 0; i < ps_count; i++) {
  494. int p = 0;
  495. int poly_size;
  496. count = fscanf(file, "%d->", &poly_size); // read count of polygon vertexes
  497. int *row = new int[poly_size * 2];
  498. int j;
  499. for (j = 0; j < poly_size; j++) {
  500. int x, y;
  501. count = fscanf(file, "%d,%d;", &x, &y); // read vertex coordinates
  502. row[p] = x;
  503. row[p + 1] = y;
  504. p = p + 2;
  505. }
  506. spaces[i].x01 = row[0];
  507. spaces[i].y01 = row[1];
  508. spaces[i].x02 = row[2];
  509. spaces[i].y02 = row[3];
  510. spaces[i].x03 = row[4];
  511. spaces[i].y03 = row[5];
  512. spaces[i].x04 = row[6];
  513. spaces[i].y04 = row[7];
  514. //printf("}\n");
  515. free(row);
  516. count = fscanf(file, "\n"); // read end of line
  517. }
  518. fclose(file);
  519. return 1;
  520. }
  521.  
  522. void extract_space(space *spaces, Mat in_mat, std::vector<Mat> &vector_images)
  523. {
  524. for (int x = 0; x < spaces_num; x++)
  525. {
  526. Mat src_mat(4, 2, CV_32F);
  527. Mat out_mat(space_size, CV_8U, 1);
  528. src_mat.at<float>(0, 0) = spaces[x].x01;
  529. src_mat.at<float>(0, 1) = spaces[x].y01;
  530. src_mat.at<float>(1, 0) = spaces[x].x02;
  531. src_mat.at<float>(1, 1) = spaces[x].y02;
  532. src_mat.at<float>(2, 0) = spaces[x].x03;
  533. src_mat.at<float>(2, 1) = spaces[x].y03;
  534. src_mat.at<float>(3, 0) = spaces[x].x04;
  535. src_mat.at<float>(3, 1) = spaces[x].y04;
  536.  
  537. Mat dest_mat(4, 2, CV_32F);
  538. dest_mat.at<float>(0, 0) = 0;
  539. dest_mat.at<float>(0, 1) = 0;
  540. dest_mat.at<float>(1, 0) = out_mat.cols;
  541. dest_mat.at<float>(1, 1) = 0;
  542. dest_mat.at<float>(2, 0) = out_mat.cols;
  543. dest_mat.at<float>(2, 1) = out_mat.rows;
  544. dest_mat.at<float>(3, 0) = 0;
  545. dest_mat.at<float>(3, 1) = out_mat.rows;
  546.  
  547. Mat H = findHomography(src_mat, dest_mat, 0);
  548. warpPerspective(in_mat, out_mat, H, space_size);
  549.  
  550. //imshow("out_mat", out_mat);
  551. //waitKey(0);
  552.  
  553. vector_images.push_back(out_mat);
  554.  
  555. }
  556.  
  557. }
  558.  
  559. void draw_detection(space *spaces, Mat &frame)
  560. {
  561. int sx, sy;
  562. for (int i = 0; i < spaces_num; i++)
  563. {
  564. Point pt1, pt2;
  565. pt1.x = spaces[i].x01;
  566. pt1.y = spaces[i].y01;
  567. pt2.x = spaces[i].x03;
  568. pt2.y = spaces[i].y03;
  569. sx = (pt1.x + pt2.x) / 2;
  570. sy = (pt1.y + pt2.y) / 2;
  571. if (spaces[i].occup)
  572. {
  573. circle(frame, Point(sx, sy - 25), 12, CV_RGB(255, 0, 0), -1);
  574. }
  575. else
  576. {
  577. circle(frame, Point(sx, sy - 25), 12, CV_RGB(0, 255, 0), -1);
  578. }
  579. }
  580. }
  581.  
  582. void evaluation(fstream &detectorOutputFile, fstream &groundTruthFile)
  583. {
  584. int detectorLine, groundTruthLine;
  585. int falsePositives = 0;
  586. int falseNegatives = 0;
  587. int truePositives = 0;
  588. int trueNegatives = 0;
  589. while (true)
  590. {
  591. if (!(detectorOutputFile >> detectorLine)) break;
  592. groundTruthFile >> groundTruthLine;
  593.  
  594. int detect = detectorLine;
  595. int ground = groundTruthLine;
  596.  
  597. //false positives
  598. if ((detect == 1) && (ground == 0))
  599. {
  600. falsePositives++;
  601. }
  602.  
  603. //false negatives
  604. if ((detect == 0) && (ground == 1))
  605. {
  606. falseNegatives++;
  607. }
  608.  
  609. //true positives
  610. if ((detect == 1) && (ground == 1))
  611. {
  612. truePositives++;
  613. }
  614.  
  615. //true negatives
  616. if ((detect == 0) && (ground == 0))
  617. {
  618. trueNegatives++;
  619. }
  620.  
  621. }
  622. cout << "falsePositives " << falsePositives << endl;
  623. cout << "falseNegatives " << falseNegatives << endl;
  624. cout << "truePositives " << truePositives << endl;
  625. cout << "trueNegatives " << trueNegatives << endl;
  626. float acc = (float)(truePositives + trueNegatives) / (float)(truePositives + trueNegatives + falsePositives + falseNegatives);
  627. cout << "Accuracy " << acc << endl;
  628. }
  629.  
  630. void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData)
  631. {
  632. //--Convert data
  633. const int rows = (int)train_samples.size();
  634. const int cols = (int)std::max(train_samples[0].cols, train_samples[0].rows);
  635. cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed
  636. trainData = cv::Mat(rows, cols, CV_32FC1);
  637. std::vector< Mat >::const_iterator itr = train_samples.begin();
  638. std::vector< Mat >::const_iterator end = train_samples.end();
  639. for (int i = 0; itr != end; ++itr, ++i)
  640. {
  641. CV_Assert(itr->cols == 1 ||
  642. itr->rows == 1);
  643. if (itr->cols == 1)
  644. {
  645. transpose(*(itr), tmp);
  646. tmp.copyTo(trainData.row(i));
  647. }
  648. else if (itr->rows == 1)
  649. {
  650. itr->copyTo(trainData.row(i));
  651. }
  652. }
  653. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement