conv2d_cv2.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "../include/conv2d_cv2.hpp"
  2. //to-do: conv2d class extending fpga
  3. int width = 224;
  4. int kernel = 5;
  5. int border = kernel/2;
  6. int sizeWithBorder = width + 2*border;
  7. int pixels = sizeWithBorder * sizeWithBorder;
  8. mutex respLock, srcLock;
  9. vector<Mat> respImg(3);
  10. Mat respImgBig = Mat::zeros(sizeWithBorder*3, sizeWithBorder*3, CV_32FC3);
  11. Mat srcImg;
  12. pthread_t tShow;
  13. pthread_attr_t attr2;
  14. bool run = 1;
  15. void showJobResp(commFPGA *fpga, jobResponse *res) {
  16. Mat im = Mat::zeros(sizeWithBorder, sizeWithBorder, CV_32FC1);
  17. for(int i = 0; i < sizeWithBorder; i++) {
  18. for(int j = 0; j < sizeWithBorder; j++) {
  19. int32_t val = (res->payload[i*sizeWithBorder + j]);
  20. im.at<float>(i, j) = 1.0 * val / (256 - 1);
  21. }
  22. }
  23. respLock.lock();
  24. respImg[res->tag] = im;
  25. merge(respImg, respImgBig);
  26. resize(respImgBig, respImgBig, cv::Size(sizeWithBorder*3,sizeWithBorder*3), 0, 0, CV_INTER_AREA);
  27. respLock.unlock();
  28. printf("%16s id %08X\n", fpga->ip, *res->jobId);
  29. }
  30. void *showThread(void *ref) {
  31. namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
  32. while(run && waitKey(16) != 27) {
  33. srcLock.lock();
  34. if(!srcImg.empty())
  35. imshow( "Display window", srcImg );
  36. srcLock.unlock();
  37. respLock.lock();
  38. if(!respImgBig.empty())
  39. imshow( "result", respImgBig);
  40. respLock.unlock();
  41. }
  42. pthread_exit(NULL);
  43. }
  44. int main_conv2d() {
  45. connection_init();
  46. usleep(5000);
  47. pthread_attr_init(&attr2);
  48. pthread_attr_setdetachstate(&attr2, PTHREAD_CREATE_JOINABLE);
  49. pthread_create(&tShow, &attr2, showThread, 0);
  50. pthread_setname_np(tShow, "tShow");
  51. VideoCapture stream("sample.mp4");
  52. //VideoCapture stream(-1);
  53. if( !stream.isOpened() ) {
  54. fprintf( stderr, "ERROR: cannot open camera \n" );
  55. return -1;
  56. }
  57. stream.set(CV_CAP_PROP_BUFFERSIZE, 3);
  58. //1 job for each channel
  59. jobData job[3] = {pixels, pixels, pixels};
  60. for(uint i=0; i<3; i++) {
  61. *job[i].moduleId = moduleIds[conv2D_5x5_Module];
  62. job[i].tag = i;
  63. respImg[i] = Mat::zeros(sizeWithBorder, sizeWithBorder, CV_32FC1);
  64. }
  65. for(uint i=0; i<fpgaCount; i++) {
  66. fpgas[i].setSuccessCb(&showJobResp);
  67. //fpgas[i].setFailedCb(&showJobResp);
  68. }
  69. Mat image;
  70. while(1) {
  71. stream.read(image);
  72. if(image.empty()) {
  73. break;
  74. }
  75. resize(image, image, cv::Size(width, width), 0, 0, CV_INTER_LINEAR);
  76. flip(image,image,1);
  77. copyMakeBorder(image, image, border, border, border, border, BORDER_REPLICATE);
  78. for(int i = 0; i < image.rows; i++) {
  79. for(int j = 0; j < image.cols; j++) {
  80. Vec3b bgrPixel = image.at<Vec3b>(i, j);
  81. job[0].payload[i*sizeWithBorder + j] = bgrPixel[0];
  82. job[1].payload[i*sizeWithBorder + j] = bgrPixel[1];
  83. job[2].payload[i*sizeWithBorder + j] = bgrPixel[2];
  84. }
  85. }
  86. sendJob(&job[0], pixels);
  87. sendJob(&job[1], pixels);
  88. sendJob(&job[2], pixels);
  89. srcLock.lock();
  90. resize(image, srcImg, cv::Size(sizeWithBorder*3, sizeWithBorder*3), 0, 0, CV_INTER_AREA);
  91. srcLock.unlock();
  92. }
  93. void *status;
  94. //run = 0;
  95. pthread_join(tShow, &status);
  96. connection_close();
  97. return 0;
  98. }