You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknetDetector.h"
  2. darknetDetector::darknetDetector(){
  3. m_dArea = 20.0;
  4. m_iSST = 1;
  5. m_iCounterSST = 0;
  6. m_iWindow = 0;
  7. m_iCounterWindow = 0;
  8. m_bPIP = false;
  9. m_bDilatePicture = false;
  10. m_fThresh = .24f;
  11. m_fHier_Thresh = .5f;
  12. //m_sOptions = read_data_cfg("data/cfg/voc.data");
  13. m_vNames = get_labels("data/voc.names");
  14. //m_vAlphabet = load_alphabet();
  15. //m_oNeuralNetwork = parse_network_cfg("data/cfg/tiny-yolo-voc.cfg");
  16. m_oNeuralNetwork = parse_network_cfg("data/cfg/tiny-yolo.cfg");
  17. //load_weights(&m_oNeuralNetwork, "data/tiny-yolo-voc.weights");
  18. load_weights(&m_oNeuralNetwork, "data/tiny-yolo.weights");
  19. set_batch_network(&m_oNeuralNetwork, 1);
  20. srand(2222222);
  21. //image im = load_image_color(input, 0, 0);
  22. //image sized;
  23. //sized.c = 3;
  24. m_iHeight = m_oNeuralNetwork.h;
  25. m_iWidth = m_oNeuralNetwork.w;
  26. //m_oMat = cv::Mat(m_iHeight, m_iWidth, CV_32FC3);
  27. //sized.data = (float*)m_oMat.data;
  28. m_lLastLayer = m_oNeuralNetwork.layers[m_oNeuralNetwork.n - 1];
  29. int j;
  30. m_pBoxes = (box*)calloc(m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n, sizeof(box));
  31. m_vProbs = (float**)calloc(m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n, sizeof(float *));
  32. for (j = 0; j < m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n; ++j) m_vProbs[j] = (float*)calloc(m_lLastLayer.classes + 1, sizeof(float *));
  33. m_vMasks = NULL;
  34. if (m_lLastLayer.coords > 4){
  35. m_vMasks = (float**)calloc(m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n, sizeof(float*));
  36. for (j = 0; j < m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n; ++j) m_vMasks[j] = (float*)calloc(m_lLastLayer.coords - 4, sizeof(float *));
  37. }
  38. };
  39. darknetDetector::~darknetDetector(){
  40. };
  41. void darknetDetector::setParametersEx(int SST, int DetectionWindow){
  42. m_iSST = SST;
  43. m_iWindow = DetectionWindow;
  44. };
  45. bool darknetDetector::m_fImageAnalyzer(Mat &Picture){
  46. if( Picture.cols > 0 && Picture.rows > 0 ){
  47. m_mBufferProtectorMutex.lock();
  48. Mat tMat = cv::Mat(m_iHeight, m_iWidth, CV_8UC3);
  49. Picture.copyTo(m_oMat);
  50. Picture.copyTo(tMat);
  51. resize(tMat, tMat, Size(m_iWidth, m_iHeight), 0.0, 0.0, INTER_AREA);
  52. //cvtColor( tMat, tMat, CV_RGB2GRAY );
  53. IplImage *tIPL;
  54. tIPL = new IplImage(tMat);
  55. DN::image im;
  56. im = DN::ipl_to_image(tIPL);
  57. network_predict(m_oNeuralNetwork, (float*)im.data );
  58. m_mBufferProtectorMutex.unlock();
  59. };
  60. return true;
  61. };
  62. void darknetDetector::markup(Mat &Picture){
  63. float nms = .3f;
  64. //char Buffer[20];
  65. //sprintf(Buffer, "Motion Detected");
  66. //rectangle(Picture, Point(Picture.cols - 120, 5), Point(Picture.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED);
  67. //putText(Picture, Buffer, Point(Picture.cols - 115, 13), FONT_HERSHEY_SIMPLEX, 0.3, Scalar(255, 0, 0), 1);
  68. m_mBufferProtectorMutex.lock();
  69. get_region_boxes(
  70. m_lLastLayer,
  71. Picture.cols,
  72. Picture.rows,
  73. m_oNeuralNetwork.w,
  74. m_oNeuralNetwork.h,
  75. m_fThresh,
  76. m_vProbs,
  77. m_pBoxes,
  78. m_vMasks,
  79. 0,
  80. 0,
  81. m_fHier_Thresh,
  82. 1
  83. );
  84. if (nms)
  85. do_nms_obj(
  86. m_pBoxes,
  87. m_vProbs,
  88. m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n,
  89. m_lLastLayer.classes,
  90. nms
  91. );
  92. //else if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);
  93. /*
  94. IplImage *tIPL;
  95. //tIPL = new IplImage(Picture);
  96. tIPL = new IplImage(m_oMat);
  97. DN::image im;
  98. im = DN::ipl_to_image(tIPL);
  99. draw_detections(
  100. im,
  101. m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n,
  102. m_fThresh,
  103. m_pBoxes,
  104. m_vProbs,
  105. m_vMasks,
  106. m_vNames,
  107. m_vAlphabet,
  108. m_lLastLayer.classes
  109. );
  110. //IplImage *tIPL2;
  111. tIPL = new IplImage(Picture);
  112. DN::image_into_ipl(im, tIPL);
  113. */
  114. m_oMat.copyTo(Picture);
  115. drawMAT_detections(
  116. Picture,
  117. m_lLastLayer.w*m_lLastLayer.h*m_lLastLayer.n,
  118. m_fThresh,
  119. m_pBoxes,
  120. m_vProbs,
  121. m_vMasks,
  122. m_vNames,
  123. m_lLastLayer.classes
  124. );
  125. m_mBufferProtectorMutex.unlock();
  126. };
  127. /*
  128. #include <cv.h>
  129. #include <highgui.h>
  130. #include <iostream>
  131. using namespace cv;
  132. int main( int argc, char** argv )
  133. {
  134. double alpha = 0.5; double beta; double input;
  135. Mat src1, src2, dst;
  136. /// Ask the user enter alpha
  137. std::cout<<" Simple Linear Blender "<<std::endl;
  138. std::cout<<"-----------------------"<<std::endl;
  139. std::cout<<"* Enter alpha [0-1]: ";
  140. std::cin>>input;
  141. /// We use the alpha provided by the user if it is between 0 and 1
  142. if( input >= 0.0 && input <= 1.0 )
  143. { alpha = input; }
  144. /// Read image ( same size, same type )
  145. src1 = imread("../../images/LinuxLogo.jpg");
  146. src2 = imread("../../images/WindowsLogo.jpg");
  147. if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
  148. if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
  149. /// Create Windows
  150. namedWindow("Linear Blend", 1);
  151. beta = ( 1.0 - alpha );
  152. addWeighted( src1, alpha, src2, beta, 0.0, dst);
  153. imshow( "Linear Blend", dst );
  154. waitKey(0);
  155. return 0;
  156. }
  157. */