chart_detection_with_network.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/mcc.hpp>
  4. #include <opencv2/dnn.hpp>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace cv;
  8. using namespace mcc;
  9. const char *about = "Basic chart detection using neural network";
  10. const char *keys = {
  11. "{ help h usage ? | | show this message }"
  12. "{t | 0 | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0}"
  13. "{m | | File path of model, if you don't have the model you can \
  14. find the link in the documentation}"
  15. "{pb | | File path of pbtxt file, available along with with the model \
  16. file }"
  17. "{v | | Input from video file, if ommited, input comes from camera }"
  18. "{ci | 0 | Camera id if input doesnt come from video (-v) }"
  19. "{nc | 1 | Maximum number of charts in the image }"
  20. "{use_gpu | | Add this flag if you want to use gpu}"};
  21. int main(int argc, char *argv[])
  22. {
  23. // ----------------------------------------------------------
  24. // Scroll down a bit (~50 lines) to find actual relevant code
  25. // ----------------------------------------------------------
  26. CommandLineParser parser(argc, argv, keys);
  27. parser.about(about);
  28. if (parser.has("help"))
  29. {
  30. parser.printMessage();
  31. return -1;
  32. }
  33. int t = parser.get<int>("t");
  34. CV_Assert(0 <= t && t <= 2);
  35. TYPECHART chartType = TYPECHART(t);
  36. string model_path = parser.get<string>("m");
  37. string pbtxt_path = parser.get<string>("pb");
  38. int camId = parser.get<int>("ci");
  39. int nc = parser.get<int>("nc");
  40. String video;
  41. if (parser.has("v"))
  42. video = parser.get<String>("v");
  43. bool use_gpu = parser.has("use_gpu");
  44. if (!parser.check())
  45. {
  46. parser.printErrors();
  47. return 0;
  48. }
  49. VideoCapture inputVideo;
  50. int waitTime;
  51. if (!video.empty())
  52. {
  53. inputVideo.open(video);
  54. waitTime = 10;
  55. }
  56. else
  57. {
  58. inputVideo.open(camId);
  59. waitTime = 10;
  60. }
  61. //--------------------------------------------------------------------------
  62. //-------------------------Actual Relevant Code-----------------------------
  63. //--------------------------------------------------------------------------
  64. //load the network
  65. cv::dnn::Net net = cv::dnn::readNetFromTensorflow(model_path, pbtxt_path);
  66. if (use_gpu)
  67. {
  68. net.setPreferableBackend(dnn::DNN_BACKEND_CUDA);
  69. net.setPreferableTarget(dnn::DNN_TARGET_CUDA);
  70. }
  71. Ptr<CCheckerDetector> detector = CCheckerDetector::create();
  72. if (!detector->setNet(net))
  73. {
  74. cout << "Loading Model failed: Aborting" << endl;
  75. return 0;
  76. }
  77. while (inputVideo.grab())
  78. {
  79. Mat image, imageCopy;
  80. inputVideo.retrieve(image);
  81. imageCopy = image.clone();
  82. // Marker type to detect
  83. if (!detector->process(image, chartType, nc, true))
  84. {
  85. printf("ChartColor not detected \n");
  86. }
  87. else
  88. {
  89. // get checker
  90. std::vector<Ptr<mcc::CChecker>> checkers = detector->getListColorChecker();
  91. for (Ptr<mcc::CChecker> checker : checkers)
  92. {
  93. // current checker
  94. Ptr<CCheckerDraw> cdraw = CCheckerDraw::create(checker);
  95. cdraw->draw(image);
  96. }
  97. }
  98. imshow("image result | q or esc to quit", image);
  99. imshow("original", imageCopy);
  100. char key = (char)waitKey(waitTime);
  101. if (key == 27)
  102. break;
  103. }
  104. return 0;
  105. }