functions.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "functions.h"
  2. functions::functions(QWidget *parent) : QMainWindow(parent)
  3. {
  4. }
  5. int functions::init_detect_visages(void) {
  6. //-- 1. Load the cascades
  7. if(!face_cascade.load(face_cascade_name)) {
  8. qDebug() << "couc";
  9. }
  10. if(!eyes_cascade.load(eyes_cascade_name)) {
  11. qDebug() << "couc";
  12. }
  13. }
  14. cv::Mat functions::detect_visages(cv::Mat frame, Color_type type) {
  15. //cv::cvtColor(frame, frame, cv::COLOR_RGB2BGR);
  16. //cv::imwrite("out1.jpg", frame);
  17. qDebug() << type;
  18. cv::cvtColor( frame, frame_gray, cv::COLOR_BGR2GRAY );
  19. cv::cvtColor(frame_gray, frame_fake_gray, cv::COLOR_GRAY2RGB);
  20. equalizeHist( frame_gray, frame_gray );
  21. //-- Detect faces
  22. face_cascade.detectMultiScale( frame_gray,
  23. faces,
  24. 1.1,
  25. 2,
  26. 0|cv::CASCADE_SCALE_IMAGE/* HAAR_SCALE_IMAGE*/,
  27. cv::Size(30, 30) );
  28. for( size_t i = 0; i < faces.size(); i++ ) {
  29. cv::Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
  30. if (type == GREY8)
  31. ellipse( frame_fake_gray, center, cv::Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, cv::Scalar( 255, 0, 255 ), 4, 8, 0 );
  32. if (type == RGB8 || type == BRG8 || type == BGR8)
  33. ellipse( frame, center, cv::Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, cv::Scalar( 255, 0, 255 ), 4, 8, 0 );
  34. cv::Mat faceROI = frame_gray( faces[i] );
  35. //-- In each face, detect eyes
  36. eyes_cascade.detectMultiScale( faceROI,
  37. eyes,
  38. 1.1,
  39. 2,
  40. 0 |cv::CASCADE_SCALE_IMAGE/* HAAR_SCALE_IMAGE*/,
  41. cv::Size(30, 30) );
  42. for( size_t j = 0; j < eyes.size(); j++ ) {
  43. cv::Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
  44. int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
  45. if (type == GREY8)
  46. circle( frame_fake_gray, center, radius, cv::Scalar( 255, 0, 0 ), 4, 8, 0 );
  47. if (type == RGB8 || type == BRG8 || type == BGR8)
  48. circle( frame, center, radius, cv::Scalar( 255, 0, 0 ), 4, 8, 0 );
  49. }
  50. }
  51. if (type == GREY8)
  52. return frame_fake_gray;
  53. if (type == RGB8 || type == BRG8 || type == BGR8)
  54. return frame;
  55. }
  56. int functions::init_dnn(void) {
  57. }
  58. cv::Mat functions::dnn_test(cv::Mat frame) {
  59. cv::String weights = "./ssd_mobilenet_v1_coco_11_06_2017/frozen_inference_graph.pb";
  60. cv::String prototxt = "./ssd_mobilenet_v1_coco_11_06_2017/ssd_mobilenet_v1_coco.pbtxt";
  61. cv::dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);
  62. const size_t inWidth = 300;
  63. const size_t inHeight = 300;
  64. const float WHRatio = inWidth / (float)inHeight;
  65. const char* classNames[]= {"background", "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
  66. "fire hydrant", "background", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "background", "backpack",
  67. "umbrella", "background", "background", "handbag", "tie", "suitcase", "frisbee","skis", "snowboard", "sports ball", "kite", "baseball bat","baseball glove", "skateboard", "surfboard", "tennis racket",
  68. "bottle", "background", "wine glass", "cup", "fork", "knife", "spoon","bowl", "banana", "apple", "sandwich", "orange","broccoli", "carrot", "hot dog", "pizza", "donut",
  69. "cake", "chair", "couch", "potted plant", "bed", "background", "dining table", "background", "background", "toilet", "background","tv", "laptop", "mouse", "remote", "keyboard",
  70. "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "background","book", "clock", "vase", "scissors","teddy bear", "hair drier", "toothbrush"};
  71. cv::Size frame_size = frame.size();
  72. cv::Size cropSize;
  73. if (frame_size.width / (float)frame_size.height > WHRatio) {
  74. cropSize = cv::Size(static_cast<int>(frame_size.height * WHRatio),
  75. frame_size.height);
  76. }
  77. else {
  78. cropSize = cv::Size(frame_size.width,
  79. static_cast<int>(frame_size.width / WHRatio));
  80. }
  81. cv::Rect crop( cv::Point((frame_size.width - cropSize.width) / 2,
  82. (frame_size.height - cropSize.height) / 2),
  83. cropSize);
  84. cv::Mat blob = cv::dnn::blobFromImage(frame, 1. / 255, cv::Size(300, 300));
  85. //cout << "blob size: " << blob.size << endl;
  86. net.setInput(blob);
  87. cv::Mat output = net.forward();
  88. //cout << "output size: " << output.size << endl;
  89. cv::Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());
  90. frame = frame(crop);
  91. float confidenceThreshold = 0.50;
  92. for (int i = 0; i < detectionMat.rows; i++)
  93. {
  94. float confidence = detectionMat.at<float>(i, 2);
  95. if (confidence > confidenceThreshold) {
  96. size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
  97. int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
  98. int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
  99. int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
  100. int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
  101. std::ostringstream ss;
  102. ss << confidence;
  103. cv::String conf(ss.str());
  104. cv::Rect object((int)xLeftBottom, (int)yLeftBottom,
  105. (int)(xRightTop - xLeftBottom),
  106. (int)(yRightTop - yLeftBottom));
  107. rectangle(frame, object, cv::Scalar(0, 255, 0), 2);
  108. //cout << "objectClass:" << objectClass << endl;
  109. cv::String label = cv::String(classNames[objectClass]) + ": " + conf;
  110. //cout << "label"<<label << endl;
  111. int baseLine = 0;
  112. cv::Size labelSize = getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  113. rectangle(frame, cv::Rect(cv::Point(xLeftBottom, yLeftBottom - labelSize.height),
  114. cv::Size(labelSize.width, labelSize.height + baseLine)),
  115. cv::Scalar(0, 255, 0), cv::FILLED);
  116. putText(frame, label, cv::Point(xLeftBottom, yLeftBottom),
  117. cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
  118. }
  119. return frame;
  120. }
  121. }