123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include "functions.h"
- functions::functions(QWidget *parent) : QMainWindow(parent)
- {
- }
- int functions::init_detect_visages(void) {
- //-- 1. Load the cascades
- if(!face_cascade.load(face_cascade_name)) {
- qDebug() << "couc";
- }
- if(!eyes_cascade.load(eyes_cascade_name)) {
- qDebug() << "couc";
- }
- }
- cv::Mat functions::detect_visages(cv::Mat frame, Color_type type) {
- //cv::cvtColor(frame, frame, cv::COLOR_RGB2BGR);
- //cv::imwrite("out1.jpg", frame);
- qDebug() << type;
- cv::cvtColor( frame, frame_gray, cv::COLOR_BGR2GRAY );
- cv::cvtColor(frame_gray, frame_fake_gray, cv::COLOR_GRAY2RGB);
- equalizeHist( frame_gray, frame_gray );
- //-- Detect faces
- face_cascade.detectMultiScale( frame_gray,
- faces,
- 1.1,
- 2,
- 0|cv::CASCADE_SCALE_IMAGE/* HAAR_SCALE_IMAGE*/,
- cv::Size(30, 30) );
- for( size_t i = 0; i < faces.size(); i++ ) {
- cv::Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
- if (type == GREY8)
- 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 );
- if (type == RGB8 || type == BRG8 || type == BGR8)
- 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 );
- cv::Mat faceROI = frame_gray( faces[i] );
- //-- In each face, detect eyes
- eyes_cascade.detectMultiScale( faceROI,
- eyes,
- 1.1,
- 2,
- 0 |cv::CASCADE_SCALE_IMAGE/* HAAR_SCALE_IMAGE*/,
- cv::Size(30, 30) );
- for( size_t j = 0; j < eyes.size(); j++ ) {
- 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 );
- int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
- if (type == GREY8)
- circle( frame_fake_gray, center, radius, cv::Scalar( 255, 0, 0 ), 4, 8, 0 );
- if (type == RGB8 || type == BRG8 || type == BGR8)
- circle( frame, center, radius, cv::Scalar( 255, 0, 0 ), 4, 8, 0 );
- }
- }
- if (type == GREY8)
- return frame_fake_gray;
- if (type == RGB8 || type == BRG8 || type == BGR8)
- return frame;
- }
- int functions::init_dnn(void) {
- }
- cv::Mat functions::dnn_test(cv::Mat frame) {
- cv::String weights = "./ssd_mobilenet_v1_coco_11_06_2017/frozen_inference_graph.pb";
- cv::String prototxt = "./ssd_mobilenet_v1_coco_11_06_2017/ssd_mobilenet_v1_coco.pbtxt";
- cv::dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);
- const size_t inWidth = 300;
- const size_t inHeight = 300;
- const float WHRatio = inWidth / (float)inHeight;
- const char* classNames[]= {"background", "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
- "fire hydrant", "background", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "background", "backpack",
- "umbrella", "background", "background", "handbag", "tie", "suitcase", "frisbee","skis", "snowboard", "sports ball", "kite", "baseball bat","baseball glove", "skateboard", "surfboard", "tennis racket",
- "bottle", "background", "wine glass", "cup", "fork", "knife", "spoon","bowl", "banana", "apple", "sandwich", "orange","broccoli", "carrot", "hot dog", "pizza", "donut",
- "cake", "chair", "couch", "potted plant", "bed", "background", "dining table", "background", "background", "toilet", "background","tv", "laptop", "mouse", "remote", "keyboard",
- "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "background","book", "clock", "vase", "scissors","teddy bear", "hair drier", "toothbrush"};
- cv::Size frame_size = frame.size();
- cv::Size cropSize;
- if (frame_size.width / (float)frame_size.height > WHRatio) {
- cropSize = cv::Size(static_cast<int>(frame_size.height * WHRatio),
- frame_size.height);
- }
- else {
- cropSize = cv::Size(frame_size.width,
- static_cast<int>(frame_size.width / WHRatio));
- }
- cv::Rect crop( cv::Point((frame_size.width - cropSize.width) / 2,
- (frame_size.height - cropSize.height) / 2),
- cropSize);
- cv::Mat blob = cv::dnn::blobFromImage(frame, 1. / 255, cv::Size(300, 300));
- //cout << "blob size: " << blob.size << endl;
- net.setInput(blob);
- cv::Mat output = net.forward();
- //cout << "output size: " << output.size << endl;
- cv::Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());
- frame = frame(crop);
- float confidenceThreshold = 0.50;
- for (int i = 0; i < detectionMat.rows; i++)
- {
- float confidence = detectionMat.at<float>(i, 2);
- if (confidence > confidenceThreshold) {
- size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
- int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
- int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
- int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
- int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
- std::ostringstream ss;
- ss << confidence;
- cv::String conf(ss.str());
- cv::Rect object((int)xLeftBottom, (int)yLeftBottom,
- (int)(xRightTop - xLeftBottom),
- (int)(yRightTop - yLeftBottom));
- rectangle(frame, object, cv::Scalar(0, 255, 0), 2);
- //cout << "objectClass:" << objectClass << endl;
- cv::String label = cv::String(classNames[objectClass]) + ": " + conf;
- //cout << "label"<<label << endl;
- int baseLine = 0;
- cv::Size labelSize = getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
- rectangle(frame, cv::Rect(cv::Point(xLeftBottom, yLeftBottom - labelSize.height),
- cv::Size(labelSize.width, labelSize.height + baseLine)),
- cv::Scalar(0, 255, 0), cv::FILLED);
- putText(frame, label, cv::Point(xLeftBottom, yLeftBottom),
- cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
- }
- return frame;
- }
- }
|