微表情数据集预处理
c++复现数据集预处理过程,参考源码:
https://github.com/genbing99/SoftNet-SpotME
extraction_preprocess.py
#include <opencv2/opencv.hpp>
#include <direct.h>
#include <iostream>
#include <io.h>
#include <stdio.h>
std::vector<cv::Rect> detectFaces(const cv::Mat& img) {
cv::Mat gray = img.clone();
if (gray.type() == CV_8UC3) cv::cvtColor(gray, gray, cv::COLOR_BGR2GRAY);
cv::CascadeClassifier faces_cascade;
faces_cascade.load("haarcascade_frontalface_alt.xml");
std::vector<cv::Rect> faces;
faces_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));
return faces;
}
void detectface()
{
cv::Mat src = cv::imread("basketball1.png");
std::vector<cv::Rect> faces = detectFaces(src);
if (faces.size() < 1) return;
for (auto iter = faces.begin(); iter != faces.end(); iter++)
{
cv::rectangle(src, *iter, cv::Scalar(255, 0, 0));
}
cv::imshow("face_det.jpg", src);
cv::waitKey(0);
//_mkdir("mint");
}
int TraverseDirectory(std::string path, std::vector<std::string>& subFolders)
{
__int64 Handle;
struct __finddata64_t FileInfo;
std::string strFind = std::string(path) + "\\*";
subFolders.clear();
if ((Handle = _findfirst64(strFind.c_str(), &FileInfo)) == -1L)
{
printf("没有找到匹配的项目\n");
return -1;
}
do
{
//判断是否有子目录
if (FileInfo.attrib & _A_SUBDIR)
{
//判断是子文件夹
if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
{
std::string newPath = FileInfo.name;// path + "\\" + FileInfo.name;
subFolders.push_back(newPath);
}
}
} while (_findnext64(Handle, &FileInfo) == 0);
_findclose(Handle);
return subFolders.size();
}
void test_folder()
{
std::vector<std::string> subFolders;
int sub_nums = TraverseDirectory("frames", subFolders);
int nums = 0;
char showname[128];
for (auto iter = subFolders.begin(); iter != subFolders.end(); iter++)
{
printf("%s ", iter->c_str());
std::vector<cv::String> images;
cv::glob(*iter, images);
for (auto jter = images.begin(); jter != images.end(); jter++)
{
cv::Mat src = cv::imread(*jter);
sprintf_s(showname, 128, "%d.jpg", nums++);
cv::imshow(showname, src);
cv::waitKey(0);
}
}
printf("\n");
printf("sub nums: %d\n", sub_nums);
//crop_images("frames");
system("pause");
}
void crop_images(std::string dataset_name)
{
std::string baseFolder = dataset_name + "\\rawpic";
//printf("%s\n", dataset_name.c_str());
std::vector<std::string> subFolders;
int sub_nums = TraverseDirectory(baseFolder, subFolders);
//printf("sub_nums = %d\n", sub_nums);
std::string dir_crop = dataset_name + "\\rawpic_crop";
_mkdir(dir_crop.c_str());
printf("start process!\n");
for (auto iter = subFolders.begin(); iter != subFolders.end(); iter++)
{
//printf("%s\n", iter->c_str());
std::string dir_crop_sub = dir_crop + "\\" + (*iter);
_mkdir(dir_crop_sub.c_str());
std::string sub_f = baseFolder + "\\" + (*iter);
std::vector<std::string> sub_Folders;
int sub_n = TraverseDirectory(sub_f, sub_Folders);
for (auto jter = sub_Folders.begin(); jter != sub_Folders.end(); jter++)
{
std::string dir_crop_sub_next = dir_crop_sub + "\\" + (*jter);
_mkdir(dir_crop_sub_next.c_str());
std::string folders = sub_f + "\\" + (*jter);
//printf("%s\n", folders.c_str());
std::vector<cv::String> imgPath;
cv::glob(folders, imgPath);
for (auto kter = imgPath.begin(); kter != imgPath.end(); kter++)
{
cv::Mat img = cv::imread(*kter);
std::vector<cv::Rect> rects = detectFaces(img);
std::size_t found = kter->find_last_of("\\");
std::string imageName = kter->substr(found + 1);
std::string imageName_save = dir_crop_sub_next + "\\" + imageName;
cv::Mat face = img(rects[0]).clone();
cv::resize(face, face, cv::Size(128, 128));
cv::imwrite(imageName_save.c_str(), face);
}
printf("finish the ssubfolder: %s\n", folders.c_str());
}
printf("finish the subfolder: %s\n\n", sub_f.c_str());
}
}
int main(int argc, char** argv)
{
crop_images("D:\\ubuntu_workspace\\SoftNet-SpotME-main\\CASME_sq");
system("pause");
return 0;
}
利用opencv检测人脸,裁剪出人脸区域并保存!
每天快乐敲代码,快乐生活