从文本文件 dlib c++ 中读取人脸矢量
Read face vectors from text file dlib c++
我正在尝试从文本文件中读取 128D 面部矢量。
-
你是怎么做到的?
-
如果我有多个人脸向量,如何在一个变量中读取它们并将其与传入的人脸进行比较?
我正在尝试从文本文件中读取 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::vector
faceVector
现在包含文件中的值。
如果我有多个人脸向量,如何在一个变量中读取它们 并用它来与传入的面孔进行比较?
您可以通过编写一个函数来比较向量,该函数将向量作为参数并返回有意义的值。例如,此函数计算两点之间的距离:
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));
相关文章:
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 为什么 getline() 不读取文本文件中的所有内容?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 如何读取文本文件中的数据并为每列 c++ 分配数组
- 如何仅读取文本文件中的第一个值
- Getline在读取文本文件时有一种奇怪的行为
- 读取文本文件时的浮点确定性
- 如何检查字符串格式在读取C++文本文件时是否有效?
- 如何在不知道大小的情况下读取文本文件并存储到数组中
- 从文件中读取文本时出现编码问题
- Qt读取文本文件的一些信息
- 读取文本文件,C++
- 如何读取文本文件的第二、第三和第四行.C++
- 我想读取文本文件中的所有内容(但文本文件中只有一个双精度值),转换为双精度值并返回值
- 使用 stdin 读取文本文件,然后遍历多个数组
- 使用 c++ ifstream 读取文本文件问题
- C++读取文本文件并存储数据
- 读取文本文件时 seekg() 的奇怪偏移量
- 在C++中使用 Visual Studio 读取文本文件时遇到问题
- 无法读取文本文件中的每个单词