mirror of
https://github.com/RustyRaptor/learnopencv.git
synced 2026-12-03 22:15:16 -07:00
97 lines
2.9 KiB
C++
97 lines
2.9 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <stdlib.h>
|
|
#include <opencv2/core.hpp>
|
|
#include <opencv2/imgproc.hpp>
|
|
#include <opencv2/highgui.hpp>
|
|
#include <opencv2/dnn.hpp>
|
|
|
|
using namespace cv;
|
|
using namespace std;
|
|
using namespace cv::dnn;
|
|
|
|
const size_t inWidth = 300;
|
|
const size_t inHeight = 300;
|
|
const double inScaleFactor = 1.0;
|
|
const float confidenceThreshold = 0.7;
|
|
const cv::Scalar meanVal(104.0, 177.0, 123.0);
|
|
|
|
#define CAFFE
|
|
|
|
const std::string caffeConfigFile = "./deploy.prototxt";
|
|
const std::string caffeWeightFile = "./res10_300x300_ssd_iter_140000_fp16.caffemodel";
|
|
|
|
const std::string tensorflowConfigFile = "./opencv_face_detector.pbtxt";
|
|
const std::string tensorflowWeightFile = "./opencv_face_detector_uint8.pb";
|
|
|
|
void detectFaceOpenCVDNN(Net net, Mat &frameOpenCVDNN)
|
|
{
|
|
int frameHeight = frameOpenCVDNN.rows;
|
|
int frameWidth = frameOpenCVDNN.cols;
|
|
#ifdef CAFFE
|
|
cv::Mat inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false, false);
|
|
#else
|
|
cv::Mat inputBlob = cv::dnn::blobFromImage(frameOpenCVDNN, inScaleFactor, cv::Size(inWidth, inHeight), meanVal, true, false);
|
|
#endif
|
|
|
|
net.setInput(inputBlob, "data");
|
|
cv::Mat detection = net.forward("detection_out");
|
|
|
|
cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
|
|
|
|
for(int i = 0; i < detectionMat.rows; i++)
|
|
{
|
|
float confidence = detectionMat.at<float>(i, 2);
|
|
|
|
if(confidence > confidenceThreshold)
|
|
{
|
|
int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * frameWidth);
|
|
int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * frameHeight);
|
|
int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * frameWidth);
|
|
int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * frameHeight);
|
|
|
|
cv::rectangle(frameOpenCVDNN, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),2, 4);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
int main( int argc, const char** argv )
|
|
{
|
|
#ifdef CAFFE
|
|
Net net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
|
|
#else
|
|
Net net = cv::dnn::readNetFromTensorflow(tensorflowWeightFile, tensorflowConfigFile);
|
|
#endif
|
|
|
|
VideoCapture source;
|
|
if (argc == 1)
|
|
source.open(0);
|
|
else
|
|
source.open(argv[1]);
|
|
Mat frame;
|
|
|
|
double tt_opencvDNN = 0;
|
|
double fpsOpencvDNN = 0;
|
|
while(1)
|
|
{
|
|
source >> frame;
|
|
if(frame.empty())
|
|
break;
|
|
double t = cv::getTickCount();
|
|
detectFaceOpenCVDNN ( net, frame );
|
|
tt_opencvDNN = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
|
|
fpsOpencvDNN = 1/tt_opencvDNN;
|
|
putText(frame, format("OpenCV DNN ; FPS = %.2f",fpsOpencvDNN), Point(10, 50), FONT_HERSHEY_SIMPLEX, 1.4, Scalar(0, 0, 255), 4);
|
|
imshow( "OpenCV - DNN Face Detection", frame );
|
|
int k = waitKey(5);
|
|
if(k == 27)
|
|
{
|
|
destroyAllWindows();
|
|
break;
|
|
}
|
|
}
|
|
}
|