从文本文件 dlib c++ 中读取人脸矢量

Read face vectors from text file dlib c++

本文关键字:读取 文本 文件 dlib c++      更新时间:2023-10-16

我正在尝试从文本文件中读取 128D 面部矢量。

  1. 你是怎么做到的?

  2. 如果我有多个人脸向量,如何在一个变量中读取它们并将其与传入的人脸进行比较?

我正在尝试从文本文件中读取 128D 面部矢量。你是怎么做到的?

这取决于数字的格式。它们是否采用类似于 CSV 的格式?或者也许是像JSON或XML这样的描述符格式?还是用空格分隔的纯值?

例如,假设您有一个文本文件,其中包含以空格分隔的值,如下所示:

39.5 23.2 23.8 23.9 12.3

你可以像这样将其读入一个向量:

#include <fstream>
#include <vector>
int main(){
std::vector<double> faceVector; // we fill the numbers into this vector
std::fstream fs("path/to/nums.txt", std::fstream::in ); // make the file stream
double num;
while (fs >> num) { // read in the number. Stops when no numbers are left
faceVector.push_back(num); // add the number to the vector
}
for (double d : faceVector) {
printf("value is %fn", d); // print each number in the vector to see if it went right
}
return 0;
}

std::vectorfaceVector现在包含文件中的值。

如果我有多个人脸向量,如何在一个变量中读取它们 并用它来与传入的面孔进行比较?

您可以通过编写一个函数来比较向量,该函数将向量作为参数并返回有意义的值。例如,此函数计算两点之间的距离:

double distance(std::vector<double> &a, std::vector<double> &b) {
double result = 0.0;
for (int i = 0; i < a.size(); i++) result += (a[i] - b[i]) * (a[i] - b[i]);
return sqrt(result);
}

你可以像这样使用它:

std::vector<double> a = { 1, 2, 3 };
std::vector<double> b = { 4, 5, 6 };
printf("distance: %f", distance(a, b));