利用opencv的detector所截取的人脸图像有时候为空(漏检),针对这种情况,可以用Python判断文件大小,如果为零,去除。或c++和opencv判断img.empty(),去除空文件。代码如下:
Python版:
import os
fly = open('fy.txt','wb')
fln = open('fn.txt','wb')
fl=open('list.txt','rb')
line = fl.readlines()
for li in line:
li = li.strip()
size = os.path.getsize(li)
if size==0:
fln.write(li+'\n')
else:
fly.write(li+'\n')
fl.close()
fln.close()
fly.close()
本来是想通过
import cv2
img=cv2.imread(filename)
img.empty()
判断文件是否为空,然而Python中numpy对mat的存储格式是numpy.array,没有empty属性,只是可以看type(img)为‘NoneType’,但不知道如何写该条判断语句。留待后面有时间实现,作为增补。
c++版:
#include<opencv.hpp>
#include <iostream>
#include <stdio.h>
#include<string>
#include<fstream>
using namespace std;
using namespace cv;
int main()
{
int num = 0;
string strList = "list.txt";
string strListNew = "Eniminate.txt";
ifstream ifsList(strList.c_str());
ofstream ofsList(strListNew);
if (ifsList.fail())
{
printf("open list failed\n");
return -1;
}
if (ofsList.fail())
{
printf("open listNew failed\n");
return -1;
}
char szPath[256];
while (!ifsList.eof())
{
memset(szPath, 0, sizeof(char)* 256);
ifsList >> szPath;
Mat img = imread(szPath);
if (img.data)
{
ofsList << szPath << endl;
}
}
ifsList.close();
ofsList.close();
return 0;
}