detection

#include "celeba_crop.h"
#include <algorithm>


vector<string> celeba_crop::txt_read(string file) {

    vector<string> result;
    std::ifstream img_path_file(file);
    while (!img_path_file.eof()) {
        string line;
        getline(img_path_file, line);
        if (line == "")
            continue;
        result.push_back(line);
    }
    return result;
}


void celeba_crop::txt_write(vector<vector<float>> path, string name)
{
    ofstream file;
    file.open (txt_root_path_ + name + ".txt");
    for(int i = 0; i < path.size(); i++)
    {
        for(int j = 0; j < path[i].size(); j++)
        {
            file << path[i][j] << " ";
        }
        file << endl;
    }
    file.close();
}

void celeba_crop::seperate_path_bbox(vector<string> path)
{
    for(int i = 0; i < path.size(); i++) {
        char *cstr = new char[path[i].length() + 1];
        strcpy(cstr, path[i].c_str());
        char *token = std::strtok(cstr, " ");
        string a(token);
        img_path_.push_back(a);
        token = std::strtok(NULL, "\n");
        string b(token);
        rects_path_.push_back(b);
    }
}

void celeba_crop::create_landmark_path(vector<string> path)
{
    for(int i = 0; i < path.size(); i++) {
        char *cstr = new char[path[i].length() + 1];
        strcpy(cstr, path[i].c_str());
        char *token = std::strtok(cstr, " ");
        token = std::strtok(NULL, "\n");
        string b(token);
        landmark_path_.push_back(b);
    }
}

vector<vector<float>> celeba_crop::path_convert2_num(vector<string> path)
{
    vector<vector<float>> nums;
    for(int i = 0; i < path.size(); i++)
    {
        vector<float> num;
        char *cstr = new char[path[i].length() + 1];
        strcpy(cstr, path[i].c_str());
        char *token = std::strtok(cstr, " ");
        while(token != 0)
        {
            float p = atof(token);
            num.push_back(p);
            token = strtok(NULL, " ");
        }
        nums.push_back(num);
    }
    return nums;
}


void celeba_crop::create_regression()
{
    for(int i = 0; i < img_bbox_path_.size(); i++)
    {
        vector<float> v2;
        for(int j = 0; j < 4; j++)
        {
            float v1 = (rand() % 11 - 5) * 1.0 / 8;
            v2.push_back(v1);
        }
        regression_nums_.push_back(v2);
    }
}

void celeba_crop::regression_rects()
{
    for(int i = 0; i < img_bbox_path_.size(); i++)
		//		for (int i = 0; i < 15000; i++)
    {
        Rect rect;
        rect.x = rects_num_[i][0] + rects_num_[i][2] * regression_nums_[i][0];
        rect.y = rects_num_[i][1] + rects_num_[i][3] * regression_nums_[i][1];
        rect.width = rects_num_[i][2] * (1 + regression_nums_[i][2]);
        rect.height = rects_num_[i][3] * (1 + regression_nums_[i][3]);
        rects_.push_back(rect);
    }
}

void celeba_crop::regression_landmark()
{
    for(int i = 0; i < landmark_.size(); i++)
    {
        for(int j = 0; j < landmark_[i].size(); j++)
        {
            if(j % 2 == 0)
                landmark_[i][j] = ( landmark_[i][j] - rects_[i].x ) / rects_[i].width;

            else
                landmark_[i][j] = ( landmark_[i][j] - rects_[i].y ) / rects_[i].height;
        }
    }
}

void celeba_crop::crop_img()
{
    ofstream file, file_label;
    file_label.open (txt_root_path_ + "label.txt");
    file.open (txt_root_path_ + "crop_image.txt");
	int j = 0;
    for(int i = 0; i < img_path_.size(); i++)
//     for (int i = 0; i < 15000; i++)
    {
        Mat img = imread(img_root_path_ + img_path_[i]);
        if(rects_[i].x < 0) rects_[i].x =0;
        if(rects_[i].y < 0) rects_[i].y =0;
        if(rects_[i].width + rects_[i].x > img.cols) rects_[i].width = img.cols - rects_[i].x ;
        if(rects_[i].height + rects_[i].y > img.rows) rects_[i].height = img.rows - rects_[i].y ;
		// overlap is invalid in the beginning
		double o = -1;

		//double   x1;      // left corner
		//double   y1;      // top corner
		//double   x2;      // right corner
		//double   y2;      // bottom corner
		
		
		double x1 = max(rects_num_[i][0], float(rects_[i].x));
		double y1 = max(rects_num_[i][1], float(rects_[i].y));
		double x2 = min(rects_num_[i][0] + rects_num_[i][2], float(rects_[i].x+ rects_[i].width));
		double y2 = min(rects_num_[i][1] + rects_num_[i][3], float(rects_[i].y+ rects_[i].height));
		
		// compute width and height of overlapping area
		double w = x2 - x1;
		double h = y2 - y1;

		// set invalid entries to 0 overlap
		if (w <= 0 || h <= 0)
			o=0;

		// get overlapping areas
		double inter = w*h;
		o = inter / (rects_num_[i][2] * rects_num_[i][3]);
	//	if (o < 0.5||o>0.7)     //part
		if (o >0.1)               //negtive
		//	if (o <0.9)               //true
			continue;        

		//if (rects_[i].width*rects_[i].height / (rects_num_[i][2] * rects_num_[i][3]) < 0.      ||rects_[i].width*rects_[i].height / (rects_num_[i][2] * rects_num_[i][3])>0.7)
		//	continue;
		  Mat img_crop = img(rects_[i]);
		  
        imwrite(txt_root_path_ + "crop2/" + img_path_[j], img_crop);
        file << txt_root_path_ + "crop2/" + img_path_[j] << endl;
        file_label << "0" << endl;
		j++;   
    }
    file.close();
    file_label.close();
}

