Path: blob/master/FaceDetectionComparison/face_detection_opencv_dnn.cpp
3118 views
#include <iostream>1#include <string>2#include <vector>3#include <stdlib.h>4#include <opencv2/core.hpp>5#include <opencv2/imgproc.hpp>6#include <opencv2/highgui.hpp>7#include <opencv2/dnn.hpp>8#include <boost/algorithm/string.hpp>910using namespace cv;11using namespace cv::dnn;12using namespace std;1314const size_t inWidth = 300;15const size_t inHeight = 300;16const double inScaleFactor = 1.0;17const float confidenceThreshold = 0.7;18const cv::Scalar meanVal(104.0, 177.0, 123.0);1920const std::string caffeConfigFile = "models/deploy.prototxt";21const std::string caffeWeightFile = "models/res10_300x300_ssd_iter_140000_fp16.caffemodel";2223const std::string tensorflowConfigFile = "models/opencv_face_detector.pbtxt";24const std::string tensorflowWeightFile = "models/opencv_face_detector_uint8.pb";2526void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN, string framework)27{28int frameHeight = frameOpenCVDNN.rows;29int frameWidth = frameOpenCVDNN.cols;3031cv::Mat inputBlob;32if (framework == "caffe")33inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);34else35inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);3637net.setInput(inputBlob, "data");38cv::Mat detection = net.forward("detection_out");3940cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());4142for(int i = 0; i < detectionMat.rows; i++)43{44float confidence = detectionMat.at<float>(i, 2);4546if(confidence > confidenceThreshold)47{48int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frameWidth);49int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frameHeight);50int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frameWidth);51int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frameHeight);5253cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),2, 4);54}55}5657}585960int main( int argc, const char** argv )61{62string videoFileName;63string device;64string framework;6566// Take arguments from command line67if (argc == 4)68{69videoFileName = argv[1];70device = argv[2];71framework = argv[3];72}73else if (argc == 3)74{75videoFileName = argv[1];76device = argv[2];77framework = "caffe";78}79else if (argc == 2)80{81videoFileName = argv[1];82device = "cpu";83framework = "caffe";84}85else86{87videoFileName = "";88device = "cpu";89framework = "caffe";90}9192boost::to_upper(device);93cout << "Configuration:" << endl;94cout << "Device - "<< device << endl;95if (framework == "caffe")96cout << "Network type - Caffe" << endl;97else98cout << "Network type - TensorFlow" << endl;99if (videoFileName == "")100cout << "No video found, using camera stream" << endl;101else102cout << "Video file - " << videoFileName << endl;103104Net net;105106if (framework == "caffe")107net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);108else109net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);110111#if (CV_MAJOR_VERSION >= 4)112if (device == "CPU")113{114net.setPreferableBackend(DNN_TARGET_CPU);115}116else117{118net.setPreferableBackend(DNN_BACKEND_CUDA);119net.setPreferableTarget(DNN_TARGET_CUDA);120}121#else122// force CPU backend for OpenCV 3.x as CUDA backend is not supported there123net.setPreferableBackend(DNN_BACKEND_DEFAULT);124device = "cpu";125#endif126127cv::VideoCapture source;128if (videoFileName != "")129source.open(videoFileName);130else131source.open(0, CAP_V4L);132133Mat frame;134135double tt_opencvDNN = 0;136double fpsOpencvDNN = 0;137138while (true)139{140source >> frame;141if (frame.empty())142break;143144double t = cv::getTickCount();145detectFaceOpenCVDNN(net, frame, framework);146tt_opencvDNN = ((double)cv::getTickCount() - t)/cv::getTickFrequency();147fpsOpencvDNN = 1/tt_opencvDNN;148149putText(frame, format("OpenCV DNN %s FPS = %.2f", device.c_str(), fpsOpencvDNN), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.3, Scalar(0, 0, 255), 4);150151imshow("OpenCV - DNN Face Detection", frame);152153int k = waitKey(5);154if(k == 27)155{156destroyAllWindows();157break;158}159}160}161162163