mirror of
https://github.com/deepinsight/insightface.git
synced 2026-01-23 03:50:18 +00:00
Add the inspireface project to cpp-package.
This commit is contained in:
176
cpp-package/inspireface/cpp/sample/utils/test_folder_helper.h
Executable file
176
cpp-package/inspireface/cpp/sample/utils/test_folder_helper.h
Executable file
@@ -0,0 +1,176 @@
|
||||
//
|
||||
// Created by YH-Mac on 2020/9/12.
|
||||
//
|
||||
|
||||
#ifndef LMKTRACKING_LIB_TEST_FOLDER_HELPER_H
|
||||
#define LMKTRACKING_LIB_TEST_FOLDER_HELPER_H
|
||||
|
||||
#include <dirent.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdio.h>
|
||||
#include <stack>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include "sys/types.h"
|
||||
#include "sys/stat.h"
|
||||
#include <chrono>
|
||||
//using namespace std;
|
||||
#define MODE (S_IRWXU | S_IRWXG | S_IRWXO)
|
||||
|
||||
using namespace std::chrono;
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
std::stack<high_resolution_clock::time_point> tictoc_stack;
|
||||
|
||||
void tic()
|
||||
{
|
||||
high_resolution_clock::time_point t1 = high_resolution_clock::now();
|
||||
tictoc_stack.push(t1);
|
||||
}
|
||||
|
||||
double toc(std::string msg = "", bool flag = true)
|
||||
{
|
||||
double diff = duration_cast<milliseconds>(high_resolution_clock::now() - tictoc_stack.top()).count();
|
||||
if(msg.size() > 0){
|
||||
if (flag)
|
||||
printf("%s time elapsed: %f ms\n", msg.c_str(), diff);
|
||||
}
|
||||
if(msg == "ir")
|
||||
return diff/3;
|
||||
|
||||
tictoc_stack.pop();
|
||||
return diff;
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
tictoc_stack = std::stack<high_resolution_clock::time_point>();
|
||||
}
|
||||
};
|
||||
|
||||
std::string dePrefix(std::string file_name, std::string prefix){
|
||||
int n = file_name.find_last_of(prefix);
|
||||
std::string de = file_name.substr(n + 1, file_name.size());
|
||||
return de;
|
||||
}
|
||||
|
||||
std::string deSuffix(std::string file_name, std::string suffix){
|
||||
int n = file_name.find_last_of(suffix);
|
||||
std::string de = file_name.substr(0, n);
|
||||
return de;
|
||||
}
|
||||
|
||||
bool check_folder(const std::string folder_path, bool is_create) {
|
||||
int res = access(folder_path.c_str(), 0);
|
||||
// std::cout << "res " << res << std::endl;
|
||||
if (res != -1) {
|
||||
std::cout << folder_path << " exists." << std::endl;
|
||||
return true;
|
||||
} else if(is_create){
|
||||
std::cout << folder_path << " not exists." << std::endl;
|
||||
int mkdir_res = mkdir(folder_path.c_str(), MODE);
|
||||
if (mkdir_res != -1) {
|
||||
std::cout << "mkdir successful." << std::endl;
|
||||
} else {
|
||||
std::cout << "mkdir fail." << std::endl;
|
||||
}
|
||||
return true;
|
||||
} else{
|
||||
std::cout << folder_path << " not exists." << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<float> read_feat_txt(const std::string feat_path){
|
||||
std::ifstream infile;
|
||||
infile.open(feat_path);
|
||||
if (!infile)
|
||||
std::cout << "file error: " << feat_path << std::endl;
|
||||
std::vector<float> feat;
|
||||
float t1;
|
||||
while (infile >> t1) {
|
||||
feat.push_back(t1);
|
||||
}
|
||||
infile.close();
|
||||
return feat;
|
||||
}
|
||||
|
||||
void extract_feat_to_txt(const std::string feat_path, std::vector<float> feat) {
|
||||
std::ofstream txt(feat_path);
|
||||
for (int i = 0; i < feat.size(); ++i) {
|
||||
txt << feat[i] << " ";
|
||||
}
|
||||
txt.close();
|
||||
// std::cout << "export feature to " << feat_path << std::endl;
|
||||
}
|
||||
|
||||
bool decide_ext(const char *gname, const char *nsuff)
|
||||
// gname: name of the given file
|
||||
// nsuff: suffix you need
|
||||
{
|
||||
char dot = '.';
|
||||
char suff[10] = {0};
|
||||
int c;
|
||||
int j = 0;
|
||||
c = strlen(gname);
|
||||
// std::cout<< c << '\n';
|
||||
for (int i = 0; i < c; i++) {
|
||||
if (gname[i] == dot)
|
||||
j = i;
|
||||
}
|
||||
int k = j;
|
||||
j = c - j - 1;
|
||||
for (int i = 0; i < j; i++) {
|
||||
suff[i] = gname[k + i + 1];
|
||||
}
|
||||
if (0 == strcmp(suff, nsuff))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string>
|
||||
readFileListSuffix(const char *basePath, const char *suffix, std::vector<std::string>& file_names, bool recursive) {
|
||||
DIR *dir;
|
||||
struct dirent *ptr;
|
||||
char base[1000];
|
||||
std::vector<std::string> file_list;
|
||||
if ((dir = opendir(basePath)) == NULL) {
|
||||
perror("Open dir error...");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while ((ptr = readdir(dir)) != NULL) {
|
||||
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
|
||||
continue;
|
||||
else if (ptr->d_type == 8) { ///file
|
||||
// printf("d_name:%s/%s\n", basePath, ptr->d_name);
|
||||
if (decide_ext(ptr->d_name, suffix)) {
|
||||
std::stringstream ss;
|
||||
ss << basePath << "/" << ptr->d_name;
|
||||
std::string path = ss.str();
|
||||
file_names.push_back(ptr->d_name);
|
||||
file_list.push_back(path);
|
||||
}
|
||||
} else if (ptr->d_type == 10) { ///link file
|
||||
// printf("d_name:%s/%s\n", basePath, ptr->d_name);
|
||||
} else if (ptr->d_type == 4 and recursive) ///dir
|
||||
{
|
||||
memset(base, '\0', sizeof(base));
|
||||
strcpy(base, basePath);
|
||||
strcat(base, "/");
|
||||
strcat(base, ptr->d_name);
|
||||
readFileListSuffix(base, suffix, file_names, recursive);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
return file_list;
|
||||
}
|
||||
|
||||
#endif //LMKTRACKING_LIB_TEST_FOLDER_HELPER_H
|
||||
122
cpp-package/inspireface/cpp/sample/utils/test_helper.h
Executable file
122
cpp-package/inspireface/cpp/sample/utils/test_helper.h
Executable file
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// Created by Jack YU on 2020/6/11.
|
||||
//
|
||||
|
||||
#ifndef ZEUSEESTRACKING_LIB_TEST_HELPER_H
|
||||
#define ZEUSEESTRACKING_LIB_TEST_HELPER_H
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "middleware/camera_stream/camera_stream.h"
|
||||
|
||||
namespace TestUtils {
|
||||
|
||||
inline uint8_t *rgb2nv21(const cv::Mat &Img) {
|
||||
if (Img.empty()) {
|
||||
exit(0);
|
||||
}
|
||||
int cols = Img.cols;
|
||||
int rows = Img.rows;
|
||||
|
||||
int Yindex = 0;
|
||||
int UVindex = rows * cols;
|
||||
|
||||
unsigned char *yuvbuff =
|
||||
new unsigned char[static_cast<int>(1.5 * rows * cols)];
|
||||
|
||||
cv::Mat NV21(rows + rows / 2, cols, CV_8UC1);
|
||||
cv::Mat OpencvYUV;
|
||||
cv::Mat OpencvImg;
|
||||
cv::cvtColor(Img, OpencvYUV, cv::COLOR_BGR2YUV_YV12);
|
||||
for (int i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < cols; j++) {
|
||||
uchar *YPointer = NV21.ptr<uchar>(i);
|
||||
int B = Img.at<cv::Vec3b>(i, j)[0];
|
||||
int G = Img.at<cv::Vec3b>(i, j)[1];
|
||||
int R = Img.at<cv::Vec3b>(i, j)[2];
|
||||
int Y = (77 * R + 150 * G + 29 * B) >> 8;
|
||||
YPointer[j] = Y;
|
||||
yuvbuff[Yindex++] = (Y < 0) ? 0 : ((Y > 255) ? 255 : Y);
|
||||
uchar *UVPointer = NV21.ptr<uchar>(rows + i / 2);
|
||||
if (i % 2 == 0 && (j) % 2 == 0) {
|
||||
int U = ((-44 * R - 87 * G + 131 * B) >> 8) + 128;
|
||||
int V = ((131 * R - 110 * G - 21 * B) >> 8) + 128;
|
||||
UVPointer[j] = V;
|
||||
UVPointer[j + 1] = U;
|
||||
yuvbuff[UVindex++] = (V < 0) ? 0 : ((V > 255) ? 255 : V);
|
||||
yuvbuff[UVindex++] = (U < 0) ? 0 : ((U > 255) ? 255 : U);
|
||||
}
|
||||
}
|
||||
}
|
||||
return yuvbuff;
|
||||
}
|
||||
|
||||
inline void rotate(const cv::Mat &image, cv::Mat &out,
|
||||
inspire::ROTATION_MODE mode) {
|
||||
if (mode == inspire::ROTATION_90) {
|
||||
cv::transpose(image, out);
|
||||
cv::flip(out, out, 2);
|
||||
} else if (mode == inspire::ROTATION_180) {
|
||||
cv::flip(out, out, -1);
|
||||
} else if (mode == inspire::ROTATION_270) {
|
||||
cv::transpose(image, out);
|
||||
cv::flip(out, out, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GetFilesInDirectory(std::vector<std::string> &out, const std::string &directory)
|
||||
{
|
||||
#ifdef WINDOWS
|
||||
HANDLE dir;
|
||||
WIN32_FIND_DATA file_data;
|
||||
|
||||
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
|
||||
return; /* No files found */
|
||||
|
||||
do {
|
||||
const string file_name = file_data.cFileName;
|
||||
const string full_file_name = directory + "/" + file_name;
|
||||
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
|
||||
if (file_name[0] == '.')
|
||||
continue;
|
||||
|
||||
if (is_directory)
|
||||
continue;
|
||||
|
||||
out.push_back(full_file_name);
|
||||
} while (FindNextFile(dir, &file_data));
|
||||
|
||||
FindClose(dir);
|
||||
#else
|
||||
DIR *dir;
|
||||
class dirent *ent;
|
||||
class stat st;
|
||||
|
||||
dir = opendir(directory.c_str());
|
||||
while ((ent = readdir(dir)) != NULL) {
|
||||
const std::string file_name = ent->d_name;
|
||||
const std::string full_file_name = directory + "/" + file_name;
|
||||
|
||||
if (file_name[0] == '.')
|
||||
continue;
|
||||
|
||||
if (stat(full_file_name.c_str(), &st) == -1)
|
||||
continue;
|
||||
|
||||
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
|
||||
|
||||
if (is_directory)
|
||||
continue;
|
||||
|
||||
out.push_back(full_file_name);
|
||||
}
|
||||
closedir(dir);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace TestUtils
|
||||
|
||||
#endif // ZEUSEESTRACKING_LIB_TEST_HELPER_H
|
||||
Reference in New Issue
Block a user