void celeba_crop::run()
{

    img_bbox_path_ = txt_read(img_txt_path_);
    seperate_path_bbox(img_bbox_path_);
    img_landmark_path_ = txt_read(landmark_txt_path_);
    create_landmark_path(img_landmark_path_);
    landmark_ = path_convert2_num(landmark_path_);
    rects_num_ = path_convert2_num(rects_path_);
    create_regression();
    regression_rects();
    regression_landmark();
    txt_write(landmark_, "landmark");
    txt_write(regression_nums_, "rectangle");
    crop_img();
}




#include <iostream>
#include "MTCNN.h"
#include "opencv2/opencv.hpp"

//#include "D:\学习软件\opencv3.1\sources\include\opencv2/opencv.hpp"



using namespace std;
using namespace cv;

//int main() {
//
//    vector<string> model_file = {
//            "../model/det1.prototxt",
//            "../model/det2.prototxt",
//            "../model/det3.prototxt"
            "../model/det4.prototxt"
//    };
//
//    vector<string> trained_file = {
//            "../model/det1.caffemodel",
//            "../model/det2.caffemodel",
//            "../model/det3.caffemodel"
            "../model/det4.caffemodel"
//    };
//
//    MTCNN mtcnn(model_file, trained_file);
//
//    vector<Rect> rectangles;
//    string img_path = "../result/trump.jpg";
//    Mat img = imread(img_path);
//
//    mtcnn.detection(img, rectangles);
//
//    std::cout << "Hello, World!" << std::endl;
//    return 0;
//}

int main() {


    //the vector used to input the address of the net model
    vector<string> model_file = {
            "../model/det1.prototxt",
            "../model/det2.prototxt",
            "../model/det3.prototxt"
//            "../model/det4.prototxt"
    };




    //the vector used to input the address of the net parameters
    vector<string> trained_file = {
            "../model2/det1.caffemodel",
            "../model2/det2.caffemodel",
            "../model2/det3.caffemodel"
//            "../model/det4.caffemodel"
    };

	//vector<string> trained_file = {
	//	"../model/200图片模型/MTCNN_train_iter_1000.caffemodel",
	//	"../model/200图片模型/MTCNN_train_iter_2000.caffemodel",
	//	"../model/200图片模型/MTCNN_train_iter_3000.caffemodel"
	//	//            "../model/det4.caffemodel"
	//};



    MTCNN mtcnn(model_file, trained_file);

      // VideoCapture cap(0);

   //  VideoCapture cap("../../SuicideSquad.mp4");
	 //	VideoCapture cap("E:/test_clip.avi");
	 	VideoCapture cap("E:/test1.ts");
	 //		VideoCapture cap("E:/留学.mp4");
	if (!cap.isOpened()) {
		std::cerr << "无法获取视频\n" << endl;
	}
    //VideoWriter writer;
//    writer.open("../result/SuicideSquad.mp4",CV_FOURCC('M', 'J', 'P', 'G'), 25, Size(1280,720), true);
//	 writer.open("E:\\4.mp4", CV_FOURCC('M', 'J', 'P', 'G'), 25, Size(1280, 720), true);
	Mat img;// = imread("E:/012.jpg");
//	resize(img, img, cvSize(660, 360));
    int frame_count = 0;

	while (cap.read(img))
    {
		if (frame_count % 10 == 0)
		{

			vector<Rect> rectangles;
			vector<float> confidences;
			std::vector<std::vector<cv::Point>> alignment;
			double t = cvGetTickCount();
			
			mtcnn.detection(img, rectangles, confidences, alignment);
			
			for (int i = 0; i < rectangles.size(); i++)
			{
				if (confidences[i] < 0.93)    //remend
					continue;                   //remend
				int green = confidences[i] * 255;
				int red = (1 - confidences[i]) * 255;
				rectangle(img, rectangles[i], cv::Scalar(0, green, red), 3);
				for (int j = 0; j < alignment[i].size(); j++)
				{
					cv::circle(img, alignment[i][j], 5, cv::Scalar(255, 255, 0), 3);
				}
			}

			//	frame_count++;
			cv::putText(img, std::to_string(frame_count), cvPoint(3, 13),
				cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar(0, 255, 0), 1, CV_AA);
			t = ((double)cvGetTickCount() - t) / getTickFrequency();
			//        writer.write(img);
			imshow("Live", img);
			printf("%lf\n", t);
			frame_count++;
			//		Mat kk;
			//resize(img,kk,cvSize(660,360));
			//		imshow("Live", kk);
			waitKey(1);
		}
		else
		{
			//        writer.write(img);
			imshow("Live", img);

			frame_count++;
			//		Mat kk;
					//resize(img,kk,cvSize(660,360));
			//		imshow("Live", kk);
			waitKey(30);
		}
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值